diff --git a/motion/model/model.rb b/motion/model/model.rb index 046df6a..b9825e6 100644 --- a/motion/model/model.rb +++ b/motion/model/model.rb @@ -485,6 +485,11 @@ def update_attributes(attrs) def read_attribute(name) @data[name] end + + def write_attribute(attr_name, value) + @data[attr_name] = value + @dirty = true + end # Default to_i implementation returns value of id column, much as # in Rails. diff --git a/spec/model_spec.rb b/spec/model_spec.rb index 6b6f22e..0c8a311 100644 --- a/spec/model_spec.rb +++ b/spec/model_spec.rb @@ -16,6 +16,15 @@ class ATask columns :name, :details, :some_day end +class BTask + include MotionModel::Model + include MotionModel::ArrayModelAdapter + columns :name, :details + def details=(value) + write_attribute(:details, "overridden") + end +end + class TypeCast include MotionModel::Model include MotionModel::ArrayModelAdapter @@ -254,6 +263,25 @@ class TypeCast @task.custom_attribute_by_method.should == 'Feed the Cat - Get food, pour out' end end + + describe 'overloading accessors using write_attribute' do + before do + BTask.delete_all + end + + it 'updates the attribute on creation' do + @task = BTask.create :name => 'foo', :details => 'bar' + @task.details.should.equal('overridden') + @task.should.not.be.dirty + end + + it 'updates the attribute but does not save a new instance' do + @task = BTask.new :name => 'foo', :details => 'bar' + @task.details.should.equal('overridden') + @task.should.be.dirty + end + + end describe 'protecting timestamps' do class NoTimestamps