Should cast casted attribute on direct assignment

This commit is contained in:
wildchild 2009-07-21 05:01:34 +06:00
parent e27135cb1e
commit f65d8bbbcc
2 changed files with 10 additions and 12 deletions

View file

@ -58,11 +58,7 @@ module CouchRest
key = self.has_key?(property.name) ? property.name : property.name.to_sym
# Don't cast the property unless it has a value
next if (value = self[key]).nil?
obj = property.typecast(value)
if obj.respond_to?(:casted_by)
obj.casted_by = self
end
self[property.name] = obj
write_property(property, value)
end
end
@ -70,16 +66,18 @@ module CouchRest
def write_attribute(name, value)
unless (property = property(name)).nil?
if property.casted
self[name] = value
else
self[name] = property.typecast(value)
end
write_property(property, value)
else
self[name] = value
end
end
def write_property(property, value)
value = property.typecast(value)
value.casted_by = self if value.respond_to?(:casted_by)
self[property.name] = value
end
def property(name)
properties.find {|p| p.name == name.to_s}
end

View file

@ -50,9 +50,9 @@ describe "assigning a value to casted attribute after initializing an object" do
@car.driver.name.should == 'Matt'
end
it "should not cast attribute" do
it "should cast attribute" do
@car.driver = JSON.parse(JSON.generate(@driver))
@car.driver.should_not be_instance_of(Driver)
@car.driver.should be_instance_of(Driver)
end
end