Class: Gcloud::Datastore::Commit
- Inherits:
-
Object
- Object
- Gcloud::Datastore::Commit
- Defined in:
- lib/gcloud/datastore/commit.rb
Overview
# Commit
Object yielded from ‘commit` methods to allow multiple changes to be made in a single commit.
Instance Method Summary collapse
-
#delete(*entities_or_keys) ⇒ Object
Remove entities from the Datastore.
- #entities ⇒ Object
-
#initialize ⇒ Commit
constructor
A new instance of Commit.
-
#insert(*entities) ⇒ Object
Inserts entities to the Datastore.
- #mutations ⇒ Object
-
#save(*entities) ⇒ Object
(also: #upsert)
Saves entities to the Datastore.
-
#update(*entities) ⇒ Object
Updates entities to the Datastore.
Constructor Details
#initialize ⇒ Commit
Returns a new instance of Commit.
37 38 39 40 41 42 |
# File 'lib/gcloud/datastore/commit.rb', line 37 def initialize @shared_upserts = [] @shared_inserts = [] @shared_updates = [] @shared_deletes = [] end |
Instance Method Details
#delete(*entities_or_keys) ⇒ Object
Remove entities from the Datastore.
115 116 117 118 119 120 121 122 |
# File 'lib/gcloud/datastore/commit.rb', line 115 def delete *entities_or_keys keys = Array(entities_or_keys).flatten.map do |e_or_k| e_or_k.respond_to?(:key) ? e_or_k.key : e_or_k end @shared_deletes += keys unless keys.empty? # Do not delete yet true end |
#entities ⇒ Object
143 144 145 |
# File 'lib/gcloud/datastore/commit.rb', line 143 def entities @shared_upserts + @shared_inserts + @shared_updates end |
#insert(*entities) ⇒ Object
Inserts entities to the Datastore.
76 77 78 79 80 81 |
# File 'lib/gcloud/datastore/commit.rb', line 76 def insert *entities entities = Array(entities).flatten @shared_inserts += entities unless entities.empty? # Do not insert yet entities end |
#mutations ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/gcloud/datastore/commit.rb', line 125 def mutations mutations = [] mutations += @shared_upserts.map do |entity| Google::Datastore::V1beta3::Mutation.new upsert: entity.to_grpc end mutations += @shared_inserts.map do |entity| Google::Datastore::V1beta3::Mutation.new insert: entity.to_grpc end mutations += @shared_updates.map do |entity| Google::Datastore::V1beta3::Mutation.new update: entity.to_grpc end mutations += @shared_deletes.map do |key| Google::Datastore::V1beta3::Mutation.new delete: key.to_grpc end mutations end |
#save(*entities) ⇒ Object Also known as: upsert
Saves entities to the Datastore.
56 57 58 59 60 61 |
# File 'lib/gcloud/datastore/commit.rb', line 56 def save *entities entities = Array(entities).flatten @shared_upserts += entities unless entities.empty? # Do not save yet entities end |
#update(*entities) ⇒ Object
Updates entities to the Datastore.
95 96 97 98 99 100 |
# File 'lib/gcloud/datastore/commit.rb', line 95 def update *entities entities = Array(entities).flatten @shared_updates += entities unless entities.empty? # Do not update yet entities end |