Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/parse/model/pointer.rb,
lib/parse/client/batch.rb,
lib/parse/model/object.rb,
lib/parse/model/core/fetching.rb

Overview

extensions

Instance Method Summary collapse

Instance Method Details

#destroyParse::BatchOperation

Submit a batch request for deleting a set of Parse::Objects.

Examples:

# assume Post and Author are Parse models
author = Author.first
posts = Post.all author: author
posts.destroy # batch destroy request

Returns:

See Also:



177
178
179
180
181
182
183
184
185
186
# File 'lib/parse/client/batch.rb', line 177

def destroy
  batch = Parse::BatchOperation.new
  each do |o|
    next unless o.respond_to?(:destroy_request)
    r = o.destroy_request
    batch.add(r) unless r.nil?
  end
  batch.submit
  batch
end

#fetch_objects(lookup = :parallel) ⇒ Array<Parse::Object>

Fetches all the objects in the array that are in Pointer state.

Parameters:

  • lookup (Symbol) (defaults to: :parallel)

    The methodology to use for HTTP requests. Use :parallel to fetch all objects in parallel HTTP requests. Set to anything else to perform requests serially.

Returns:

See Also:



560
561
562
563
564
565
# File 'lib/parse/model/core/fetching.rb', line 560

def fetch_objects(lookup = :parallel)
  items = valid_parse_objects
  lookup == :parallel ? items.threaded_each(2, &:fetch) : items.each(&:fetch)
  #self.replace items
  self
end

#fetch_objects!(lookup = :parallel) ⇒ Array<Parse::Object>

Fetches all the objects in the array even if they are not in a Pointer state.

Parameters:

  • lookup (Symbol) (defaults to: :parallel)

    The methodology to use for HTTP requests. Use :parallel to fetch all objects in parallel HTTP requests. Set to anything else to perform requests serially.

Returns:

See Also:



546
547
548
549
550
551
552
# File 'lib/parse/model/core/fetching.rb', line 546

def fetch_objects!(lookup = :parallel)
  # this gets all valid parse objects from the array
  items = valid_parse_objects
  lookup == :parallel ? items.threaded_each(2, &:fetch!) : items.each(&:fetch!)
  #self.replace items
  self #return for chaining.
end

#objectIdsArray<String>

This method maps all the ids (String) of all Parse::Objects in the array.

Returns:



416
417
418
# File 'lib/parse/model/pointer.rb', line 416

def objectIds
  map { |m| m.is_a?(Parse::Pointer) ? m.id : nil }.compact
end

#parse_idsArray<String>

Returns an array of objectIds for all objects that are Parse::Objects.

Returns:

  • (Array<String>)

    an array of objectIds for all objects that are Parse::Objects.



2054
2055
2056
# File 'lib/parse/model/object.rb', line 2054

def parse_ids
  parse_objects.map(&:id)
end

#parse_objects(className = nil) ⇒ Array<Parse::Object>

This helper method selects or converts all objects in an array that are either inherit from Parse::Pointer or are a JSON Parse hash. If it is a hash, a Pare::Object will be built from it if it constrains the proper fields. Non-convertible objects will be removed.

When className is provided by the caller, it is treated as authoritative — incoming hash className values are ignored. This blocks attacker- controlled type confusion when this helper is invoked from typed associations (has_many, belongs_to) that already know the expected class.

When className is nil (caller is doing untyped array conversion), the helper falls back to the hash-supplied className for compatibility with raw JSON deserialization callers.

Parameters:

  • className (String, nil) (defaults to: nil)

    the authoritative Parse class name.

Returns:



2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
# File 'lib/parse/model/object.rb', line 2031

def parse_objects(className = nil)
  f = Parse::Model::KEY_CLASS_NAME
  map do |m|
    next m if m.is_a?(Parse::Pointer)
    if m.is_a?(Hash)
      resolved = if className
                   # Caller knows the type; warn on mismatch but always
                   # use the declared className.
                   incoming = m[f] || m[:className]
                   if incoming && incoming != className
                     warn "[Parse::Array#parse_objects] expected className=#{className.inspect}, ignoring incoming className=#{incoming.inspect}"
                   end
                   className
                 else
                   m[f] || m[:className]
                 end
      next Parse::Object.build(m, resolved) if resolved
    end
    nil
  end.compact
