Class: Flox::Entity

Inherits:
Hash
  • Object
show all
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

Player

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, id = nil, data = nil) ⇒ 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_atTime



102
103
104
# File 'lib/flox/entity.rb', line 102

def created_at
  Time.parse self[:createdAt]
end

#idString



28
29
30
# File 'lib/flox/entity.rb', line 28

def id
  @id
end

#owner_idString



98
99
100
# File 'lib/flox/entity.rb', line 98

def owner_id
  self[:ownerId]
end

#pathString (readonly)



112
113
114
# File 'lib/flox/entity.rb', line 112

def path
  "entities/#{@type}/#{@id}"
end

#public_accessString



108
109
110
# File 'lib/flox/entity.rb', line 108

def public_access
  self[:publicAccess]
end

#typeString (readonly)



31
32
33
# File 'lib/flox/entity.rb', line 31

def type
  @type
end

#updated_atTime



105
106
107
# File 'lib/flox/entity.rb', line 105

def updated_at
  Time.parse self[:updatedAt]
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

#inspectString



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