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

See Also:



157
158
159
160
161
162
163
164
165
166
# File 'lib/parse/client/batch.rb', line 157

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.

See Also:



102
103
104
105
106
107
# File 'lib/parse/model/core/fetching.rb', line 102

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.

See Also:



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

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.



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

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

#parse_idsArray<String>



565
566
567
# File 'lib/parse/model/object.rb', line 565

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



553
554
555
556
557
558
559
560
561
562
# File 'lib/parse/model/object.rb', line 553

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.



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

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

See Also:



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

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.

Yields:

  • the block for the each iteration.

See Also:



68
69
70
# File 'lib/parse/model/core/fetching.rb', line 68

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.

Yields:

  • the block for the map iteration.

See Also:



78
79
80
# File 'lib/parse/model/core/fetching.rb', line 78

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.



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

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