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, #attributes, inherited, #method_missing

Constructor Details

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

Returns a new instance of Collection.



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

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



62
63
64
# File 'lib/nagare.rb', line 62

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

.attributes(*attributes) ⇒ Object



70
71
72
# File 'lib/nagare.rb', line 70

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

.key(key) ⇒ Object



66
67
68
# File 'lib/nagare.rb', line 66

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

Instance Method Details

#as_json(options = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/nagare.rb', line 86

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

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

#eachObject



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

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