Class: Parse::CollectionProxy
- Inherits:
-
Object
- Object
- Parse::CollectionProxy
- Includes:
- ActiveModel::Dirty, ActiveModel::Model, Enumerable
- Defined in:
- lib/parse/model/associations/collection_proxy.rb
Overview
A CollectionProxy is a special type of array wrapper that notifies a delegate object about changes to the array in order to perform dirty tracking. This is used for all Array properties in Parse::Objects. Subclasses of CollectionProxy are also available for supporting different association types such as an array of Parse pointers and Parse relations.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#collection ⇒ Array
Contents of the collection.
-
#delegate ⇒ Object
readonly
The object to be notified of changes to the collection.
-
#key ⇒ String
readonly
the name of the property key to use when sending notifications for _will_change! and _fetch!.
-
#loaded ⇒ Object
The writer for :collection is defined explicitly below with dirty-tracking notification; do not declare attr_writer :collection here.
-
#parse_class ⇒ Object
The writer for :collection is defined explicitly below with dirty-tracking notification; do not declare attr_writer :collection here.
Instance Method Summary collapse
-
#&(other_ary) ⇒ Array
Set Intersection - Returns a new array containing unique elements common to the two arrays.
-
#+(other_ary) ⇒ Array
Alias Array Concatenation.
-
#-(other_ary) ⇒ Array
Alias Array Difference.
-
#<<(*list) ⇒ Object
Append items to the collection.
-
#==(other_list) ⇒ Boolean
True if two collection proxies have similar items.
-
#add(*items) ⇒ Object
(also: #push)
Add items to the collection.
-
#add!(*items) ⇒ Boolean
Atomically adds all items to the array field.
-
#add_unique(*items) ⇒ Array
(also: #push_unique)
Add items to the collection if they don't already exist.
-
#add_unique!(*items) ⇒ Boolean
Atomically adds the items that are not already part of the array field.
-
#as_json(opts = nil) ⇒ Array
A JSON representation.
-
#changes_applied! ⇒ Object
mark that collection changes where applied, which clears dirty tracking.
-
#clear ⇒ Array
Remove all items from the collection.
-
#clear_changes! ⇒ Object
clears all dirty tracked information.
-
#count ⇒ Integer
Number of items in the collection.
-
#destroy! ⇒ Object
Atomically deletes all items in the array, and marks the field as
undefineddirectly with the Parse server. -
#each(&block) ⇒ Object
Alias for Array#each.
-
#empty? ⇒ Boolean
true if the collection is empty.
-
#first(*args) ⇒ Object
The first item in the collection.
-
#flatten ⇒ Array
Alias Array flattening.
-
#forward(method, params = nil) ⇒ Object
Forward a method call to the delegate.
-
#initialize(collection = nil, delegate: nil, key: nil, parse_class: nil) ⇒ CollectionProxy
constructor
Create a new CollectionProxy instance.
-
#last(*args) ⇒ Object
The last item in the collection.
-
#loaded? ⇒ Boolean
true if the collection has been loaded.
-
#map(&block) ⇒ Object
Alias for Array#map.
-
#notify_will_change! ⇒ Object
Notifies the delegate that the collection changed.
-
#parse_objects ⇒ Array<Parse::Object>
Alias to
to_a.parse_objectsfrom Array#parse_objects. -
#parse_pointers ⇒ Array<Parse::Pointer>
Alias to
to_a.parse_pointersfrom Array#parse_pointers. -
#reload! ⇒ Object
Reload and restore the collection to its original set of items.
-
#remove(*items) ⇒ Object
(also: #delete)
Remove items from the collection.
-
#remove!(*items) ⇒ Boolean
Atomically removes the items from the array field.
-
#replace(items) ⇒ self
Replace the contents of the collection with a new set of items, as Array#replace does.
-
#reset! ⇒ Object
Reset the state of the collection.
-
#rollback! ⇒ Object
Locally restores previous attributes (not from the persistent store) and clears the dirty tracking of the collection.
-
#second ⇒ Object
The second item in the collection.
-
#select(&block) ⇒ Object
Alias for Array#select.
-
#set_collection!(list) ⇒ Array
Set the internal collection of items without dirty tracking or change notifications.
- #to_a ⇒ Array (also: #to_ary)
-
#uniq(&block) ⇒ Object
Alias for Array#uniq.
-
#uniq!(&block) ⇒ Object
Alias for Array#uniq!.
-
#|(items) ⇒ Array
Set Union - Returns a new array by joining two arrays, excluding any duplicates and preserving the order from the original array.
Constructor Details
#initialize(collection = nil, delegate: nil, key: nil, parse_class: nil) ⇒ CollectionProxy
Create a new CollectionProxy instance.
62 63 64 65 66 67 68 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 62 def initialize(collection = nil, delegate: nil, key: nil, parse_class: nil) @delegate = delegate @key = key.to_sym if key.present? @collection = collection.is_a?(Array) ? collection : [] @loaded = @collection.count > 0 @parse_class = parse_class end |
Instance Attribute Details
#collection ⇒ Array
If you modify this directly, it is highly recommended that you call #notify_will_change! to notify the dirty tracking system.
Returns contents of the collection.
167 168 169 170 171 172 173 174 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 167 def collection if @collection.empty? && @loaded == false @collection = forward(:"#{@key}_fetch!") || @collection || [] @loaded = true end @collection end |
#delegate ⇒ Object (readonly)
The object to be notified of changes to the collection.
|
|
# File 'lib/parse/model/associations/collection_proxy.rb', line 31
|
#key ⇒ String (readonly)
the name of the property key to use when sending notifications for _will_change! and _fetch!
|
|
# File 'lib/parse/model/associations/collection_proxy.rb', line 42
|
#loaded ⇒ Object
The writer for :collection is defined explicitly below with dirty-tracking notification; do not declare attr_writer :collection here.
|
|
# File 'lib/parse/model/associations/collection_proxy.rb', line 35
|
#parse_class ⇒ Object
The writer for :collection is defined explicitly below with dirty-tracking notification; do not declare attr_writer :collection here.
|
|
# File 'lib/parse/model/associations/collection_proxy.rb', line 38
|
Instance Method Details
#&(other_ary) ⇒ Array
Set Intersection - Returns a new array containing unique elements common to the two arrays. The order is preserved from the original array.
It compares elements using their hash and eql? methods for efficiency. See Array#&
228 229 230 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 228 def &(other_ary) collection & [other_ary].flatten end |
#+(other_ary) ⇒ Array
Alias Array Concatenation. Returns a new array built by concatenating the two arrays together to produce a third array.
251 252 253 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 251 def +(other_ary) collection + [other_ary].flatten.to_a end |
#-(other_ary) ⇒ Array
Alias Array Difference. Returns a new array that is a copy of the original array, removing any items that also appear in other_ary. The order is preserved from the original array.
240 241 242 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 240 def -(other_ary) collection - [other_ary].flatten end |
#<<(*list) ⇒ Object
Append items to the collection
421 422 423 424 425 426 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 421 def <<(*list) if list.count > 0 notify_will_change! list.flatten.each { |e| collection.push(e) } end end |
#==(other_list) ⇒ Boolean
Returns true if two collection proxies have similar items.
112 113 114 115 116 117 118 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 112 def ==(other_list) if other_list.is_a?(Array) return @collection == other_list elsif other_list.is_a?(Parse::CollectionProxy) return @collection == other_list.instance_variable_get(:@collection) end end |
#add(*items) ⇒ Object Also known as: push
Add items to the collection
183 184 185 186 187 188 189 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 183 def add(*items) notify_will_change! if items.count > 0 items.each do |item| collection.push item end @collection end |
#add!(*items) ⇒ Boolean
Parse objects are automatically converted to pointer format
Atomically adds all items to the array field. The request is sent directly to the Parse backend. On success the local collection takes the array the server returned (or appends the items when the reply does not include the field) without marking the field as changed, so a later save does not overwrite the server array. On an owner that has not been saved yet there is nothing to update on the server, so the items are added locally as a normal change and sent with the next save.
285 286 287 288 289 290 291 292 293 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 285 def add!(*items) items = items.flatten return false unless @delegate.respond_to?(:op_add!) return true if items.empty? return add(*items) && true if unsaved_delegate? apply_atomic_op(:op_add!, items) do |list, objs| list + objs end end |
#add_unique(*items) ⇒ Array Also known as: push_unique
Add items to the collection if they don't already exist
196 197 198 199 200 201 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 196 def add_unique(*items) return unless items.count > 0 notify_will_change! @collection = collection | items.flatten @collection end |
#add_unique!(*items) ⇒ Boolean
Parse objects are automatically converted to pointer format
Atomically adds the items that are not already part of the array field. The request is sent directly to the Parse backend. The local collection is updated as in #add!.
302 303 304 305 306 307 308 309 310 311 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 302 def add_unique!(*items) items = items.flatten return false unless @delegate.respond_to?(:op_add_unique!) return true if items.empty? return add_unique(*items) && true if unsaved_delegate? apply_atomic_op(:op_add_unique!, items) do |list, objs| objs.each { |o| list << o unless list.include?(o) } list end end |
#as_json(opts = nil) ⇒ Array
Returns a JSON representation.
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 394 def as_json(opts = nil) opts ||= {} pointers_only = opts.delete(:pointers_only) || opts.delete("pointers_only") collection.map do |item| if pointers_only && item.respond_to?(:pointer) # Convert Parse objects/pointers to pointer format for storage ptr = item.pointer { Parse::Model::TYPE_FIELD => Parse::Model::TYPE_POINTER, Parse::Model::KEY_CLASS_NAME => ptr.parse_class, Parse::Model::OBJECT_ID => ptr.id, } elsif item.respond_to?(:as_json) item.as_json(opts) else item end end end |
#changes_applied! ⇒ Object
mark that collection changes where applied, which clears dirty tracking.
357 358 359 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 357 def changes_applied! changes_applied end |
#clear ⇒ Array
Remove all items from the collection. The field is marked as changed, so the next save stores the empty array.
129 130 131 132 133 134 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 129 def clear notify_will_change! @collection.clear @loaded = true @collection end |
#clear_changes! ⇒ Object
clears all dirty tracked information.
352 353 354 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 352 def clear_changes! clear_changes_information end |
#count ⇒ Integer
Returns number of items in the collection.
379 380 381 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 379 def count collection.count end |
#destroy! ⇒ Object
Atomically deletes all items in the array, and marks the field as undefined directly
with the Parse server. This request is sent directly to the Parse backend.
331 332 333 334 335 336 337 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 331 def destroy! return false unless @delegate.respond_to?(:op_destroy!) @delegate.send :op_destroy!, @key collection_will_change! @collection.clear reset! end |
#each(&block) ⇒ Object
Alias for Array#each
438 439 440 441 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 438 def each(&block) return collection.enum_for(:each) unless block_given? collection.each(&block) end |
#empty? ⇒ Boolean
true if the collection is empty.
416 417 418 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 416 def empty? collection.empty? end |
#first(*args) ⇒ Object
Returns the first item in the collection.
363 364 365 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 363 def first(*args) collection.first(*args) end |
#flatten ⇒ Array
Alias Array flattening.
257 258 259 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 257 def flatten collection.flatten end |
#forward(method, params = nil) ⇒ Object
Forward a method call to the delegate.
98 99 100 101 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 98 def forward(method, params = nil) return unless @delegate && @delegate.respond_to?(method) params.nil? ? @delegate.send(method) : @delegate.send(method, params) end |
#last(*args) ⇒ Object
Returns the last item in the collection.
374 375 376 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 374 def last(*args) collection.last(*args) end |
#loaded? ⇒ Boolean
true if the collection has been loaded
90 91 92 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 90 def loaded? @loaded end |
#map(&block) ⇒ Object
Alias for Array#map
444 445 446 447 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 444 def map(&block) return collection.enum_for(:map) unless block_given? collection.map(&block) end |
#notify_will_change! ⇒ Object
Notifies the delegate that the collection changed.
429 430 431 432 433 434 435 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 429 def notify_will_change! if @delegate && @delegate.respond_to?(:_capture_transaction_state!, true) @delegate.send(:_capture_transaction_state!) end collection_will_change! forward "#{@key}_will_change!" end |
#parse_objects ⇒ Array<Parse::Object>
Alias to to_a.parse_objects from Array#parse_objects
475 476 477 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 475 def parse_objects collection.to_a.parse_objects end |
#parse_pointers ⇒ Array<Parse::Pointer>
Alias to to_a.parse_pointers from Array#parse_pointers
481 482 483 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 481 def parse_pointers collection.to_a.parse_pointers end |
#reload! ⇒ Object
Reload and restore the collection to its original set of items.
121 122 123 124 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 121 def reload! reset! collection #force reload end |
#remove(*items) ⇒ Object Also known as: delete
Remove items from the collection
263 264 265 266 267 268 269 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 263 def remove(*items) notify_will_change! if items.count > 0 items.each do |item| collection.delete item end @collection end |
#remove!(*items) ⇒ Boolean
Parse objects are automatically converted to pointer format
Atomically removes the items from the array field. The request is sent directly to the Parse backend. The local collection is updated as in #add!.
319 320 321 322 323 324 325 326 327 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 319 def remove!(*items) items = items.flatten return false unless @delegate.respond_to?(:op_remove!) return true if items.empty? return remove(*items) && true if unsaved_delegate? apply_atomic_op(:op_remove!, items) do |list, objs| list.reject { |x| objs.include?(x) } end end |
#replace(items) ⇒ self
Replace the contents of the collection with a new set of items, as Array#replace does. The field is marked as changed.
140 141 142 143 144 145 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 140 def replace(items) notify_will_change! @collection = Array(items.is_a?(Parse::CollectionProxy) ? items.to_a : items).dup @loaded = true self end |
#reset! ⇒ Object
Reset the state of the collection. The items are dropped and the collection is marked as not loaded. This is not a change to the field, so it is not dirty tracked; use #clear to empty the field.
106 107 108 109 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 106 def reset! @loaded = false @collection.clear end |
#rollback! ⇒ Object
Locally restores previous attributes (not from the persistent store) and clears the dirty tracking of the collection. The values are restored directly: going through the public writers would report a new change to the owner while undoing one.
343 344 345 346 347 348 349 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 343 def rollback! changed.each do |attr| instance_variable_set(:"@#{attr}", attribute_was(attr)) end clear_changes_information @collection end |
#second ⇒ Object
Returns the second item in the collection.
368 369 370 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 368 def second collection.second end |
#select(&block) ⇒ Object
Alias for Array#select
450 451 452 453 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 450 def select(&block) return collection.enum_for(:select) unless block_given? collection.select(&block) end |
#set_collection!(list) ⇒ Array
Set the internal collection of items without dirty tracking or change notifications.
157 158 159 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 157 def set_collection!(list) @collection = list end |
#to_a ⇒ Array Also known as: to_ary
148 149 150 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 148 def to_a collection.to_a end |
#uniq(&block) ⇒ Object
Alias for Array#uniq
456 457 458 459 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 456 def uniq(&block) return collection.uniq(&block) if block_given? return collection.uniq end |
#uniq!(&block) ⇒ Object
Alias for Array#uniq!
462 463 464 465 466 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 462 def uniq!(&block) notify_will_change! return collection.uniq!(&block) if block_given? return collection.uniq! end |
#|(items) ⇒ Array
Set Union - Returns a new array by joining two arrays, excluding any duplicates and preserving the order from the original array. It compares elements using their hash and eql? methods for efficiency. See Array#|
214 215 216 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 214 def |(items) collection | [items].flatten end |