Module: Eipiai::Representable

Included in:
Api
Defined in:
lib/eipiai/models/representable.rb

Overview

Representable

Module used to make a model “representable”.

When including this module into a class, it is expected that a similar class with a suffix “Representer” exists.

Instance Method Summary collapse

Instance Method Details

#from_hash(hash = {}) ⇒ Object

from_hash

call ‘#from_hash` on the representer belonging to the object.

Examples:

item = Item.new.extend(Eipiai::Representable)
item.from_hash('uid' => 'hello').uid # => 'hello'

Parameters:

  • hash (Hash) (defaults to: {})

    to be used to set the object attributes

Returns:

  • (Object)

    the object itself



51
52
53
# File 'lib/eipiai/models/representable.rb', line 51

def from_hash(hash = {})
  represented.from_hash(hash)
end

#pathString

path

The “path” to access the model. This method calls the representer’s ‘path` method.

Examples:

item = Item.new(uid: 'hello').extend(Eipiai::Representable)
item.path # => '/item/hello'

Returns:

  • (String)

    path to the objects resource



66
67
68
# File 'lib/eipiai/models/representable.rb', line 66

def path
  represented.path
end

#representedObject

represented

Return the represented version of the object.

Examples:

item = Item.new.extend(Eipiai::Representable)
item.represented.class # => ItemRepresenter

Returns:

  • (Object)

    Representer instance based on current model



21
22
23
# File 'lib/eipiai/models/representable.rb', line 21

def represented
  "#{self.class.name}Representer".constantize.new(self)
end

#to_h(options = {}) ⇒ Hash

to_h

call ‘#to_h` on the representer belonging to the object.

Examples:

item = Item.new.extend(Eipiai::Representable)
item.to_h # => { '_links' => { 'self' => { 'href' => '/item' } } }

Parameters:

  • options (Hash) (defaults to: {})

    to be used inside the representer

Returns:

  • (Hash)

    hash representation of the object



36
37
38
# File 'lib/eipiai/models/representable.rb', line 36

def to_h(options = {})
  represented.to_h(options)
end