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
-
#dataset_id ⇒ String
The dataset_id of the Key.
-
#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.
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.
- #to_proto ⇒ 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
#dataset_id ⇒ String
The dataset_id of the Key.
59 60 61 |
# File 'lib/gcloud/datastore/key.rb', line 59 def dataset_id @dataset_id end |
#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.
42 43 44 |
# File 'lib/gcloud/datastore/key.rb', line 42 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.
191 192 193 |
# File 'lib/gcloud/datastore/key.rb', line 191 def parent @parent end |
Class Method Details
.from_proto(proto) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/gcloud/datastore/key.rb', line 243 def self.from_proto proto # Disable rules because the complexity here is neccessary. key_proto = proto.dup key = Key.new proto_path_element = Array(key_proto.path_element).pop if proto_path_element key = Key.new proto_path_element.kind, proto_path_element.id || proto_path_element.name end if key_proto.partition_id key.dataset_id = key_proto.partition_id.dataset_id key.namespace = key_proto.partition_id.namespace end if Array(key_proto.path_element).count > 0 key.parent = Key.from_proto(key_proto) end # 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?
215 216 217 |
# File 'lib/gcloud/datastore/key.rb', line 215 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?
224 225 226 |
# File 'lib/gcloud/datastore/key.rb', line 224 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.
205 206 207 208 |
# File 'lib/gcloud/datastore/key.rb', line 205 def path new_path = parent ? parent.path : [] new_path << [kind, (id || name)] end |
#to_proto ⇒ Object
230 231 232 233 234 235 236 237 |
# File 'lib/gcloud/datastore/key.rb', line 230 def to_proto Proto::Key.new.tap do |k| k.path_element = path.map do |pe_kind, pe_id_or_name| Proto.new_path_element pe_kind, pe_id_or_name end k.partition_id = Proto.new_partition_id dataset_id, namespace end end |