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

Constructor Details

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

Returns a new instance of Collection.



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

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



56
57
58
# File 'lib/nagare.rb', line 56

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

.attributes(*attributes) ⇒ Object



64
65
66
# File 'lib/nagare.rb', line 64

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

.key(key) ⇒ Object



60
61
62
# File 'lib/nagare.rb', line 60

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

Instance Method Details

#as_json(options = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/nagare.rb', line 80

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

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

#eachObject



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

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