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
# 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.public_access = ''
  self.merge!(data) if data
end

Instance Attribute Details

#created_atTime

Returns the time the entity was created.

Returns:

  • (Time)

    the time the entity was created.



89
90
91
# File 'lib/flox/entity.rb', line 89

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).



85
86
87
# File 'lib/flox/entity.rb', line 85

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.



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

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'



95
96
97
# File 'lib/flox/entity.rb', line 95

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.



92
93
94
# File 'lib/flox/entity.rb', line 92

def updated_at
  Time.parse self['updatedAt']
end

Instance Method Details

#inspectString

Returns provides a simple string representation of the Entity.

Returns:

  • (String)

    provides a simple string representation of the Entity.



73
74
75
76
77
78
79
# File 'lib/flox/entity.rb', line 73

def inspect
  description = "[#{self.class} #{@id} (#{@type})\n"
  each_pair do |key, value|
    description += "    #{key}: #{value}\n"
  end
  description += "]"
end