Class: Array
- Inherits:
-
Object
- Object
- Array
- 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
-
#destroy(session: nil) ⇒ Parse::BatchOperation
Submit a batch request for deleting a set of Parse::Objects.
-
#fetch_objects(lookup = :parallel) ⇒ Array<Parse::Object>
Fetches all the objects in the array that are in Pointer state.
-
#fetch_objects!(lookup = :parallel) ⇒ Array<Parse::Object>
Fetches all the objects in the array even if they are not in a Pointer state.
-
#objectIds ⇒ Array<String>
This method maps all the ids (String) of all Parse::Objects in the array.
-
#parse_ids ⇒ Array<String>
An array of objectIds for all objects that are Parse::Objects.
-
#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.
-
#parse_pointers(table = nil) ⇒ Array<Parse::Pointer>
Convert all potential objects in the array to a list of Parse::Pointer instances.
-
#save(merge: true, force: false, session: nil) ⇒ Parse::BatchOperation
Submit a batch request for saving a set of Parse::Objects.
-
#threaded_each(threads = 2) { ... } ⇒ self
Perform a threaded each iteration on a set of array items.
-
#threaded_map(threads = 2) { ... } ⇒ Array
Perform a threaded map operation on a set of array items.
-
#valid_parse_objects ⇒ Array<Parse::Object,Parse::Pointer>
Filter all objects in the array that do not inherit from Parse::Pointer or Parse::Object.
Instance Method Details
#destroy(session: nil) ⇒ Parse::BatchOperation
Submit a batch request for deleting a set of Parse::Objects.
Each object whose delete succeeds has its local state updated the same way Parse::Core::Actions#destroy updates it. Objects whose delete fails are left untouched; inspect the returned batch's responses to find them. Destroy callbacks are not run. Every Parse::Session and Parse::User whose delete succeeded, or reported "object not found" (the row is already gone, for example revoked elsewhere), is dropped from its client's identity plane, as their single-object destroy does.
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
# File 'lib/parse/client/batch.rb', line 475 def destroy(session: nil) token = Parse::BatchOperation.session_token_for!(session) targets = select { |o| o.respond_to?(:destroy_request) } if targets.empty? && !empty? raise ArgumentError, "Array#destroy requires Parse::Object elements; " \ "this array holds none (#{first.class})" end # A session deleted from a pointer or a partial fetch carries no token or # owner to drop from the identity cache; look them up first, in one # query per client. Parse::Session.send(:_preload_identity_for_destroy!, targets, session_token: token) if defined?(Parse::Session) _destroy_batch(targets, token) ensure # A looked-up session token is a live credential; never leave it on an # object whose delete was skipped or whose batch raised. targets&.each { |o| o.send(:_clear_identity_for_destroy!) if _batch_identity_hook?(o, :_clear_identity_for_destroy!) } end |
#fetch_objects(lookup = :parallel) ⇒ Array<Parse::Object>
Fetches all the objects in the array that are in Pointer state.
611 612 613 614 615 616 |
# File 'lib/parse/model/core/fetching.rb', line 611 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.
597 598 599 600 601 602 603 |
# File 'lib/parse/model/core/fetching.rb', line 597 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 |
#objectIds ⇒ Array<String>
This method maps all the ids (String) of all Parse::Objects in the array.
466 467 468 |
# File 'lib/parse/model/pointer.rb', line 466 def objectIds map { |m| m.is_a?(Parse::Pointer) ? m.id : nil }.compact end |
#parse_ids ⇒ Array<String>
Returns an array of objectIds for all objects that are Parse::Objects.
2544 2545 2546 |
# File 'lib/parse/model/object.rb', line 2544 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.
2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 |
# File 'lib/parse/model/object.rb', line 2521 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.
480 481 482 483 484 485 486 487 488 489 490 491 492 |
# File 'lib/parse/model/pointer.rb', line 480 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, session: nil) ⇒ Parse::BatchOperation
The objects of the array to be saved do not all have to be of the same collection.
Each write is sent with the credentials of its object's class client, as a single Parse::Core::Actions#save is. Objects bound to different clients are saved in separate batch calls.
Submit a batch request for saving 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.
Each object is updated only from its own responses. An object whose requests all succeed gets its id and timestamps and has its changes cleared. When an object has several requests (an attribute update plus relation updates) and only some succeed, the fields written by the successful requests are cleared and the rest stay dirty, so a later save resends only what failed. An object listed more than once is saved once. Elements that are not Parse objects are skipped.
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 |
# File 'lib/parse/client/batch.rb', line 569 def save(merge: true, force: false, session: nil) token = Parse::BatchOperation.session_token_for!(session) targets = select { |o| o.is_a?(Parse::Object) } if targets.empty? && !empty? raise ArgumentError, "Array#save requires Parse::Object elements; " \ "this array holds none (#{first.class})" end batch = Parse::BatchOperation.new objects = {} targets.each do |o| next if objects.key?(o.object_id) objects[o.object_id] = o reqs = o.change_requests(force) reqs.each { |r| r.opts[:session_token] = token } if token batch.add reqs end if merge == false batch.submit return batch end outcomes = Hash.new { |h, k| h[k] = [] } begin batch.submit do |request, response| outcomes[request.tag] << [request, response] if objects.key?(request.tag) end ensure # Apply what landed even when a chunk raised, so a successful create is # never left looking new (which would create it again on the next save). outcomes.each do |tag, pairs| Parse::BatchOperation.apply_save_outcome(objects[tag], pairs) end end batch end |
#threaded_each(threads = 2) { ... } ⇒ self
Perform a threaded each iteration on a set of array items.
577 578 579 |
# File 'lib/parse/model/core/fetching.rb', line 577 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.
587 588 589 |
# File 'lib/parse/model/core/fetching.rb', line 587 def threaded_map(threads = 2, &block) Parallel.map(self, { in_threads: threads }, &block) end |
#valid_parse_objects ⇒ Array<Parse::Object,Parse::Pointer>
Filter all objects in the array that do not inherit from Parse::Pointer or Parse::Object.
473 474 475 |
# File 'lib/parse/model/pointer.rb', line 473 def valid_parse_objects select { |s| s.is_a?(Parse::Pointer) } end |