class Flox::Entity
The base class of all objects that can be stored persistently on the Flox
server.
The class extends `Hash`. Thus, all properties of the Entity
can be accessed as keys of the Entity
instance.
entity[:name] = 'Donald Duck'
For convenience, the standard entity properties (e.g. `created_at` and `updated_at`)can be accessed via Ruby attributes.
entity.public_access = 'rw'
To load and save an entity, use the respective methods on the Flox
class.
my_entity = flox.load_entity(:SaveGame, '12345') # => Flox::Entity flox.save_entity(my_entity)
Attributes
@return [String] the primary identifier of the entity.
@return [String] the type of the entity. Types group entities together on the server.
Public Class Methods
@param type [String] Typically the class name of the entity (as used in the other SDKs). @param id [String] The unique identifier of the entity. @param data [Hash] The initial contents of the entity.
# File lib/flox/entity.rb, line 36 def initialize(type, id=nil, data=nil) @type = type @id = id ? id : String.random_uid self[:createdAt] = self[:updatedAt] = Time.now.utc.to_xs_datetime self[:publicAccess] = '' if (data) data_sym = Hash[data.map{|k, v| [k.to_sym, v]}] self.merge!(data_sym) end end
Public Instance Methods
Accesses a property of the entity; both symbols and strings work.
# File lib/flox/entity.rb, line 85 def [](key) super(key.to_sym) end
(see []
)
# File lib/flox/entity.rb, line 90 def []=(key, value) super(key.to_sym, value) end
# File lib/flox/entity.rb, line 47 def created_at Time.parse self[:createdAt] end
@return [String] provides a simple string representation of the Entity
.
# File lib/flox/entity.rb, line 76 def inspect description = "[#{self.class} #{@id} (#{@type})\n" each_pair do |key, value| description += " #{key}: #{value}\n" end description += "]" end
# File lib/flox/entity.rb, line 63 def owner_id self[:ownerId] end
# File lib/flox/entity.rb, line 67 def owner_id=(value) self[:ownerId] = value.to_s end
# File lib/flox/entity.rb, line 71 def path "entities/#{@type}/#{@id}" end
# File lib/flox/entity.rb, line 55 def public_access self[:publicAccess] end
# File lib/flox/entity.rb, line 59 def public_access=(access) self[:publicAccess] = access.to_s end
# File lib/flox/entity.rb, line 51 def updated_at Time.parse self[:updatedAt] end