Gcloud::Datastore::Key

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"

Methods

Attributes

dataset_id [RW]

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", "heidi"
entity.key.dataset_id #=> "my-todo-project"

id [R]

The id of the Key.

Returns

Integer or nil

Example

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

kind [RW]

The kind of the Key.

Returns

String

Example

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

name [R]

The name of the Key.

Returns

String or nil

Example

key = Gcloud::Datastore::Key.new "User", "heidi"
key.name #=> "heidi"

namespace [RW]

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", "heidi"
entity.key.namespace #=> "ns~todo-project"

parent [R]

The parent of the Key.

Returns

Key or nil

Example

require "gcloud"

gcloud = Gcloud.new
dataset = gcloud.datastore

user = dataset.find "User", "heidi"
query = Gcloud::Datastore::Query.new
query.kind("List").
  ancestor(user.key)
lists = dataset.run query
lists.first.key.parent #=> Key("User", "heidi")

Public Class Methods

new(kind = nil, id_or_name = nil)

Public Instance Methods

complete?()

Determine if the key is complete. A complete key has either an id or a name.

Inverse of incomplete?

incomplete?()

Determine if the key is incomplete. An incomplete key has neither an id nor a name.

Inverse of complete?

path()

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"]]