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

#&, #+, #-, #==, #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

#collection ⇒ Array<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

#<<(*list) ⇒ Object

See Also:



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

def <<(*list)
  add(*list)
  self
end

#add(parse_object) ⇒ Array<Parse::Object> #add(parse_objects) ⇒ Array<Parse::Object> Also known as: push

Add Parse::Objects to the collection. An objectId String is accepted and becomes a pointer of the declared class.

Overloads:

Returns:

Raises:

  • (ArgumentError) —

    if an item is nil, of another Parse class, or cannot be turned into a pointer.



40
41
42
43
44
45
46
# File 'lib/parse/model/associations/pointer_collection_proxy.rb', line 40

def add(*items)
  items = typecast_items(items)
  return @collection if items.empty?
  notify_will_change!
  items.each { |item| collection.push(item) }
  @collection
end

#add!(*items) ⇒ Object

Atomically add a set of Parse::Objects to this collection.



101
102
103
# File 'lib/parse/model/associations/pointer_collection_proxy.rb', line 101

def add!(*items)
  super(*typecast_items(items))
end

#add_unique(*items) ⇒ Array<Parse::Object> Also known as: push_unique

Add items that are not already part of the collection.

Parameters:

Returns:



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

def add_unique(*items)
  items = typecast_items(items)
  return @collection if items.empty?
  notify_will_change!
  @collection = collection | items
  @collection
end

#add_unique!(*items) ⇒ Object

Atomically add a set of Parse::Objects to this collection for those not already in the collection.



109
110
111
# File 'lib/parse/model/associations/pointer_collection_proxy.rb', line 109

def add_unique!(*items)
  super(*typecast_items(items))
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

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

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

post.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.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/parse/model/associations/pointer_collection_proxy.rb', line 146

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

#fetch ⇒ Object

Fetch the set of pointer objects in this collection.



127
128
129
# File 'lib/parse/model/associations/pointer_collection_proxy.rb', line 127

def fetch
  collection.fetch_objects
end

#fetch! ⇒ Object

Force fetch the set of pointer objects in this collection.



121
122
123
# File 'lib/parse/model/associations/pointer_collection_proxy.rb', line 121

def fetch!
  collection.fetch_objects!
end

#remove(parse_object) ⇒ Array<Parse::Object> #remove(parse_objects) ⇒ Array<Parse::Object> Also known as: delete

Removes Parse::Objects from the collection. An objectId String removes the object of the declared class with that id.

Overloads:

Returns:



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

def remove(*items)
  items = typecast_items(items, strict: false)
  return @collection if items.empty?
  notify_will_change!
  items.each { |item| collection.delete item }
  @collection
end

#remove!(*items) ⇒ Object

Atomically remove a set of Parse::Objects to this collection.



115
116
117
# File 'lib/parse/model/associations/pointer_collection_proxy.rb', line 115

def remove!(*items)
  super(*typecast_items(items, strict: false))
end

#replace(items) ⇒ self

Replace the contents of the collection. The items are validated as in #add.

Parameters:

Returns:

  • (self)


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

def replace(items)
  super(typecast_items(Array(items.is_a?(Parse::CollectionProxy) ? items.to_a : items)))
end