lib/couchrest/model/casted_array.rb in couchrest_model-2.1.0.rc1 vs lib/couchrest/model/casted_array.rb in couchrest_model-2.2.0.beta1
- old
+ new
@@ -3,10 +3,11 @@
# elements of the array.
#
module CouchRest::Model
class CastedArray < Array
+ include CouchRest::Model::Configuration
include CouchRest::Model::CastedBy
include CouchRest::Model::Dirty
attr_accessor :casted_by_property
def initialize(array, property, parent = nil)
@@ -27,61 +28,48 @@
def unshift(obj)
super(instantiate_and_cast(obj))
end
- def []= index, obj
- value = instantiate_and_cast(obj, false)
- couchrest_parent_will_change! if use_dirty? && value != self[index]
- super(index, value)
+ def []=(index, obj)
+ super(index, instantiate_and_cast(obj))
end
- def insert index, *args
- values = *args.map{|obj| instantiate_and_cast(obj, false)}
- couchrest_parent_will_change! if use_dirty?
+ def insert(index, *args)
+ values = args.map{|obj| instantiate_and_cast(obj)}
super(index, *values)
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 clear
- couchrest_parent_will_change! if use_dirty? && self.length > 0
- super
- end
-
- def delete(obj)
- couchrest_parent_will_change! if use_dirty? && self.length > 0
- super(obj)
- end
-
- def delete_at(index)
- couchrest_parent_will_change! if use_dirty? && self.length > 0
- super(index)
- end
-
def build(*args)
obj = casted_by_property.build(*args)
self.push(obj)
obj
end
def as_couch_json
map{ |v| (v.respond_to?(:as_couch_json) ? v.as_couch_json : v)}
end
+ # Overwrite the standard dirty tracking clearing.
+ # We don't have any properties, but we do need to check
+ # entries in our array.
+ def clear_changes_information
+ if use_dirty?
+ each do |val|
+ if val.respond_to?(:clear_changes_information)
+ val.clear_changes_information
+ end
+ end
+ @original_change_data = current_change_data
+ else
+ @original_change_data = nil
+ end
+ end
+
protected
- def instantiate_and_cast(obj, change = true)
+ def instantiate_and_cast(obj)
property = casted_by_property
- couchrest_parent_will_change! if change && use_dirty?
if casted_by && property && obj.class != property.type
property.cast_value(casted_by, obj)
else
obj.casted_by = casted_by if obj.respond_to?(:casted_by)
obj.casted_by_property = casted_by_property if obj.respond_to?(:casted_by_property)