Class: Gcloud::Datastore::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/datastore/key.rb

Overview

Datastore 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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Key.



41
42
43
44
45
46
47
48
# File 'lib/gcloud/datastore/key.rb', line 41

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.



35
36
37
# File 'lib/gcloud/datastore/key.rb', line 35

def dataset_id
  @dataset_id
end

#idObject

The id of the Key.



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

def id
  @id
end

#kindObject

The kind of the Key.



31
32
33
# File 'lib/gcloud/datastore/key.rb', line 31

def kind
  @kind
end

#nameObject

The name of the Key.



72
73
74
# File 'lib/gcloud/datastore/key.rb', line 72

def name
  @name
end

#namespaceObject

The namespace of the Key.



39
40
41
# File 'lib/gcloud/datastore/key.rb', line 39

def namespace
  @namespace
end

#parentObject

The parent of the Key.



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

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.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/gcloud/datastore/key.rb', line 128

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.

Returns:

  • (Boolean)


100
101
102
# File 'lib/gcloud/datastore/key.rb', line 100

def complete?
  !incomplete?
end

#incomplete?Boolean

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

Returns:

  • (Boolean)


107
108
109
# File 'lib/gcloud/datastore/key.rb', line 107

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.

puts key.path #=> [["Person", "username"], ["Task", 123456]]


92
93
94
95
# File 'lib/gcloud/datastore/key.rb', line 92

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.



114
115
116
117
118
119
120
121
# File 'lib/gcloud/datastore/key.rb', line 114

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