end

#parse_pointers(table = nil) ⇒ Array<Parse::Pointer>

Convert all potential objects in the array to a list of Parse::Pointer instances. The array can contain a mixture of objects types including JSON Parse-like hashes.

Returns:



430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/parse/model/pointer.rb', line 430

def parse_pointers(table = nil)
  self.map do |m|
    #if its an exact Parse::Pointer
    if m.is_a?(Parse::Pointer) || m.respond_to?(:pointer)
      next m.pointer
    elsif m.is_a?(Hash) && m[Parse::Model::KEY_CLASS_NAME] && m[Parse::Model::OBJECT_ID]
      next Parse::Pointer.new m[Parse::Model::KEY_CLASS_NAME], m[Parse::Model::OBJECT_ID]
    elsif m.is_a?(Hash) && m[:className] && m[:objectId]
      next Parse::Pointer.new m[:className], m[:objectId]
    end
    nil
  end.compact
end

#save(merge: true, force: false) ⇒ Parse::BatchOperation

Note:

The objects of the array to be saved do not all have to be of the same collection.

Submit a batch request for deleting a set of Parse::Objects. Batch requests are supported implicitly and intelligently through an extension of array. When an array of Parse::Object subclasses is saved, Parse-Stack will batch all possible save operations for the objects in the array that have changed. It will also batch save 50 at a time until all items in the array are saved. Note: Parse does not allow batch saving Parse::User objects.

Examples:

# assume Post and Author are Parse models
author = Author.first
posts = Post.first 100
posts.each { |post| post.author = author }
posts.save # batch save

Parameters:

  • merge (Boolean) (defaults to: true)

    whether to merge the updated changes to the series of objects back to the original ones submitted. If you don’t need the original objects to be updated with the changes, set this to false for improved performance.

  • force (Boolean) (defaults to: false)

    Do not skip objects that do not have pending changes (dirty tracking).

Returns:

See Also:



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/parse/client/batch.rb', line 210

def save(merge: true, force: false)
  batch = Parse::BatchOperation.new
  objects = {}
  each do |o|
    next unless o.is_a?(Parse::Object)
    objects[o.object_id] = o
    batch.add o.change_requests(force)
  end
  if merge == false
    batch.submit
    return batch
  end
  #rebind updates
  batch.submit do |request, response|
    next unless request.tag.present? && response.present? && response.success?
    o = objects[request.tag]
    next unless o.is_a?(Parse::Object)
    result = response.result
    o.id = result["objectId"] if o.id.blank?
    o.set_attributes!(result)
    o.clear_changes!
  end
  batch
end

#threaded_each(threads = 2) { ... } ⇒ self

Perform a threaded each iteration on a set of array items.

Parameters:

  • threads (Integer) (defaults to: 2)

    the maximum number of threads to spawn/

Yields:

  • the block for the each iteration.

Returns:

  • (self)

See Also:



526
527
528
# File 'lib/parse/model/core/fetching.rb', line 526

def threaded_each(threads = 2, &block)
  Parallel.each(self, { in_threads: threads }, &block)
end

#threaded_map(threads = 2) { ... } ⇒ Array

Perform a threaded map operation on a set of array items.

Parameters:

  • threads (Integer) (defaults to: 2)

    the maximum number of threads to spawn

Yields:

  • the block for the map iteration.

Returns:

  • (Array)

    the resultant array from the map.

See Also:



536
537
538
# File 'lib/parse/model/core/fetching.rb', line 536

def threaded_map(threads = 2, &block)
  Parallel.map(self, { in_threads: threads }, &block)
end

#valid_parse_objectsArray<Parse::Object,Parse::Pointer>

Filter all objects in the array that do not inherit from Parse::Pointer or Parse::Object.

Returns:



423
424
425
# File 'lib/parse/model/pointer.rb', line 423

def valid_parse_objects
  select { |s| s.is_a?(Parse::Pointer) }
end