Class: GrapeClient::Collection
- Inherits:
-
Object
- Object
- GrapeClient::Collection
- Defined in:
- lib/grape_client/collection.rb
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
Instance Method Summary collapse
- #collect ⇒ Object
- #each(&_block) ⇒ Object
- #first ⇒ Object
-
#initialize(clazz, elements, headers = nil) ⇒ Collection
constructor
A new instance of Collection.
- #map(&_block) ⇒ Object
Constructor Details
#initialize(clazz, elements, headers = nil) ⇒ Collection
Returns a new instance of Collection.
5 6 7 8 9 |
# File 'lib/grape_client/collection.rb', line 5 def initialize(clazz, elements, headers = nil) @is_first_page = true @clazz = clazz update(elements, headers) end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
3 4 5 |
# File 'lib/grape_client/collection.rb', line 3 def count @count end |
Instance Method Details
#collect ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/grape_client/collection.rb', line 33 def collect result = [] per_page do |elements| result += elements end result end |
#each(&_block) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/grape_client/collection.rb', line 15 def each(&_block) per_page do |elements| elements.each do |attrs| yield @clazz.new(attrs) end end end |
#first ⇒ Object
11 12 13 |
# File 'lib/grape_client/collection.rb', line 11 def first @clazz.new(@elements.first) if @elements.any? end |
#map(&_block) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/grape_client/collection.rb', line 23 def map(&_block) result = [] per_page do |elements| result += elements.map do |attrs| yield @clazz.new(attrs) end end result end |