Class: Google::Cloud::Datastore::Entity
- Inherits:
-
Object
- Object
- Google::Cloud::Datastore::Entity
- Defined in:
- lib/google/cloud/datastore/entity.rb
Overview
# Entity
Entity represents a Datastore record. Every Entity has a Key, and a list of properties.
Entities in Datastore form a hierarchically structured space similar to the directory structure of a file system. When you create an entity, you can optionally designate another entity as its parent; the new entity is a child of the parent entity.
Instance Attribute Summary collapse
-
#key ⇒ Object
The Key that identifies the entity.
-
#properties ⇒ Google::Cloud::Datastore::Properties
readonly
Retrieve properties in a hash-like structure.
Class Method Summary collapse
-
.from_grpc(grpc) ⇒ Object
object.
Instance Method Summary collapse
-
#[](prop_name) ⇒ Object?
Retrieve a property value by providing the name.
-
#[]=(prop_name, prop_value) ⇒ Object
Set a property value by name.
-
#exclude_from_indexes!(name, flag = nil) {|value| ... } ⇒ Object
Sets whether a property should be excluded from the Datastore indexes.
-
#exclude_from_indexes?(name) ⇒ Boolean
Indicates if a property is flagged to be excluded from the Datastore indexes.
-
#initialize ⇒ Entity
constructor
Create a new Entity object.
-
#persisted? ⇒ Boolean
Indicates if the record is persisted.
-
#serialized_size ⇒ Object
The number of bytes the Entity will take to serialize during API calls.
-
#to_grpc ⇒ Object
object.
Constructor Details
#initialize ⇒ Entity
Create a new Entity object.
65 66 67 68 69 |
# File 'lib/google/cloud/datastore/entity.rb', line 65 def initialize @properties = Properties.new @key = Key.new @_exclude_indexes = {} end |
Instance Attribute Details
#key ⇒ Object
The Key that identifies the entity.
61 62 63 |
# File 'lib/google/cloud/datastore/entity.rb', line 61 def key @key end |
#properties ⇒ Google::Cloud::Datastore::Properties
Retrieve properties in a hash-like structure. Properties can be accessed or set by string or symbol.
205 206 207 |
# File 'lib/google/cloud/datastore/entity.rb', line 205 def properties @properties end |
Class Method Details
.from_grpc(grpc) ⇒ Object
object.
363 364 365 366 367 368 369 |
# File 'lib/google/cloud/datastore/entity.rb', line 363 def self.from_grpc grpc entity = Entity.new entity.key = Key.from_grpc grpc.key entity.send :properties=, Properties.from_grpc(grpc.properties) entity.send :update_exclude_indexes!, grpc.properties entity end |
Instance Method Details
#[](prop_name) ⇒ Object?
Retrieve a property value by providing the name.
Property values are converted from the Datastore value type automatically. Blob properties are returned as StringIO objects. Location properties are returned as a Hash with ‘:longitude` and `:latitude` keys.
119 120 121 |
# File 'lib/google/cloud/datastore/entity.rb', line 119 def [] prop_name properties[prop_name] end |
#[]=(prop_name, prop_value) ⇒ Object
Set a property value by name.
Property values are converted to use the proper Datastore value type automatically. Use an IO-compatible object (File, StringIO, Tempfile) to indicate the property value should be stored as a Datastore ‘blob`. IO-compatible objects are converted to StringIO objects when they are set. Use a Hash with `:longitude` and `:latitude` keys to indicate the property value should be stored as a Geo Point/LatLng.
175 176 177 |
# File 'lib/google/cloud/datastore/entity.rb', line 175 def []= prop_name, prop_value properties[prop_name] = prop_value end |
#exclude_from_indexes!(name, flag = nil) {|value| ... } ⇒ Object
Sets whether a property should 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 is `false`. This is another way of saying that values are indexed by default.
If the property is multi-valued, each value in the list can be managed separately for exclusion from indexing. When you call this method for a multi-valued property, you can pass either a single boolean argument to be applied to all of the values, or an array that contains the boolean argument for each corresponding value in the property. For example, if a multi-valued property contains ‘[“a”, “b”]`, and only the value `“b”` should be indexed (meaning that `“a”`’ should be excluded), you should pass the array: ‘[true, false]`.
331 332 333 334 335 336 337 338 339 |
# File 'lib/google/cloud/datastore/entity.rb', line 331 def exclude_from_indexes! name, flag = nil, &block name = name.to_s flag = block if block_given? if flag.nil? @_exclude_indexes.delete name else @_exclude_indexes[name] = flag end end |
#exclude_from_indexes?(name) ⇒ Boolean
Indicates if a property is flagged to be excluded from the Datastore indexes. The default value is ‘false`. This is another way of saying that values are indexed by default.
If the property is multi-valued, each value in the list can be managed separately for exclusion from indexing. Calling this method for a multi-valued property will return an array that contains the ‘excluded` boolean value for each corresponding value in the property. For example, if a multi-valued property contains `[“a”, “b”]`, and only the value `“b”` is indexed (meaning that `“a”`’ is excluded), the return value for this method will be ‘[true, false]`.
280 281 282 283 284 |
# File 'lib/google/cloud/datastore/entity.rb', line 280 def exclude_from_indexes? name value = self[name] flag = @_exclude_indexes[name.to_s] map_exclude_flag_to_value flag, value end |
#persisted? ⇒ Boolean
Indicates if the record is persisted. Default is false.
250 251 252 |
# File 'lib/google/cloud/datastore/entity.rb', line 250 def persisted? @key && @key.frozen? end |
#serialized_size ⇒ Object
The number of bytes the Entity will take to serialize during API calls.
344 345 346 |
# File 'lib/google/cloud/datastore/entity.rb', line 344 def serialized_size to_grpc.to_proto.length end |
#to_grpc ⇒ Object
object.
351 352 353 354 355 356 357 358 |
# File 'lib/google/cloud/datastore/entity.rb', line 351 def to_grpc grpc = Google::Datastore::V1::Entity.new( properties: @properties.to_grpc ) grpc.key = @key.to_grpc unless @key.nil? update_properties_indexed! grpc.properties grpc end |