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:



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

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:



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

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:



432
433
434
# File 'lib/parse/model/pointer.rb', line 432

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.



2242
2243
2244
# File 'lib/parse/model/object.rb', line 2242

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:



2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
# File 'lib/parse/model/object.rb', line 2219

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 && !Parse::Model.same_parse_class?(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:



446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/parse/model/pointer.rb', line 446

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:



528
529
530
# File 'lib/parse/model/core/fetching.rb', line 528

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:



538
539
540
# File 'lib/parse/model/core/fetching.rb', line 538

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:



439
440
441
# File 'lib/parse/model/pointer.rb', line 439

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