Class: CouchRest::Model::CollectionOfProxy

Inherits:
CastedArray
  • Object
show all
Defined in:
lib/couchrest/model/associations.rb

Overview

Special proxy for a collection of items so that adding and removing to the list automatically updates the associated property.

Instance Attribute Summary

Attributes inherited from CastedArray

#casted_by_property

Instance Method Summary collapse

Methods inherited from CastedArray

#build, #clear, #delete, #delete_at

Methods included from Dirty

#couchrest_attribute_will_change!, #couchrest_parent_will_change!, #use_dirty?

Methods included from CastedBy

#base_doc, #base_doc?

Constructor Details

#initialize(array, property, parent) ⇒ CollectionOfProxy



177
178
179
180
181
182
183
184
185
# File 'lib/couchrest/model/associations.rb', line 177

def initialize(array, property, parent)
  (array ||= []).compact!
  super(array, property, parent)
  casted_by[casted_by_property.to_s] = [] # replace the original array!
  array.compact.each do |obj|
    check_obj(obj)
    casted_by[casted_by_property.to_s] << obj.id
  end
end

Instance Method Details

#<<(obj) ⇒ Object



187
188
189
190
191
# File 'lib/couchrest/model/associations.rb', line 187

def << obj
  check_obj(obj)
  casted_by[casted_by_property.to_s] << obj.id
  super(obj)
end

#[]=(index, obj) ⇒ Object



205
206
207
208
209
# File 'lib/couchrest/model/associations.rb', line 205

def []= index, obj
  check_obj(obj)
  casted_by[casted_by_property.to_s][index] = obj.id
  super(index, obj)
end

#popObject



211
212
213
214
# File 'lib/couchrest/model/associations.rb', line 211

def pop
  casted_by[casted_by_property.to_s].pop
  super
end

#push(obj) ⇒ Object



193
194
195
196
197
# File 'lib/couchrest/model/associations.rb', line 193

def push(obj)
  check_obj(obj)
  casted_by[casted_by_property.to_s].push obj.id
  super(obj)
end

#shiftObject



216
217
218
219
# File 'lib/couchrest/model/associations.rb', line 216

def shift
  casted_by[casted_by_property.to_s].shift
  super
end

#unshift(obj) ⇒ Object



199
200
201
202
203
# File 'lib/couchrest/model/associations.rb', line 199

def unshift(obj)
  check_obj(obj)
  casted_by[casted_by_property.to_s].unshift obj.id
  super(obj)
end