Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Check timestamps before overwriting them #24

Merged
merged 1 commit into from Apr 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/simple_states/event.rb
Expand Up @@ -36,7 +36,11 @@ def set_attrs(obj, data)
end

def set_attr(obj, key, value)
obj.send(:"#{key}=", value) if obj.respond_to?(:"#{key}=")
obj.send(:"#{key}=", value) if obj.respond_to?(:"#{key}=") and not has_timestamp?(obj, key)
end

def has_timestamp?(obj, key)
key.to_s.end_with?('_at') && obj.respond_to?(key) && obj.send(key)
end

def ordered?(obj, data)
Expand Down
22 changes: 21 additions & 1 deletion spec/events_spec.rb
Expand Up @@ -34,11 +34,31 @@
end

describe 'when a timestamp is passed' do
let(:attrs) { { started_at: now - 60 } }
let(:attrs) { { started_at: now - 60, state: "started" } }
it { expect { obj.start(attrs) }.to change { obj.started_at }.to(attrs[:started_at]) }
end
end

describe 'ignores timestamp' do
describe 'when started_at had been set earlier' do
let(:attrs) { { started_at: now - 60, finished_at: now - 50, state: "passed" } }
let(:early) { now - 55 }
before { obj.started_at = early }
before { obj.finish(attrs) }
it { expect(obj.started_at).to eq early }
it { expect(obj.finished_at).to eq attrs[:finished_at] }
end

describe 'when finished_at had been set earlier' do
let(:attrs) { { started_at: now - 60, finished_at: now - 50, state: "passed" } }
let(:early) { now - 45 }
before { obj.finished_at = early }
before { obj.finish(attrs) }
it { expect(obj.started_at).to eq attrs[:started_at] }
it { expect(obj.finished_at).to eq early }
end
end

describe 'accepts an arbitrary attribute' do
before { klass.send(:attr_accessor, :foo) }
it { expect { obj.start(foo: :bar) }.to change { obj.foo }.to(:bar) }
Expand Down