Class: Flox::Entity
- Inherits:
-
Hash
- Object
- Hash
- Flox::Entity
- Defined in:
- lib/flox/entity.rb
Overview
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)
Direct Known Subclasses
Instance Attribute Summary collapse
-
#created_at ⇒ Time
The time the entity was created.
-
#id ⇒ String
The primary identifier of the entity.
-
#owner_id ⇒ String
The player ID of the owner of the entity (referencing a Player entitity).
-
#path ⇒ String
readonly
The path to the REST-resource of the entity, relative to the game's root.
-
#public_access ⇒ String
The access rights of all players except the owner (the owner always has unlimited access).
-
#type ⇒ String
readonly
The type of the entity.
-
#updated_at ⇒ Time
The time the entity was last changed on the server.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Accesses a property of the entity; both symbols and strings work.
-
#[]=(key, value) ⇒ Object
Accesses a property of the entity; both symbols and strings work.
-
#initialize(type, id = nil, data = nil) ⇒ Entity
constructor
A new instance of Entity.
-
#inspect ⇒ String
Provides a simple string representation of the Entity.
Constructor Details
#initialize(type, id = nil, data = nil) ⇒ Entity
Returns a new instance of Entity.
36 37 38 39 40 41 42 43 44 45 |
# 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 |
Instance Attribute Details
#created_at ⇒ Time
Returns the time the entity was created.
102 103 104 |
# File 'lib/flox/entity.rb', line 102 def created_at Time.parse self[:createdAt] end |
#id ⇒ String
Returns the primary identifier of the entity.
28 29 30 |
# File 'lib/flox/entity.rb', line 28 def id @id end |
#owner_id ⇒ String
Returns the player ID of the owner of the entity (referencing a Player entitity).
98 99 100 |
# File 'lib/flox/entity.rb', line 98 def owner_id self[:ownerId] end |
#path ⇒ String (readonly)
Returns the path to the REST-resource of the entity, relative to the game's root.
112 113 114 |
# File 'lib/flox/entity.rb', line 112 def path "entities/#{@type}/#{@id}" end |
#public_access ⇒ String
Returns the access rights of all players except the owner (the owner always has unlimited access). Possible values: '', 'r', 'rw'.
108 109 110 |
# File 'lib/flox/entity.rb', line 108 def public_access self[:publicAccess] end |
#type ⇒ String (readonly)
Returns the type of the entity. Types group entities together on the server.
31 32 33 |
# File 'lib/flox/entity.rb', line 31 def type @type end |
Instance Method Details
#[](key) ⇒ Object
Accesses a property of the entity; both symbols and strings work.
85 86 87 |
# File 'lib/flox/entity.rb', line 85 def [](key) super(key.to_sym) end |
#[]=(key, value) ⇒ Object
Accesses a property of the entity; both symbols and strings work.
90 91 92 |
# File 'lib/flox/entity.rb', line 90 def []=(key, value) super(key.to_sym, value) end |
#inspect ⇒ String
Returns provides a simple string representation of the Entity.
76 77 78 79 80 81 82 |
# 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 |