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
Returns the value of attribute delegate.
-
#key ⇒ String
readonly
the name of the property key to use when sending notifications for _will_change! and _fetch!.
-
#loaded ⇒ Object
Returns the value of attribute loaded.
-
#parse_class ⇒ Object
Returns the value of attribute parse_class.
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) ⇒ Object
Atomically adds all items from the array.
-
#add_unique(*items) ⇒ Array
(also: #push_unique)
Add items to the collection if they don’t already exist.
-
#add_unique!(*items) ⇒ Object
Atomically adds all items from the array that are not already part of the collection.
-
#as_json(opts = nil) ⇒ Hash
A JSON representation.
-
#changes_applied! ⇒ Object
mark that collection changes where applied, which clears dirty tracking.
-
#clear ⇒ Object
clear all items in 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 ⇒ 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 ⇒ 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) ⇒ Object
Atomically deletes all items from the array.
-
#reset! ⇒ Object
Reset the state of the collection.
-
#rollback! ⇒ Object
Locally restores previous attributes (not from the persistent store).
-
#second ⇒ Object
The second item in the collection.
-
#select ⇒ 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 ⇒ Object
Alias for Array#uniq.
-
#uniq! ⇒ 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.
61 62 63 64 65 66 67 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 61 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.
47 48 49 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 47 def collection @collection end |
#delegate ⇒ Object
Returns the value of attribute delegate.
|
|
# File 'lib/parse/model/associations/collection_proxy.rb', line 32
|
#key ⇒ String (readonly)
the name of the property key to use when sending notifications for _will_change! and _fetch!
47 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 47 attr_accessor :collection, :delegate, :loaded, :parse_class |
#loaded ⇒ Object
Returns the value of attribute loaded.
|
|
# File 'lib/parse/model/associations/collection_proxy.rb', line 36
|
#parse_class ⇒ Object
Returns the value of attribute parse_class.
|
|
# File 'lib/parse/model/associations/collection_proxy.rb', line 39
|
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#&
184 185 186 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 184 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.
207 208 209 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 207 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.
196 197 198 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 196 def -(other_ary) collection - [other_ary].flatten end |
#<<(*list) ⇒ Object
Append items to the collection
314 315 316 317 318 319 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 314 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.
90 91 92 93 94 95 96 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 90 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
143 144 145 146 147 148 149 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 143 def add(*items) notify_will_change! if items.count > 0 items.each do |item| collection.push item end @collection end |
#add!(*items) ⇒ Object
Atomically adds all items from the array. This request is sent directly to the Parse backend.
231 232 233 234 235 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 231 def add!(*items) return false unless @delegate.respond_to?(:op_add!) @delegate.send :op_add!, @key, items.flatten reset! end |
#add_unique(*items) ⇒ Array Also known as: push_unique
Add items to the collection if they don’t already exist
154 155 156 157 158 159 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 154 def add_unique(*items) return unless items.count > 0 notify_will_change! @collection = collection | items.flatten @collection end |
#add_unique!(*items) ⇒ Object
Atomically adds all items from the array that are not already part of the collection. This request is sent directly to the Parse backend.
241 242 243 244 245 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 241 def add_unique!(*items) return false unless @delegate.respond_to?(:op_add_unique!) @delegate.send :op_add_unique!, @key, items.flatten reset! end |
#as_json(opts = nil) ⇒ Hash
Returns a JSON representation.
304 305 306 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 304 def as_json(opts = nil) collection.as_json(opts) end |
#changes_applied! ⇒ Object
mark that collection changes where applied, which clears dirty tracking.
277 278 279 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 277 def changes_applied! changes_applied end |
#clear ⇒ Object
clear all items in the collection
105 106 107 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 105 def clear @collection.clear end |
#clear_changes! ⇒ Object
clears all dirty tracked information.
272 273 274 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 272 def clear_changes! clear_changes_information end |
#count ⇒ Integer
Returns number of items in the collection.
299 300 301 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 299 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.
258 259 260 261 262 263 264 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 258 def destroy! return false unless @delegate.respond_to?(:op_destroy!) @delegate.send :op_destroy!, @key collection_will_change! @collection.clear reset! end |
#each ⇒ Object
Alias for Array#each
328 329 330 331 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 328 def each return collection.enum_for(:each) unless block_given? collection.each &Proc.new end |
#empty? ⇒ Boolean
true if the collection is empty.
309 310 311 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 309 def empty? collection.empty? end |
#first(*args) ⇒ Object
Returns the first item in the collection.
283 284 285 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 283 def first(*args) collection.first(*args) end |
#flatten ⇒ Array
Alias Array flattening.
213 214 215 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 213 def flatten collection.flatten end |
#forward(method, params = nil) ⇒ Object
Forward a method call to the delegate.
78 79 80 81 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 78 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.
294 295 296 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 294 def last(*args) collection.last(*args) end |
#loaded? ⇒ Boolean
true if the collection has been loaded
70 71 72 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 70 def loaded? @loaded end |
#map ⇒ Object
Alias for Array#map
334 335 336 337 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 334 def map return collection.enum_for(:map) unless block_given? collection.map &Proc.new end |
#notify_will_change! ⇒ Object
Notifies the delegate that the collection changed.
322 323 324 325 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 322 def notify_will_change! collection_will_change! forward "#{@key}_will_change!" end |
#parse_objects ⇒ Array<Parse::Object>
Alias to to_a.parse_objects from Array#parse_objects
365 366 367 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 365 def parse_objects collection.to_a.parse_objects end |
#parse_pointers ⇒ Array<Parse::Pointer>
Alias to to_a.parse_pointers from Array#parse_pointers
371 372 373 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 371 def parse_pointers collection.to_a.parse_pointers end |
#reload! ⇒ Object
Reload and restore the collection to its original set of items.
99 100 101 102 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 99 def reload! reset! collection #force reload end |
#remove(*items) ⇒ Object Also known as: delete
Remove items from the collection
219 220 221 222 223 224 225 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 219 def remove(*items) notify_will_change! if items.count > 0 items.each do |item| collection.delete item end @collection end |
#remove!(*items) ⇒ Object
Atomically deletes all items from the array. This request is sent directly to the Parse backend.
250 251 252 253 254 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 250 def remove!(*items) return false unless @delegate.respond_to?(:op_remove!) @delegate.send :op_remove!, @key, items.flatten reset! end |
#reset! ⇒ Object
Reset the state of the collection.
84 85 86 87 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 84 def reset! @loaded = false clear end |
#rollback! ⇒ Object
Locally restores previous attributes (not from the persistent store)
267 268 269 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 267 def rollback! restore_attributes end |
#second ⇒ Object
Returns the second item in the collection.
288 289 290 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 288 def second collection.second end |
#select ⇒ Object
Alias for Array#select
340 341 342 343 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 340 def select return collection.enum_for(:select) unless block_given? collection.select &Proc.new end |
#set_collection!(list) ⇒ Array
Set the internal collection of items without dirty tracking or change notifications.
117 118 119 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 117 def set_collection!(list) @collection = list end |
#to_a ⇒ Array Also known as: to_ary
110 111 112 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 110 def to_a collection.to_a end |
#uniq ⇒ Object
Alias for Array#uniq
346 347 348 349 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 346 def uniq return collection.uniq(&Proc.new) if block_given? return collection.uniq end |
#uniq! ⇒ Object
Alias for Array#uniq!
352 353 354 355 356 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 352 def uniq! notify_will_change! return collection.uniq!(&Proc.new) 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#|
170 171 172 |
# File 'lib/parse/model/associations/collection_proxy.rb', line 170 def |(items) collection | [items].flatten end |