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
|
end
|
||||||
|
|
||||||
def read_attribute(property)
|
def read_attribute(property)
|
||||||
self[property.to_s]
|
prop = find_property!(property)
|
||||||
|
self[prop.to_s]
|
||||||
end
|
end
|
||||||
|
|
||||||
def write_attribute(property, value)
|
def write_attribute(property, value)
|
||||||
prop = property.is_a?(Property) ? property : self.class.properties.detect {|p| p.to_s == property.to_s}
|
prop = find_property!(property)
|
||||||
raise "Missing property definition for #{property.to_s}" unless prop
|
|
||||||
self[prop.to_s] = prop.cast(self, value)
|
self[prop.to_s] = prop.cast(self, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -40,6 +40,13 @@ module CouchRest
|
||||||
end
|
end
|
||||||
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
|
module ClassMethods
|
||||||
|
|
||||||
def property(name, *options, &block)
|
def property(name, *options, &block)
|
||||||
|
|
|
@ -59,42 +59,54 @@ describe "Model properties" do
|
||||||
@card.updated_at.should_not be_nil
|
@card.updated_at.should_not be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should let you use read_attribute method" do
|
describe '#read_attribute' do
|
||||||
@card.last_name = "Aimonetti"
|
it "should let you use read_attribute method" do
|
||||||
@card.read_attribute(:last_name).should eql('Aimonetti')
|
@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').should eql('Aimonetti')
|
||||||
@card.read_attribute(last_name_prop).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
|
end
|
||||||
|
|
||||||
it "should let you use write_attribute method" do
|
describe '#write_attribute' do
|
||||||
@card.write_attribute(:last_name, 'Aimonetti 1')
|
it "should let you use write_attribute method" do
|
||||||
@card.last_name.should eql('Aimonetti 1')
|
@card.write_attribute(:last_name, 'Aimonetti 1')
|
||||||
@card.write_attribute('last_name', 'Aimonetti 2')
|
@card.last_name.should eql('Aimonetti 1')
|
||||||
@card.last_name.should eql('Aimonetti 2')
|
@card.write_attribute('last_name', 'Aimonetti 2')
|
||||||
last_name_prop = @card.properties.find{|p| p.name == 'last_name'}
|
@card.last_name.should eql('Aimonetti 2')
|
||||||
@card.write_attribute(last_name_prop, 'Aimonetti 3')
|
last_name_prop = @card.properties.find{|p| p.name == 'last_name'}
|
||||||
@card.last_name.should eql('Aimonetti 3')
|
@card.write_attribute(last_name_prop, 'Aimonetti 3')
|
||||||
end
|
@card.last_name.should eql('Aimonetti 3')
|
||||||
|
end
|
||||||
|
|
||||||
it "should let you use write_attribute on readonly properties" do
|
it 'should raise an error if the property does not exist' do
|
||||||
lambda {
|
expect { @card.write_attribute(:this_property_should_not_exist, 823) }.to raise_error(ArgumentError)
|
||||||
@card.read_only_value = "foo"
|
end
|
||||||
}.should raise_error
|
|
||||||
@card.write_attribute(:read_only_value, "foo")
|
|
||||||
@card.read_only_value.should == 'foo'
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should cast via write_attribute" do
|
it "should let you use write_attribute on readonly properties" do
|
||||||
@card.write_attribute(:cast_alias, {:name => ["Sam", "Lown"]})
|
lambda {
|
||||||
@card.cast_alias.class.should eql(Person)
|
@card.read_only_value = "foo"
|
||||||
@card.cast_alias.name.last.should eql("Lown")
|
}.should raise_error
|
||||||
end
|
@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
|
it "should cast via write_attribute" do
|
||||||
@card.write_attribute(:first_name, {:name => "Sam"})
|
@card.write_attribute(:cast_alias, {:name => ["Sam", "Lown"]})
|
||||||
@card.first_name.class.should eql(Hash)
|
@card.cast_alias.class.should eql(Person)
|
||||||
@card.first_name[:name].should eql("Sam")
|
@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
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue