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

Returns a new instance of Entity.

Parameters:

  • type (String)

    Typically the class name of the entity (as used in the other SDKs).

  • id (String) (defaults to: nil)

    The unique identifier of the entity.

  • data (Hash) (defaults to: nil)

    The initial contents of the 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

Returns the time the entity was created.

Returns:

  • (Time)

    the time the entity was created.



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

def created_at
  Time.parse self[:createdAt]
end

#idString

Returns the primary identifier of the entity.

Returns:

  • (String)

    the primary identifier of the entity.



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

def id
  @id
end

#owner_idString

Returns the player ID of the owner of the entity (referencing a Player entitity).

Returns:

  • (String)

    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

#pathString (readonly)

Returns the path to the REST-resource of the entity, relative to the game's root.

Returns:

  • (String)

    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_accessString

Returns the access rights of all players except the owner (the owner always has unlimited access). Possible values: '', 'r', 'rw'.

Returns:

  • (String)

    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

#typeString (readonly)

Returns the type of the entity. Types group entities together on the server.

Returns:

  • (String)

    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

#updated_atTime

Returns the time the entity was last changed on the server.

Returns:

  • (Time)

    the time the entity was last changed on the server.



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

Returns provides a simple string representation of the Entity.

Returns:

  • (String)

    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