2010-03-31 10:25:33 +02:00
|
|
|
#
|
|
|
|
# Wrapper around Array so that the casted_by attribute is set in all
|
|
|
|
# elements of the array.
|
|
|
|
#
|
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
module CouchRest::Model
|
2010-03-31 10:25:33 +02:00
|
|
|
class CastedArray < Array
|
|
|
|
attr_accessor :casted_by
|
2010-06-16 21:04:53 +02:00
|
|
|
attr_accessor :property
|
|
|
|
|
|
|
|
def initialize(array, property)
|
|
|
|
self.property = property
|
|
|
|
super(array)
|
|
|
|
end
|
2010-03-31 10:25:33 +02:00
|
|
|
|
|
|
|
def << obj
|
2010-06-16 21:04:53 +02:00
|
|
|
super(instantiate_and_cast(obj))
|
2010-03-31 10:25:33 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def push(obj)
|
2010-06-16 21:04:53 +02:00
|
|
|
super(instantiate_and_cast(obj))
|
2010-03-31 10:25:33 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def []= index, obj
|
2010-06-16 21:04:53 +02:00
|
|
|
super(index, instantiate_and_cast(obj))
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def instantiate_and_cast(obj)
|
|
|
|
if self.casted_by && self.property && obj.class != self.property.type_class
|
|
|
|
self.property.cast_value(self.casted_by, obj)
|
|
|
|
else
|
|
|
|
obj.casted_by = self.casted_by if obj.respond_to?(:casted_by)
|
|
|
|
obj
|
|
|
|
end
|
2010-03-31 10:25:33 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|