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:



154
155
156
157
158
159
160
161
162
163
# File 'lib/parse/client/batch.rb', line 154

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:



98
99
100
101
102
103
# File 'lib/parse/model/core/fetching.rb', line 98

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:



84
85
86
87
88
89
90
# File 'lib/parse/model/core/fetching.rb', line 84

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:



204
205
206
# File 'lib/parse/model/pointer.rb', line 204

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.



560
561
562
# File 'lib/parse/model/object.rb', line 560

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 constains the proper fields. Non-convertible objects will be removed. If the className is not contained or known, you can pass a table name as an argument

Parameters:

  • className (String) (defaults to: nil)

    the name of the Parse class if it could not be detected.

Returns:



548
549
550
551
552
553
554
555
556
557
# File 'lib/parse/model/object.rb', line 548

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) && (m[f] || m[:className] || className)
      next Parse::Object.build m, (m[f] || m[:className] || className)
    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:



218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/parse/model/pointer.rb', line 218

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:



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/parse/client/batch.rb', line 187

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:



64
65
66
# File 'lib/parse/model/core/fetching.rb', line 64

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:



74
75
76
# File 'lib/parse/model/core/fetching.rb', line 74

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:



211
212
213
# File 'lib/parse/model/pointer.rb', line 211

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