Class: Nagare::Collection

Inherits:
Item
  • Object
show all
Includes:
Enumerable
Defined in:
lib/nagare.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Item

_attributes, _options, #attributes, inherited, #method_missing, options

Constructor Details

#initialize(collection, context, serializer:) ⇒ Collection

Returns a new instance of Collection.



88
89
90
91
92
# File 'lib/nagare.rb', line 88

def initialize(collection, context, serializer:)
  @collection = collection
  @context = context
  @serializer = serializer
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Nagare::Item

Class Method Details

._keyObject



76
77
78
# File 'lib/nagare.rb', line 76

def self._key
  @key || "items"
end

.attributes(*attributes) ⇒ Object



84
85
86
# File 'lib/nagare.rb', line 84

def self.attributes(*attributes)
  @attributes = _attributes.concat(attributes)
end

.key(key) ⇒ Object



80
81
82
# File 'lib/nagare.rb', line 80

def self.key(key)
  @key = key
end

Instance Method Details

#as_json(options = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/nagare.rb', line 100

def as_json(options = nil)
  items = map do |item|
    item.as_json
  end

  attributes.merge({
    self.class._key => items,
  })
end

#eachObject



94
95
96
97
98
# File 'lib/nagare.rb', line 94

def each
  collection.map do |item|
    yield serializer.new(item, context)
  end
end