Class: Gcloud::Datastore::Key

Inherits:
Object
  • Object
show all
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", "[email protected]"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind = nil, id_or_name = nil) ⇒ Key

Create a new Key instance.

Parameters

kind

The kind of the Key. This is optional. (String)

id_or_name

The id or name of the Key. This is optional. (Integer or String)

Returns

Gcloud::Datastore::Dataset::Key

Example

key = Gcloud::Datastore::Key.new "User", "[email protected]"


103
104
105
106
107
108
109
110
# File 'lib/gcloud/datastore/key.rb', line 103

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_idObject

The dataset_id of the Key.

Returns

String

Example

require "gcloud"

gcloud = Gcloud.new "my-todo-project",
                    "/path/to/keyfile.json"

dataset = gcloud.datastore
entity = dataset.find "User", "[email protected]"
entity.key.dataset_id #=> "my-todo-project"


63
64
65
# File 'lib/gcloud/datastore/key.rb', line 63

def dataset_id
  @dataset_id
end

#idObject

The id of the Key.

Returns

Integer or nil

Example

key = Gcloud::Datastore::Key.new "User", 123456
key.id #=> 123456


146
147
148
# File 'lib/gcloud/datastore/key.rb', line 146

def id
  @id
end

#kindObject

The kind of the Key.

Returns

String

Example

key = Gcloud::Datastore::Key.new "User"
key.kind #=> "User"
key.kind = "Task"


43
44
45
# File 'lib/gcloud/datastore/key.rb', line 43

def kind
  @kind
end

#nameObject

The name of the Key.

Returns

String or nil

Example

key = Gcloud::Datastore::Key.new "User", "[email protected]"
key.name #=> "[email protected]"


182
183
184
# File 'lib/gcloud/datastore/key.rb', line 182

def name
  @name
end

#namespaceObject

The namespace of the Key.

Returns

String or nil

Example

require "gcloud"

gcloud = Gcloud.new "my-todo-project",
                    "/path/to/keyfile.json"

dataset = gcloud.datastore
entity = dataset.find "User", "[email protected]"
entity.key.namespace #=> "ns~todo-project"


83
84
85
# File 'lib/gcloud/datastore/key.rb', line 83

def namespace
  @namespace
end

#parentObject

The parent of the Key.

Returns

Key or nil

Example

require "gcloud"

gcloud = Gcloud.new
dataset = gcloud.datastore

user = dataset.find "User", "[email protected]"
query = dataset.query("List").
  ancestor(user.key)
lists = dataset.run query
lists.first.key.parent #=> Key("User", "[email protected]")


222
223
224
# File 'lib/gcloud/datastore/key.rb', line 222

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.



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/gcloud/datastore/key.rb', line 279

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?

Returns:

  • (Boolean)


249
250
251
# File 'lib/gcloud/datastore/key.rb', line 249

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?

Returns:

  • (Boolean)


258
259
260
# File 'lib/gcloud/datastore/key.rb', line 258

def incomplete?
  kind.nil? || (id.nil? && (name.nil? || name.empty?))
end

#pathObject

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", "[email protected]"
key.path #=> [["User", "[email protected]"], ["List", "todos"]]


239
240
241
242
# File 'lib/gcloud/datastore/key.rb', line 239

def path
  new_path = parent ? parent.path : []
  new_path << [kind, (id || name)]
end

#to_protoObject

Convert the Key to a protocol buffer object. This is not part of the public API.



265
266
267
268
269
270
271
272
# File 'lib/gcloud/datastore/key.rb', line 265

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