Class: AppEngine::Datastore::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/appengine-apis/datastore_types.rb

Overview

The primary key for a datastore entity.

A datastore GUID. A Key instance uniquely identifies an entity across all apps, and includes all information necessary to fetch the entity from the datastore with #Datastore.get(Key).

See also code.google.com/appengine/docs/java/javadoc/com/google/appengine/api/datastore/Key.html

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_path(parent_or_kind, kind_or_id, *args) ⇒ Object

call-seq:

Key.from_path(parent=nil, kind, id, [kind, id]...) -> Key

Constructs a Key out of a path.

This is useful when an application wants to use just the ‘id’ portion of a key in e.g. a URL, where the rest of the URL provides enough context to fill in the rest, i.e. the app id (always implicit), the entity kind, and possibly an ancestor key. Since the ‘id’ is a relatively small int, it is more attractive for use in end-user-visible URLs than the full string representation of a key.

Args:

  • parent: Optional parent key

  • kind: the entity kind (a string)

  • id: the id (an integer)

  • Additional, optional ‘kind’ and ‘id’ arguments are allowed in

an alternating order (kind1, 1, kind2, 2, ...)
  • options: a Hash. If specified, options is used as

    the parent Key.
    

Returns:

  • A new Key instance whose #kind and #id methods return the last

    kind and id arugments passed
    


308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/appengine-apis/datastore_types.rb', line 308

def from_path(parent_or_kind, kind_or_id, *args)
  # Extract parent
  parent = nil
  if parent_or_kind.is_a? Key
    parent = parent_or_kind
    args[0,0] = [kind_or_id]
  else
    args[0,0] = [parent_or_kind, kind_or_id]
  end

  if args.size % 2 != 0
    raise ArgumentError, 'Expected an even number of arguments ' \
                         '(kind1, id1, kind2, id2, ...); received ' \
                         "#{args.inspect}"
  end

  # Type-check parent
  if parent
    unless parent.is_a? Key
      raise ArgumentError, 'Expected nil or a Key as a parent; ' \
                           "received #{parent} (a #{parent.class})."
    end
    unless parent.is_complete?
      raise KeyError, 'The parent key has not yet been Put.'
    end
  end

  current = parent
  (0...args.size).step(2) do |i|
    kind, id = args[i,2]
    kind = kind.to_s if kind.is_a? Symbol
    if current
      current = current.getChild(kind, id)
    else
      current = JavaDatastore::KeyFactory.createKey(kind, id)
    end
  end

  return current
end

.new(encoded) ⇒ Object

Creates a new Key from an encoded String.



279
280
281
# File 'lib/appengine-apis/datastore_types.rb', line 279

def new(encoded)
  JavaDatastore::KeyFactory.stringToKey(encoded)
end

Instance Method Details

#id_or_nameObject



272
273
274
# File 'lib/appengine-apis/datastore_types.rb', line 272

def id_or_name
  name || id
end

#to_sObject

Converts a Key into a websafe string. For example, this string can safely be used as an URL parameter embedded in a HTML document.



264
265
266
# File 'lib/appengine-apis/datastore_types.rb', line 264

def to_s
  JavaDatastore::KeyFactory.keyToString(self)
end