Class: Gcloud::Datastore::Key
- Inherits:
-
Object
- Object
- Gcloud::Datastore::Key
- Defined in:
- lib/gcloud/datastore/key.rb
Overview
# Key
Every Datastore record has an identifying key, which includes the record’s entity kind and a unique identifier. The identifier may be either a key name string, assigned explicitly by the application, or an integer numeric ID, assigned automatically by Datastore.
Instance Attribute Summary collapse
-
#id ⇒ Integer?
The id of the Key.
-
#kind ⇒ String
The kind of the Key.
-
#name ⇒ String?
The name of the Key.
-
#namespace ⇒ String?
The namespace of the Key.
-
#parent ⇒ Key?
The parent of the Key.
-
#project ⇒ String
(also: #dataset_id)
The project of the Key.
Class Method Summary collapse
Instance Method Summary collapse
-
#complete? ⇒ Boolean
Determine if the key is complete.
-
#incomplete? ⇒ Boolean
Determine if the key is incomplete.
-
#initialize(kind = nil, id_or_name = nil) ⇒ Gcloud::Datastore::Dataset::Key
constructor
Create a new Key instance.
-
#path ⇒ Array<Array<(String, String)>>
Represent the Key’s path (including parent) as an array of arrays.
-
#serialized_size ⇒ Object
The number of bytes the Key will take to serialize during API calls.
- #to_grpc ⇒ Object
Constructor Details
#initialize(kind = nil, id_or_name = nil) ⇒ Gcloud::Datastore::Dataset::Key
Create a new Key instance.
90 91 92 93 94 95 96 97 |
# File 'lib/gcloud/datastore/key.rb', line 90 def initialize kind = nil, id_or_name = nil @kind = kind if id_or_name.is_a? Integer @id = id_or_name else @name = id_or_name end end |
Instance Attribute Details
#id ⇒ Integer?
The id of the Key.
127 128 129 |
# File 'lib/gcloud/datastore/key.rb', line 127 def id @id end |
#kind ⇒ String
The kind of the Key.
40 41 42 |
# File 'lib/gcloud/datastore/key.rb', line 40 def kind @kind end |
#name ⇒ String?
The name of the Key.
157 158 159 |
# File 'lib/gcloud/datastore/key.rb', line 157 def name @name end |
#namespace ⇒ String?
The namespace of the Key.
76 77 78 |
# File 'lib/gcloud/datastore/key.rb', line 76 def namespace @namespace end |
#parent ⇒ Key?
The parent of the Key.
198 199 200 |
# File 'lib/gcloud/datastore/key.rb', line 198 def parent @parent end |
#project ⇒ String Also known as: dataset_id
The project of the Key.
57 58 59 |
# File 'lib/gcloud/datastore/key.rb', line 57 def project @project end |
Class Method Details
.from_grpc(grpc) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/gcloud/datastore/key.rb', line 263 def self.from_grpc grpc key_grpc = grpc.dup key = Key.new path_grpc = key_grpc.path.pop if path_grpc key = Key.new path_grpc.kind, (path_grpc.id || path_grpc.name) end if key_grpc.partition_id key.project = key_grpc.partition_id.project_id key.namespace = key_grpc.partition_id.namespace_id end key.parent = Key.from_grpc(key_grpc) if key_grpc.path.count > 0 # Freeze the key to make it immutable. key.freeze key end |
Instance Method Details
#complete? ⇒ Boolean
Determine if the key is complete. A complete key has either an id or a name.
Inverse of #incomplete?
222 223 224 |
# File 'lib/gcloud/datastore/key.rb', line 222 def complete? !incomplete? end |
#incomplete? ⇒ Boolean
Determine if the key is incomplete. An incomplete key has neither an id nor a name.
Inverse of #complete?
231 232 233 |
# File 'lib/gcloud/datastore/key.rb', line 231 def incomplete? kind.nil? || (id.nil? && (name.nil? || name.empty?)) end |
#path ⇒ Array<Array<(String, String)>>
Represent the Key’s path (including parent) as an array of arrays. Each inner array contains two values, the kind and the id or name. If neither an id or name exist then nil will be returned.
212 213 214 215 |
# File 'lib/gcloud/datastore/key.rb', line 212 def path new_path = parent ? parent.path : [] new_path << [kind, (id || name)] end |
#serialized_size ⇒ Object
The number of bytes the Key will take to serialize during API calls.
237 238 239 |
# File 'lib/gcloud/datastore/key.rb', line 237 def serialized_size to_grpc.to_proto.length end |
#to_grpc ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/gcloud/datastore/key.rb', line 243 def to_grpc grpc_path = path.map do |pe_kind, pe_id_or_name| path_args = { kind: pe_kind } if pe_id_or_name.is_a? Integer path_args[:id] = pe_id_or_name elsif pe_id_or_name.is_a? String path_args[:name] = pe_id_or_name unless pe_id_or_name.empty? end Google::Datastore::V1beta3::Key::PathElement.new(path_args) end grpc = Google::Datastore::V1beta3::Key.new(path: grpc_path) if project || namespace grpc.partition_id = Google::Datastore::V1beta3::PartitionId.new( project_id: project.to_s, namespace_id: namespace.to_s) end grpc end |