Class: SimpleRepresenter::Representer

Inherits:
Object
  • Object
show all
Includes:
Definable
Defined in:
lib/simple_representer/representer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Definable

included

Constructor Details

#initialize(represented, **options) ⇒ Representer

Returns a new instance of Representer.



15
16
17
18
# File 'lib/simple_representer/representer.rb', line 15

def initialize(represented, **options)
  @represented = represented.is_a?(Hash) ? represented.clone.extend(CallableHash) : represented
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/simple_representer/representer.rb', line 13

def options
  @options
end

#representedObject (readonly)

Returns the value of attribute represented.



13
14
15
# File 'lib/simple_representer/representer.rb', line 13

def represented
  @represented
end

Class Method Details

.for_collection(collection, **options) ⇒ Object



32
33
34
# File 'lib/simple_representer/representer.rb', line 32

def self.for_collection(collection, **options)
  Collection.new(self, collection, options)
end

Instance Method Details

#to_hObject Also known as: to_hash

return hash with symbols as keys



21
22
23
24
25
# File 'lib/simple_representer/representer.rb', line 21

def to_h
  build_result do |obj, result|
    obj[result[0]] = result[1]
  end
end

#to_json(*_args) ⇒ Object



28
29
30
# File 'lib/simple_representer/representer.rb', line 28

def to_json(*_args)
  ::Oj.generate(to_h)
end