Class: Gcloud::Datastore::Entity
- Inherits:
-
Object
- Object
- Gcloud::Datastore::Entity
- Defined in:
- lib/gcloud/datastore/entity.rb
Overview
# Entity
Entity represents a Datastore record. Every Entity has a Key, and a list of properties.
Instance Attribute Summary collapse
-
#key ⇒ Object
The Key that identifies the entity.
-
#properties ⇒ Gcloud::Datastore::Properties
readonly
Retrieve properties in a hash-like structure.
Class Method Summary collapse
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.
- #to_proto ⇒ Object
Constructor Details
#initialize ⇒ Entity
Create a new Entity object.
40 41 42 43 44 |
# File 'lib/gcloud/datastore/entity.rb', line 40 def initialize @properties = Properties.new @key = Key.new @_exclude_indexes = {} end |
Instance Attribute Details
#key ⇒ Object
The Key that identifies the entity.
36 37 38 |
# File 'lib/gcloud/datastore/entity.rb', line 36 def key @key end |
#properties ⇒ Gcloud::Datastore::Properties (readonly)
Retrieve properties in a hash-like structure. Properties can be accessed or set by string or symbol.
125 126 127 |
# File 'lib/gcloud/datastore/entity.rb', line 125 def properties @properties end |
Class Method Details
.from_proto(proto) ⇒ Object
277 278 279 280 281 282 283 284 285 |
# File 'lib/gcloud/datastore/entity.rb', line 277 def self.from_proto proto entity = Entity.new entity.key = Key.from_proto proto.key Array(proto.property).each do |p| entity[p.name] = Proto.from_proto_value p.value end entity.send :update_exclude_indexes!, proto entity end |
Instance Method Details
#[](prop_name) ⇒ Object?
Retrieve a property value by providing the name.
69 70 71 |
# File 'lib/gcloud/datastore/entity.rb', line 69 def [] prop_name @properties[prop_name] end |
#[]=(prop_name, prop_value) ⇒ Object
Set a property value by name.
95 96 97 |
# File 'lib/gcloud/datastore/entity.rb', line 95 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]`.
254 255 256 257 258 259 260 261 262 |
# File 'lib/gcloud/datastore/entity.rb', line 254 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]`.
203 204 205 206 207 |
# File 'lib/gcloud/datastore/entity.rb', line 203 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.
173 174 175 |
# File 'lib/gcloud/datastore/entity.rb', line 173 def persisted? @key && @key.frozen? end |
#to_proto ⇒ Object
266 267 268 269 270 271 272 273 |
# File 'lib/gcloud/datastore/entity.rb', line 266 def to_proto entity = Proto::Entity.new.tap do |e| e.key = @key.to_proto e.property = Proto.to_proto_properties @properties.to_h end update_properties_indexed! entity entity end |