Gcloud::Datastore::Entity
Entity¶ ↑
Entity represents a Datastore record. Every Entity has a Key, and a list of properties.
entity = Gcloud::Datastore::Entity.new entity.key = Gcloud::Datastore::Key.new "User", "username" entity["name"] = "User McUser" entity["email"] = "user@example.net"
Methods
Attributes
properties
[R]
¶
↑
Retrieve properties in a hash-like structure. Properties can be accessed or set by string or symbol.
Returns¶ ↑
Example¶ ↑
entity.properties[:name] = "User McUser" entity.properties["name"] #=> "User McUser" entity.properties.each do |name, value| puts "property #{name} has a value of #{value}" end
A property's existance can be determined by calling exist?
entity.properties.exist? :name #=> true entity.properties.exist? "name" #=> true entity.properties.exist? :expiration #=> false
A property can be removed from the entity.
entity.properties.delete :name entity.save
The properties can be converted to a hash:
prop_hash = entity.properties.to_h
Public Instance Methods
[](prop_name)
¶
↑
Retrieve a property value by providing the name.
Parameters¶ ↑
prop_name
-
The name of the property. (
String
orSymbol
)
Returns¶ ↑
Object if the property exists, nil
if the property doesn't
exist
Example¶ ↑
Properties can be retrieved with a string name:
require "gcloud" gcloud = Gcloud.new dataset = gcloud.datastore user = dataset.find "User", "heidi" user["name"] #=> "Heidi Henderson"
Or with a symbol name:
require "gcloud" gcloud = Gcloud.new dataset = gcloud.datastore user = dataset.find "User", "heidi" user[:name] #=> "Heidi Henderson"
[]=(prop_name, prop_value)
¶
↑
Set a property value by name.
Parameters¶ ↑
prop_name
-
The name of the property. (
String
orSymbol
) prop_value
-
The value of the property. (
Object
)
Example¶ ↑
Properties can be set with a string name:
require "gcloud" gcloud = Gcloud.new dataset = gcloud.datastore user = dataset.find "User", "heidi" user["name"] = "Heidi H. Henderson"
Or with a symbol name:
require "gcloud" gcloud = Gcloud.new dataset = gcloud.datastore user = dataset.find "User", "heidi" user[:name] = "Heidi H. Henderson"
exclude_from_indexes!(name, flag = nil, &block)
¶
↑
Flag a property to be excluded from the Datastore indexes. Setting true will exclude the property from the indexes. Setting false will include the property on any applicable indexes. The default value for the flag is false.
entity["age"] = 21 entity.exclude_from_indexes! "age", true
Properties that are arrays can be given multiple exclude flags.
entity["tags"] = ["ruby", "code"] entity.exclude_from_indexes! "tags", [true, false]
Or, array properties can be given a single flag that will be applied to each item in the array.
entity["tags"] = ["ruby", "code"] entity.exclude_from_indexes! "tags", true
Flags can also be set with a block for either single and array values.
entity["age"] = 21 entity.exclude_from_indexes! "age" do |age| age > 18 end
exclude_from_indexes?(name)
¶
↑
Indicates if a property is flagged to be excluded from the Datastore indexes. The default value is false.
Single property values will return a single flag setting.
entity["age"] = 21 entity.exclude_from_indexes? "age" #=> false
Array property values will return an array of flag settings.
entity["tags"] = ["ruby", "code"] entity.exclude_from_indexes? "tags" #=> [false, false]
key=(new_key)
¶
↑
Sets the Key that identifies the entity.
Example¶ ↑
The Key can be set before the entity is saved.
require "gcloud" gcloud = Gcloud.new dataset = gcloud.datastore entity = Gcloud::Datastore::Entity.new entity.key = Gcloud::Datastore::Key.new "User" dataset.save entity
Once the entity is saved, the key is frozen and immutable. Trying to set a
key when immutable will raise a RuntimeError
.
require "gcloud" gcloud = Gcloud.new dataset = gcloud.datastore entity = dataset.find "User", "heidi" entity.persisted? #=> true entity.key = Gcloud::Datastore::Key.new "User" #=> RuntimeError entity.key.frozen? #=> true entity.key.id = 9876543221 #=> RuntimeError