Should cast casted attribute on direct assignment
This commit is contained in:
parent
e27135cb1e
commit
f65d8bbbcc
|
@ -58,11 +58,7 @@ module CouchRest
|
||||||
key = self.has_key?(property.name) ? property.name : property.name.to_sym
|
key = self.has_key?(property.name) ? property.name : property.name.to_sym
|
||||||
# Don't cast the property unless it has a value
|
# Don't cast the property unless it has a value
|
||||||
next if (value = self[key]).nil?
|
next if (value = self[key]).nil?
|
||||||
obj = property.typecast(value)
|
write_property(property, value)
|
||||||
if obj.respond_to?(:casted_by)
|
|
||||||
obj.casted_by = self
|
|
||||||
end
|
|
||||||
self[property.name] = obj
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -70,16 +66,18 @@ module CouchRest
|
||||||
|
|
||||||
def write_attribute(name, value)
|
def write_attribute(name, value)
|
||||||
unless (property = property(name)).nil?
|
unless (property = property(name)).nil?
|
||||||
if property.casted
|
write_property(property, value)
|
||||||
self[name] = value
|
|
||||||
else
|
|
||||||
self[name] = property.typecast(value)
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
self[name] = value
|
self[name] = value
|
||||||
end
|
end
|
||||||
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)
|
def property(name)
|
||||||
properties.find {|p| p.name == name.to_s}
|
properties.find {|p| p.name == name.to_s}
|
||||||
end
|
end
|
||||||
|
|
|
@ -50,9 +50,9 @@ describe "assigning a value to casted attribute after initializing an object" do
|
||||||
@car.driver.name.should == 'Matt'
|
@car.driver.name.should == 'Matt'
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should not cast attribute" do
|
it "should cast attribute" do
|
||||||
@car.driver = JSON.parse(JSON.generate(@driver))
|
@car.driver = JSON.parse(JSON.generate(@driver))
|
||||||
@car.driver.should_not be_instance_of(Driver)
|
@car.driver.should be_instance_of(Driver)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue