Class: Parse::BatchOperation
- Inherits:
-
Object
- Object
- Parse::BatchOperation
- Includes:
- Enumerable
- Defined in:
- lib/parse/client/batch.rb
Overview
This class provides a standard way to submit, manage and process batch operations for Parse::Objects and associations.
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.
songs = Songs.first 1000 #first 1000 songs
songs.each do |song|
# ... modify song ...
end
# will batch save 50 items at a time until all are saved.
songs.save
The objects do not have to be of the same collection in order to be supported in the batch request.
Instance Attribute Summary collapse
-
#requests ⇒ Object
Returns the value of attribute requests.
-
#responses ⇒ Array
The set of responses from this batch.
Instance Method Summary collapse
-
#add(req) ⇒ Object
Add an additional request to this batch.
-
#as_json(*args) ⇒ Hash
A formatted payload for the batch request.
-
#change_requests ⇒ Object
This method is for interoperability with Parse::Object instances.
-
#clear! ⇒ Array
Remove all requests in this batch.
-
#client ⇒ Parse::Client
The client to be used for the request.
-
#count ⇒ Integer
The number of requests in the batch.
- #each ⇒ Array
-
#error? ⇒ Boolean
True if the request had an error.
-
#initialize(reqs = nil) ⇒ BatchOperation
constructor
A new instance of BatchOperation.
-
#submit(segment = 50) ⇒ Array<Parse::Response>
(also: #save)
Submit the batch operation in chunks until they are all complete.
-
#success? ⇒ Boolean
True if the request was successful.
Constructor Details
#initialize(reqs = nil) ⇒ BatchOperation
Returns a new instance of BatchOperation.
54 55 56 57 58 59 |
# File 'lib/parse/client/batch.rb', line 54 def initialize(reqs = nil) @requests = [] @responses = [] reqs = [reqs] unless reqs.is_a?(Enumerable) reqs.each { |r| add(r) } if reqs.is_a?(Enumerable) end |
Instance Attribute Details
#requests ⇒ Object
Returns the value of attribute requests.
|
|
# File 'lib/parse/client/batch.rb', line 40
|
#responses ⇒ Array
Returns the set of responses from this batch.
45 |
# File 'lib/parse/client/batch.rb', line 45 attr_accessor :requests, :responses |
Instance Method Details
#add(req) ⇒ Array<Parse::Request> #add(batch) ⇒ Array<Parse::Request>
Add an additional request to this batch.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/parse/client/batch.rb', line 68 def add(req) if req.respond_to?(:change_requests) requests = req.change_requests.select { |r| r.is_a?(Parse::Request) } @requests += requests elsif req.is_a?(Array) requests = req.select { |r| r.is_a?(Parse::Request) } @requests += requests elsif req.is_a?(BatchOperation) @requests += req.requests if req.is_a?(BatchOperation) else @requests.push(req) if req.is_a?(Parse::Request) end @requests end |
#as_json(*args) ⇒ Hash
Returns a formatted payload for the batch request.
96 97 98 |
# File 'lib/parse/client/batch.rb', line 96 def as_json(*args) { requests: requests }.as_json end |
#change_requests ⇒ Object
This method is for interoperability with Parse::Object instances.
85 86 87 |
# File 'lib/parse/client/batch.rb', line 85 def change_requests @requests end |
#clear! ⇒ Array
Remove all requests in this batch.
107 108 109 |
# File 'lib/parse/client/batch.rb', line 107 def clear! @requests.clear end |
#client ⇒ Parse::Client
Returns the client to be used for the request.
49 50 51 |
# File 'lib/parse/client/batch.rb', line 49 def client @client ||= Parse::Client.client end |
#count ⇒ Integer
Returns the number of requests in the batch.
101 102 103 |
# File 'lib/parse/client/batch.rb', line 101 def count @requests.count end |
#each ⇒ Array
90 91 92 93 |
# File 'lib/parse/client/batch.rb', line 90 def each return enum_for(:each) unless block_given? @requests.each(&Proc.new) end |
#error? ⇒ Boolean
Returns true if the request had an error.
118 119 120 121 |
# File 'lib/parse/client/batch.rb', line 118 def error? return false if @responses.empty? ! success? end |
#submit(segment = 50) ⇒ Array<Parse::Response> Also known as: save
Submit the batch operation in chunks until they are all complete. In general, Parse limits requests in each batch to 50 and it is possible that a Parse::BatchOperation instance contains more than 50 requests. This method will slice up the array of request and send them based on the segment amount until they have all been submitted.
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/parse/client/batch.rb', line 130 def submit(segment = 50) @responses = [] @requests.uniq!(&:signature) @responses = @requests.each_slice(segment).to_a.threaded_map(2) do |slice| client.batch_request( BatchOperation.new(slice) ) end @responses.flatten! #puts "Requests: #{@requests.count} == Response: #{@responses.count}" @requests.zip(@responses).each(&Proc.new) if block_given? @responses end |
#success? ⇒ Boolean
Returns true if the request was successful.
112 113 114 115 |
# File 'lib/parse/client/batch.rb', line 112 def success? return false if @responses.empty? @responses.compact.all?(&:success?) end |