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

id[RW]

@return [String] the primary identifier of the entity.

type[R]

@return [String] the type of the entity. Types group entities together on the server.

Public Class Methods

new(type, id=nil, data=nil) click to toggle source

@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

[](key) click to toggle source

Accesses a property of the entity; both symbols and strings work.

Calls superclass method
# File lib/flox/entity.rb, line 85
def [](key)
  super(key.to_sym)
end
[]=(key, value) click to toggle source

(see [])

Calls superclass method
# File lib/flox/entity.rb, line 90
def []=(key, value)
  super(key.to_sym, value)
end
created_at() click to toggle source
# File lib/flox/entity.rb, line 47
def created_at
  Time.parse self[:createdAt]
end
inspect() click to toggle source

@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
owner_id() click to toggle source
# File lib/flox/entity.rb, line 63
def owner_id
  self[:ownerId]
end
owner_id=(value) click to toggle source
# File lib/flox/entity.rb, line 67
def owner_id=(value)
  self[:ownerId] = value.to_s
end
path() click to toggle source
# File lib/flox/entity.rb, line 71
def path
  "entities/#{@type}/#{@id}"
end
public_access() click to toggle source
# File lib/flox/entity.rb, line 55
def public_access
  self[:publicAccess]
end
public_access=(access) click to toggle source
# File lib/flox/entity.rb, line 59
def public_access=(access)
  self[:publicAccess] = access.to_s
end
updated_at() click to toggle source
# File lib/flox/entity.rb, line 51
def updated_at
  Time.parse self[:updatedAt]
end