refactor #read_ and #write_attribute to behave the same when called with a missing property
Signed-off-by: Marcos Tapajós <tapajos@gmail.com>
This commit is contained in:
parent
49c9656fe3
commit
9437678380
|
@ -23,12 +23,12 @@ module CouchRest
|
|||
end
|
||||
|
||||
def read_attribute(property)
|
||||
self[property.to_s]
|
||||
prop = find_property!(property)
|
||||
self[prop.to_s]
|
||||
end
|
||||
|
||||
def write_attribute(property, value)
|
||||
prop = property.is_a?(Property) ? property : self.class.properties.detect {|p| p.to_s == property.to_s}
|
||||
raise "Missing property definition for #{property.to_s}" unless prop
|
||||
prop = find_property!(property)
|
||||
self[prop.to_s] = prop.cast(self, value)
|
||||
end
|
||||
|
||||
|
@ -40,6 +40,13 @@ module CouchRest
|
|||
end
|
||||
end
|
||||
|
||||
private
|
||||
def find_property!(property)
|
||||
prop = property.is_a?(Property) ? property : self.class.properties.detect {|p| p.to_s == property.to_s}
|
||||
raise ArgumentError, "Missing property definition for #{property.to_s}" unless prop
|
||||
prop
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
|
||||
def property(name, *options, &block)
|
||||
|
|
|
@ -59,42 +59,54 @@ describe "Model properties" do
|
|||
@card.updated_at.should_not be_nil
|
||||
end
|
||||
|
||||
it "should let you use read_attribute method" do
|
||||
@card.last_name = "Aimonetti"
|
||||
@card.read_attribute(:last_name).should eql('Aimonetti')
|
||||
@card.read_attribute('last_name').should eql('Aimonetti')
|
||||
last_name_prop = @card.properties.find{|p| p.name == 'last_name'}
|
||||
@card.read_attribute(last_name_prop).should eql('Aimonetti')
|
||||
describe '#read_attribute' do
|
||||
it "should let you use read_attribute method" do
|
||||
@card.last_name = "Aimonetti"
|
||||
@card.read_attribute(:last_name).should eql('Aimonetti')
|
||||
@card.read_attribute('last_name').should eql('Aimonetti')
|
||||
last_name_prop = @card.properties.find{|p| p.name == 'last_name'}
|
||||
@card.read_attribute(last_name_prop).should eql('Aimonetti')
|
||||
end
|
||||
|
||||
it 'should raise an error if the property does not exist' do
|
||||
expect { @card.read_attribute(:this_property_should_not_exist) }.to raise_error(ArgumentError)
|
||||
end
|
||||
end
|
||||
|
||||
it "should let you use write_attribute method" do
|
||||
@card.write_attribute(:last_name, 'Aimonetti 1')
|
||||
@card.last_name.should eql('Aimonetti 1')
|
||||
@card.write_attribute('last_name', 'Aimonetti 2')
|
||||
@card.last_name.should eql('Aimonetti 2')
|
||||
last_name_prop = @card.properties.find{|p| p.name == 'last_name'}
|
||||
@card.write_attribute(last_name_prop, 'Aimonetti 3')
|
||||
@card.last_name.should eql('Aimonetti 3')
|
||||
end
|
||||
describe '#write_attribute' do
|
||||
it "should let you use write_attribute method" do
|
||||
@card.write_attribute(:last_name, 'Aimonetti 1')
|
||||
@card.last_name.should eql('Aimonetti 1')
|
||||
@card.write_attribute('last_name', 'Aimonetti 2')
|
||||
@card.last_name.should eql('Aimonetti 2')
|
||||
last_name_prop = @card.properties.find{|p| p.name == 'last_name'}
|
||||
@card.write_attribute(last_name_prop, 'Aimonetti 3')
|
||||
@card.last_name.should eql('Aimonetti 3')
|
||||
end
|
||||
|
||||
it "should let you use write_attribute on readonly properties" do
|
||||
lambda {
|
||||
@card.read_only_value = "foo"
|
||||
}.should raise_error
|
||||
@card.write_attribute(:read_only_value, "foo")
|
||||
@card.read_only_value.should == 'foo'
|
||||
end
|
||||
it 'should raise an error if the property does not exist' do
|
||||
expect { @card.write_attribute(:this_property_should_not_exist, 823) }.to raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "should cast via write_attribute" do
|
||||
@card.write_attribute(:cast_alias, {:name => ["Sam", "Lown"]})
|
||||
@card.cast_alias.class.should eql(Person)
|
||||
@card.cast_alias.name.last.should eql("Lown")
|
||||
end
|
||||
it "should let you use write_attribute on readonly properties" do
|
||||
lambda {
|
||||
@card.read_only_value = "foo"
|
||||
}.should raise_error
|
||||
@card.write_attribute(:read_only_value, "foo")
|
||||
@card.read_only_value.should == 'foo'
|
||||
end
|
||||
|
||||
it "should not cast via write_attribute if property not casted" do
|
||||
@card.write_attribute(:first_name, {:name => "Sam"})
|
||||
@card.first_name.class.should eql(Hash)
|
||||
@card.first_name[:name].should eql("Sam")
|
||||
it "should cast via write_attribute" do
|
||||
@card.write_attribute(:cast_alias, {:name => ["Sam", "Lown"]})
|
||||
@card.cast_alias.class.should eql(Person)
|
||||
@card.cast_alias.name.last.should eql("Lown")
|
||||
end
|
||||
|
||||
it "should not cast via write_attribute if property not casted" do
|
||||
@card.write_attribute(:first_name, {:name => "Sam"})
|
||||
@card.first_name.class.should eql(Hash)
|
||||
@card.first_name[:name].should eql("Sam")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue