Class: SimpleRepresenter::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_representer/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(representer, collection = [], options = {}) ⇒ Collection

Returns a new instance of Collection.



9
10
11
12
13
# File 'lib/simple_representer/collection.rb', line 9

def initialize(representer, collection = [], options = {})
  @representer = representer
  @collection = collection
  @options = options
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



7
8
9
# File 'lib/simple_representer/collection.rb', line 7

def collection
  @collection
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/simple_representer/collection.rb', line 7

def options
  @options
end

#representerObject (readonly)

Returns the value of attribute representer.



7
8
9
# File 'lib/simple_representer/collection.rb', line 7

def representer
  @representer
end

Instance Method Details

#to_hObject Also known as: to_hash



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

def to_h
  collection.map { |elem| representer.new(elem, **options).to_h }
end

#to_json(*_args) ⇒ Object



20
21
22
# File 'lib/simple_representer/collection.rb', line 20

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