Class: AppEngine::Datastore::Entity

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

Overview

Entity is the fundamental unit of data storage. It has an immutable identifier (contained in the Key) object, a reference to an optional parent Entity, a kind (represented as an arbitrary string), and a set of zero or more typed properties.

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

Constant Summary collapse

SPECIAL_JAVA_TYPES =
{
  JavaDatastore::Text => Text,
  JavaDatastore::Blob => Blob,
  JavaDatastore::ShortBlob => ByteString,
  JavaDatastore::Link => Link,
  JavaDatastore::Email => Email,
  JavaDatastore::Category => Category,
  JavaDatastore::PhoneNumber => PhoneNumber,
  JavaDatastore::PostalAddress => PostalAddress,
  java.util.Date => Time,
}.freeze

Instance Method Summary collapse

Instance Method Details

#delete(name) ⇒ Object

Removes any property with the specified name. If there is no property with this name set, simply does nothing.



468
469
470
471
472
473
# File 'lib/appengine-apis/datastore_types.rb', line 468

def delete(name)
  name = name.to_s if name.kind_of? Symbol
  Datastore.convert_exceptions do
    removeProperty(name)
  end
end

#eachObject

Iterates over all the properties in this Entity.



498
499
500
501
502
# File 'lib/appengine-apis/datastore_types.rb', line 498

def each()  # :yields: name, value
  getProperties.each do |name, value|
    yield name, java_value_to_ruby(value)
  end
end

#get_property(name) ⇒ Object Also known as: []

Returns the property with the specified name.



433
434
435
436
437
# File 'lib/appengine-apis/datastore_types.rb', line 433

def get_property(name)
  name = name.to_s if name.kind_of? Symbol
  prop = Datastore.convert_exceptions { getProperty(name) }
  java_value_to_ruby(prop)
end

#has_property?(name) ⇒ Boolean Also known as: has_property

Returns true if a property has been set. This function can be used to test if a property has been specifically set to nil.

Returns:

  • (Boolean)


479
480
481
482
483
484
# File 'lib/appengine-apis/datastore_types.rb', line 479

def has_property?(name)
  name = name.to_s if name.kind_of? Symbol
  Datastore.convert_exceptions do
    hasProperty(name)
  end
end

#java_value_to_ruby(prop) ⇒ Object



512
513
514
515
516
517
518
519
# File 'lib/appengine-apis/datastore_types.rb', line 512

def java_value_to_ruby(prop)
  ruby_type = SPECIAL_JAVA_TYPES[prop.class]
  if ruby_type
    ruby_type.new_from_java(prop)
  else
    prop
  end
end

#set_property(name, value) ⇒ Object Also known as: []=

Sets the property named, name, to value.

As the value is stored in the datastore, it is converted to the datastore’s native type.

All Enumerables are prone to losing their sort order and their original types as they are stored in the datastore. For example, a Set may be returned as an Array from #getProperty, with an arbitrary re-ordering of elements.

value may be one of the supported datatypes, or a heterogenous Enumerable of one of the supported datatypes.

Throws ArgumentError if the value is not of a type that the data store supports.



456
457
458
459
460
461
462
# File 'lib/appengine-apis/datastore_types.rb', line 456

def set_property(name, value)
  name = name.to_s if name.kind_of? Symbol
  value = Datastore.ruby_to_java(value)
  Datastore.convert_exceptions do
    setProperty(name, value)
  end
end

#to_hashObject



504
505
506
507
508
509
510
# File 'lib/appengine-apis/datastore_types.rb', line 504

def to_hash
  inject({}) do |hash, item|
    name, value = item
    hash[name] = value
    hash
  end
end

#update(other) ⇒ Object Also known as: merge!

Add the properties from other to this Entity. Other may be an Entity or Hash



489
490
491
492
493
494
# File 'lib/appengine-apis/datastore_types.rb', line 489

def update(other)
  other.each do |name, value|
    self[name] = value
  end
  self
end