Class: Google::Cloud::Datastore::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/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:

require "google/cloud/datastore"

task_key = Google::Cloud::Datastore::Key.new "Task", "sampleTask"

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create a new Key instance.

Examples:

require "google/cloud/datastore"

task_key = Google::Cloud::Datastore::Key.new "Task", "sampleTask"

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.



104
105
106
107
108
109
110
111
# File 'lib/google/cloud/datastore/key.rb', line 104

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

#idInteger?

The id of the Key.

Examples:

require "google/cloud/datastore"

task_key = Google::Cloud::Datastore::Key.new "Task", 123456
task_key.id #=> 123456

Returns:

  • (Integer, nil)


145
146
147
# File 'lib/google/cloud/datastore/key.rb', line 145

def id
  @id
end

#kindString

The kind of the Key.

Examples:

require "google/cloud/datastore"

key = Google::Cloud::Datastore::Key.new "TaskList"
key.kind #=> "TaskList"
key.kind = "Task"

Returns:

  • (String)


48
49
50
# File 'lib/google/cloud/datastore/key.rb', line 48

def kind
  @kind
end

#nameString?

The name of the Key.

Examples:

require "google/cloud/datastore"

task_key = Google::Cloud::Datastore::Key.new "Task", "sampleTask"
task_key.name #=> "sampleTask"

Returns:

  • (String, nil)


179
180
181
# File 'lib/google/cloud/datastore/key.rb', line 179

def name
  @name
end

#namespaceString?

The namespace of the Key.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new(
  project: "my-todo-project",
  keyfile: "/path/to/keyfile.json"
)

task = datastore.find "Task", "sampleTask"
task.key.namespace #=> "example-ns"

Returns:

  • (String, nil)


88
89
90
# File 'lib/google/cloud/datastore/key.rb', line 88

def namespace
  @namespace
end

#parentKey?

The parent of the Key.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

task_list = datastore.find "TaskList", "default"
query = datastore.query("Task").
  ancestor(task_list)
lists = datastore.run query
lists.first.key.parent # Key("TaskList", "default")

Returns:



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

def parent
  @parent
end

#projectString Also known as: project_id, dataset_id

The project of the Key.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new(
  project: "my-todo-project",
  keyfile: "/path/to/keyfile.json"
)

task = datastore.find "Task", "sampleTask"
task.key.project #=> "my-todo-project"

Returns:

  • (String)


66
67
68
# File 'lib/google/cloud/datastore/key.rb', line 66

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


251
252
253
# File 'lib/google/cloud/datastore/key.rb', line 251

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)


260
261
262
# File 'lib/google/cloud/datastore/key.rb', line 260

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:

require "google/cloud/datastore"

parent_key = Google::Cloud::Datastore::Key.new "TaskList", "default"
task_key = Google::Cloud::Datastore::Key.new "Task", "sampleTask"
task_key.parent = parent_key
task_key.path #=> [["TaskList", "default"], ["Task", "sampleTask"]]

Returns:

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


241
242
243
244
# File 'lib/google/cloud/datastore/key.rb', line 241

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

#serialized_sizeObject

The number of bytes the Key will take to serialize during API calls.



266
267
268
# File 'lib/google/cloud/datastore/key.rb', line 266

def serialized_size
  to_grpc.to_proto.length
end