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 Method Summary collapse

Methods inherited from CastedArray

#as_couch_json, #build, #clear_changes_information, #insert

Methods included from Dirty

#changed?, #changes, #clear_changes_information, #use_dirty?

Methods included from CastedBy

#base_doc, #base_doc?

Constructor Details

#initialize(array, property, parent) ⇒ CollectionOfProxy

Returns a new instance of CollectionOfProxy.


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

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

Instance Method Details

#<<(obj) ⇒ Object


189
190
191
192
193
# File 'lib/couchrest/model/associations.rb', line 189

def << obj
  check_obj(obj)
  casted_by_attribute << obj.id
  super(obj)
end

#[]=(index, obj) ⇒ Object


207
208
209
210
211
# File 'lib/couchrest/model/associations.rb', line 207

def []= index, obj
  check_obj(obj)
  casted_by_attribute[index] = obj.id
  super(index, obj)
end

#popObject


213
214
215
216
# File 'lib/couchrest/model/associations.rb', line 213

def pop
  casted_by_attribute.pop
  super
end

#push(obj) ⇒ Object


195
196
197
198
199
# File 'lib/couchrest/model/associations.rb', line 195

def push(obj)
  check_obj(obj)
  casted_by_attribute.push obj.id
  super(obj)
end

#shiftObject


218
219
220
221
# File 'lib/couchrest/model/associations.rb', line 218

def shift
  casted_by_attribute.shift
  super
end

#unshift(obj) ⇒ Object


201
202
203
204
205
# File 'lib/couchrest/model/associations.rb', line 201

def unshift(obj)
  check_obj(obj)
  casted_by_attribute.unshift obj.id
  super(obj)
end