casted_by is now set on assignment to a document.
This commit is contained in:
parent
3e4c90f104
commit
efeb654114
3 changed files with 95 additions and 1 deletions
|
@ -44,13 +44,14 @@ module CouchRest
|
|||
target = property.type
|
||||
if target.is_a?(Array)
|
||||
klass = ::CouchRest.constantize(target[0])
|
||||
self[property.name] = self[key].collect do |value|
|
||||
arr = self[key].collect do |value|
|
||||
# Auto parse Time objects
|
||||
obj = ( (property.init_method == 'new') && klass == Time) ? Time.parse(value) : klass.send(property.init_method, value)
|
||||
obj.casted_by = self if obj.respond_to?(:casted_by)
|
||||
obj.document_saved = true if obj.respond_to?(:document_saved)
|
||||
obj
|
||||
end
|
||||
self[property.name] = target[0] != 'String' ? CastedArray.new(arr) : arr
|
||||
else
|
||||
# Auto parse Time objects
|
||||
self[property.name] = if ((property.init_method == 'new') && target == 'Time')
|
||||
|
@ -63,6 +64,7 @@ module CouchRest
|
|||
self[property.name].casted_by = self if self[property.name].respond_to?(:casted_by)
|
||||
self[property.name].document_saved = true if self[property.name].respond_to?(:document_saved)
|
||||
end
|
||||
self[property.name].casted_by = self if self[property.name].respond_to?(:casted_by)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -109,6 +111,14 @@ module CouchRest
|
|||
meth = property.name
|
||||
class_eval <<-EOS
|
||||
def #{meth}=(value)
|
||||
if #{property.casted} && value.is_a?(Array)
|
||||
arr = CastedArray.new
|
||||
arr.casted_by = self
|
||||
value.each { |v| arr << v }
|
||||
value = arr
|
||||
elsif #{property.casted}
|
||||
value.casted_by = self if value.respond_to?(:casted_by)
|
||||
end
|
||||
self['#{meth}'] = value
|
||||
end
|
||||
EOS
|
||||
|
|
|
@ -38,3 +38,22 @@ module CouchRest
|
|||
|
||||
end
|
||||
end
|
||||
|
||||
class CastedArray < Array
|
||||
attr_accessor :casted_by
|
||||
|
||||
def << obj
|
||||
obj.casted_by = self.casted_by if obj.respond_to?(:casted_by)
|
||||
super(obj)
|
||||
end
|
||||
|
||||
def push(obj)
|
||||
obj.casted_by = self.casted_by if obj.respond_to?(:casted_by)
|
||||
super(obj)
|
||||
end
|
||||
|
||||
def []= index, obj
|
||||
obj.casted_by = self.casted_by if obj.respond_to?(:casted_by)
|
||||
super(index, obj)
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue