class SimpleRepresenter::Representer

Attributes

options[R]
represented[R]

Public Class Methods

for_collection(collection, **options) click to toggle source
# File lib/simple_representer/representer.rb, line 32
def self.for_collection(collection, **options)
  Collection.new(self, collection, options)
end
new(represented, **options) click to toggle source
# 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

Public Instance Methods

to_h() click to toggle source

return hash with symbols as keys

# File lib/simple_representer/representer.rb, line 21
def to_h
  build_result do |obj, result|
    obj[result[0]] = result[1]
  end
end
Also aliased as: to_hash
to_hash()
Alias for: to_h
to_json(*_args) click to toggle source
# File lib/simple_representer/representer.rb, line 28
def to_json(*_args)
  ::Oj.dump(to_h, { mode: :compat })
end

Private Instance Methods

build_result() { |obj, result| ... } click to toggle source
# File lib/simple_representer/representer.rb, line 38
def build_result
  self.class.definitions.each_with_object({}) do |definition, obj|
    result = definition.call(self)
    next unless result

    yield obj, result
  end
end