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.

Examples:

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind = nil, id_or_name = nil) ⇒ Gcloud::Datastore::Dataset::Key

Create a new Key instance.

Examples:

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

Parameters:

  • kind (String) (defaults to: nil)

    The kind of the Key. This is optional.

  • id_or_name (Integer, String) (defaults to: nil)

    The id or name of the Key. This is optional.


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_idString

The dataset_id of the Key.

Examples:

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"

Returns:

  • (String)

59
60
61
# File 'lib/gcloud/datastore/key.rb', line 59

def dataset_id
  @dataset_id
end

#idInteger?

The id of the Key.

Examples:

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

Returns:

  • (Integer, nil)

127
128
129
# File 'lib/gcloud/datastore/key.rb', line 127

def id
  @id
end

#kindString

The kind of the Key.

Examples:

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

Returns:

  • (String)

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

def kind
  @kind
end

#nameString?

The name of the Key.

Examples:

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

Returns:

  • (String, nil)

157
158
159
# File 'lib/gcloud/datastore/key.rb', line 157

def name
  @name
end

#namespaceString?

The namespace of the Key.

Examples:

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"

Returns:

  • (String, nil)

76
77
78
# File 'lib/gcloud/datastore/key.rb', line 76

def namespace
  @namespace
end

#parentKey?

The parent of the Key.

Examples:

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

Returns:


191
192
193
# File 'lib/gcloud/datastore/key.rb', line 191

def parent
  @parent
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)

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?

Returns:

  • (Boolean)

224
225
226
# File 'lib/gcloud/datastore/key.rb', line 224

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

#pathArray<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.

Examples:

key = Gcloud::Datastore::Key.new "List", "todos"
key.parent = Gcloud::Datastore::Key.new "User", "[email protected]"
key.path #=> [["User", "[email protected]"], ["List", "todos"]]

Returns:

  • (Array<Array<(String, String)>>)

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