Method: AppEngine::Datastore::Key.from_path
- Defined in:
- lib/appengine-apis/datastore_types.rb
.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 |