Fixed some casting issues for assigned properties

This commit is contained in:
Peter Gumeson 2009-06-12 12:22:58 -07:00
parent cdf998b185
commit 62e8709df7

View file

@ -41,7 +41,7 @@ module CouchRest
end end
end end
def cast_property(property) def cast_property(property, assigned=false)
return unless property.casted return unless property.casted
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
@ -53,22 +53,23 @@ module CouchRest
unless value.instance_of?(klass) unless value.instance_of?(klass)
value = convert_property_value(property, klass, value) value = convert_property_value(property, klass, value)
end end
associate_casted_to_parent(value) associate_casted_to_parent(value, assigned)
value value
end end
self[key] = target[0] != 'String' ? CastedArray.new(arr) : arr self[key] = klass != String ? CastedArray.new(arr) : arr
self[key].casted_by = self if self[key].respond_to?(:casted_by)
else else
klass = ::CouchRest.constantize(target) klass = ::CouchRest.constantize(target)
unless self[key].instance_of?(klass) unless self[key].instance_of?(klass)
self[key] = convert_property_value(property, klass, self[property.name]) self[key] = convert_property_value(property, klass, self[property.name])
end end
associate_casted_to_parent(self[property.name]) associate_casted_to_parent(self[property.name], assigned)
end end
end end
def associate_casted_to_parent(casted) def associate_casted_to_parent(casted, assigned)
casted.casted_by = self if casted.respond_to?(:casted_by) casted.casted_by = self if casted.respond_to?(:casted_by)
casted.document_saved = true if casted.respond_to?(:document_saved) casted.document_saved = true if !assigned && casted.respond_to?(:document_saved)
end end
def convert_property_value(property, klass, value) def convert_property_value(property, klass, value)
@ -83,7 +84,7 @@ module CouchRest
return unless self.class.properties return unless self.class.properties
property = self.class.properties.detect{|property| property.name == property_name} property = self.class.properties.detect{|property| property.name == property_name}
return unless property return unless property
cast_property(property) cast_property(property, true)
end end
module ClassMethods module ClassMethods