Class: Parse::PointerCollectionProxy

Inherits:
CollectionProxy show all
Defined in:
lib/parse/model/associations/pointer_collection_proxy.rb

Overview

A PointerCollectionProxy is a collection proxy that only allows Parse Pointers (Objects) to be part of the collection. This is done by typecasting the collection to a particular Parse class. Ex. An Artist may have several Song objects. Therefore an Artist could have a column :songs, that is an array (collection) of Song (Parse::Object subclass) objects.

Direct Known Subclasses

RelationCollectionProxy

Instance Attribute Summary collapse

Attributes inherited from CollectionProxy

#delegate, #key, #loaded, #parse_class

Instance Method Summary collapse

Methods inherited from CollectionProxy

#&, #+, #-, #<<, #==, #add_unique, #changes_applied!, #clear, #clear_changes!, #count, #destroy!, #each, #empty?, #first, #flatten, #forward, #initialize, #last, #loaded?, #map, #notify_will_change!, #parse_objects, #parse_pointers, #reload!, #reset!, #rollback!, #second, #select, #set_collection!, #to_a, #uniq, #uniq!, #|

Constructor Details

This class inherits a constructor from Parse::CollectionProxy

Instance Attribute Details

#collectionArray<Parse::Object>

Note:

If you modify this directly, it is highly recommended that you call CollectionProxy#notify_will_change! to notify the dirty tracking system.

The internal backing store of the collection.



23
24
25
26
# File 'lib/parse/model/associations/pointer_collection_proxy.rb', line 23

def collection=(c)
  notify_will_change!
  @collection = c
end

Instance Method Details

#add(parse_object) ⇒ Array<Parse::Object> #add(parse_objects) ⇒ Array<Parse::Object>

Add Parse::Objects to the collection.

Overloads:

Returns:



36
37
38
39
40
41
42
# File 'lib/parse/model/associations/pointer_collection_proxy.rb', line 36

def add(*items)
  notify_will_change! if items.count > 0
  items.flatten.parse_objects.each do |item|
    collection.push(item) if item.is_a?(Parse::Pointer)
  end
  @collection
end

#add!(*items) ⇒ Object

Atomically add a set of Parse::Objects to this collection. This is done by making the API request directly with Parse server; the local object is not updated with changes.



65
66
67
# File 'lib/parse/model/associations/pointer_collection_proxy.rb', line 65

def add!(*items)
  super(items.flatten.parse_pointers)
end

#add_unique!(*items) ⇒ Object

Atomically add a set of Parse::Objects to this collection for those not already in the collection. This is done by making the API request directly with Parse server; the local object is not updated with changes.



75
76
77
# File 'lib/parse/model/associations/pointer_collection_proxy.rb', line 75

def add_unique!(*items)
  super(items.flatten.parse_pointers)
end

#as_json(opts = nil) ⇒ Object

Encode the collection as JSON. By default, returns Parse::Pointers for backward compatibility when saving. Set ‘pointers_only: false` to get full hydrated objects for API responses.

Examples:

Default - pointers for storage

capture.assets.as_json
# => [{"__type"=>"Pointer", "className"=>"Asset", "objectId"=>"abc"}, ...]

Full objects for API responses (only fetched fields, no autofetch)

capture.assets.as_json(pointers_only: false)
# => [{"objectId"=>"abc", "file"=>{...}, "caption"=>"...", ...}, ...]

Parameters:

  • opts (Hash) (defaults to: nil)

    options for serialization

Options Hash (opts):

  • :pointers_only (Boolean) — default: true

    When true (default), converts all Parse objects to pointer format. Set to false to serialize full objects.

  • :only_fetched (Boolean) — default: true

    When true (default when pointers_only is false), only serialize fields that were actually fetched. This prevents autofetch from being triggered during serialization of partially hydrated objects.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/parse/model/associations/pointer_collection_proxy.rb', line 114

def as_json(opts = nil)
  opts ||= {}

  # Normalize string keys to symbols to avoid conflicts with defaults
  opts = opts.transform_keys { |k| k.is_a?(String) ? k.to_sym : k }

  # Check if pointers_only was explicitly set, otherwise default to true
  pointers_only = opts.fetch(:pointers_only, true)

  # Default to pointers_only: true for backward compatibility
  # When pointers_only is false, default only_fetched to true to prevent
  # autofetch during serialization of partially hydrated objects
  defaults = { pointers_only: true }
  unless pointers_only
    defaults[:only_fetched] = true unless opts.key?(:only_fetched)
  end
  opts = defaults.merge(opts)
  super(opts)
end

#fetchObject

Fetch the set of pointer objects in this collection.



95
96
97
# File 'lib/parse/model/associations/pointer_collection_proxy.rb', line 95

def fetch
  collection.fetch_objects
end

#fetch!Object

Force fetch the set of pointer objects in this collection.



89
90
91
# File 'lib/parse/model/associations/pointer_collection_proxy.rb', line 89

def fetch!
  collection.fetch_objects!
end

#remove(parse_object) ⇒ Array<Parse::Object> #remove(parse_objects) ⇒ Array<Parse::Object>

Removes Parse::Objects from the collection.

Overloads:

Returns:



52
53
54
55
56
57
58
# File 'lib/parse/model/associations/pointer_collection_proxy.rb', line 52

def remove(*items)
  notify_will_change! if items.count > 0
  items.flatten.parse_objects.each do |item|
    collection.delete item
  end
  @collection
end

#remove!(*items) ⇒ Object

Atomically remove a set of Parse::Objects to this collection. This is done by making the API request directly with Parse server; the local object is not updated with changes.



83
84
85
# File 'lib/parse/model/associations/pointer_collection_proxy.rb', line 83

def remove!(*items)
  super(items.flatten.parse_pointers)
end