couchrest_model/lib/couchrest/model/casted_array.rb

65 lines
1.5 KiB
Ruby
Raw Normal View History

#
# Wrapper around Array so that the casted_by attribute is set in all
# elements of the array.
#
module CouchRest::Model
class CastedArray < Array
include CouchRest::Model::Dirty
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
def << obj
couchrest_parent_will_change! if use_dirty?
2010-06-16 21:04:53 +02:00
super(instantiate_and_cast(obj))
end
def push(obj)
couchrest_parent_will_change! if use_dirty?
2010-06-16 21:04:53 +02:00
super(instantiate_and_cast(obj))
end
def pop
couchrest_parent_will_change! if use_dirty? && self.length > 0
super
end
def shift
couchrest_parent_will_change! if use_dirty? && self.length > 0
super
end
def unshift(obj)
couchrest_parent_will_change! if use_dirty?
super(instantiate_and_cast(obj))
end
def []= index, obj
value = instantiate_and_cast(obj)
couchrest_parent_will_change! if use_dirty? && value != self[index]
super(index, value)
2010-06-16 21:04:53 +02:00
end
def clear
couchrest_parent_will_change! if use_dirty? && self.length > 0
super
end
2010-06-16 21:04:53 +02:00
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
end
end
end