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.
key = Gcloud::Datastore::Key.new "User", "username"
Instance Attribute Summary collapse
-
#dataset_id ⇒ Object
The dataset_id of the Key.
-
#id ⇒ Object
The id of the Key.
-
#kind ⇒ Object
The kind of the Key.
-
#name ⇒ Object
The name of the Key.
-
#namespace ⇒ Object
The namespace of the Key.
-
#parent ⇒ Object
The parent of the Key.
Class Method Summary collapse
-
.from_proto(proto) ⇒ Object
Create a new Key from a protocol buffer object.
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) ⇒ Key
constructor
A new instance of Key.
-
#path ⇒ Object
Represent the Key’s path (including parent) as an array of arrays.
-
#to_proto ⇒ Object
Convert the Key to a protocol buffer object.
Constructor Details
#initialize(kind = nil, id_or_name = nil) ⇒ Key
Returns a new instance of Key.
85 86 87 88 89 90 91 92 |
# File 'lib/gcloud/datastore/key.rb', line 85 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 ⇒ Object
63 64 65 |
# File 'lib/gcloud/datastore/key.rb', line 63 def dataset_id @dataset_id end |
#id ⇒ Object
128 129 130 |
# File 'lib/gcloud/datastore/key.rb', line 128 def id @id end |
#kind ⇒ Object
43 44 45 |
# File 'lib/gcloud/datastore/key.rb', line 43 def kind @kind end |
#name ⇒ Object
164 165 166 |
# File 'lib/gcloud/datastore/key.rb', line 164 def name @name end |
#namespace ⇒ Object
83 84 85 |
# File 'lib/gcloud/datastore/key.rb', line 83 def namespace @namespace end |
#parent ⇒ Object
205 206 207 |
# File 'lib/gcloud/datastore/key.rb', line 205 def parent @parent end |
Class Method Details
.from_proto(proto) ⇒ Object
Create a new Key from a protocol buffer object. This is not part of the public API.
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/gcloud/datastore/key.rb', line 262 def self.from_proto proto #:nodoc: # 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?
232 233 234 |
# File 'lib/gcloud/datastore/key.rb', line 232 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?
241 242 243 |
# File 'lib/gcloud/datastore/key.rb', line 241 def incomplete? kind.nil? || (id.nil? && (name.nil? || name.empty?)) end |
#path ⇒ Object
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.
Returns
Array of arrays
Example
key = Gcloud::Datastore::Key.new "List", "todos"
key.parent = Gcloud::Datastore::Key.new "User", "heidi"
key.path #=> [["User", "heidi"], ["List", "todos"]]
222 223 224 225 |
# File 'lib/gcloud/datastore/key.rb', line 222 def path new_path = parent ? parent.path : [] new_path << [kind, (id || name)] end |
#to_proto ⇒ Object
Convert the Key to a protocol buffer object. This is not part of the public API.
248 249 250 251 252 253 254 255 |
# File 'lib/gcloud/datastore/key.rb', line 248 def to_proto #:nodoc: 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 |