Class: BreweryDB::PaginatedCollection

Inherits:
Collection
  • Object
show all
Includes:
Enumerable
Defined in:
lib/brewery_db/paginated_collection.rb

Constant Summary collapse

BATCH_SIZE =
50

Instance Attribute Summary

Attributes inherited from Collection

#size

Instance Method Summary collapse

Methods inherited from Collection

#initialize

Constructor Details

This class inherits a constructor from BreweryDB::Collection

Instance Method Details

#count(*args, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/brewery_db/paginated_collection.rb', line 7

def count(*args, &block)
  # The Ruby documentation is wrong, and Enumerable#count no longer calls
  # #size, so let's make sure it's used here when no arguments are given.
  if args.empty? && !block_given?
    size
  else
    super
  end
end

#eachObject



17
18
19
20
21
22
23
24
25
# File 'lib/brewery_db/paginated_collection.rb', line 17

def each
  return to_enum unless block_given?

  while @collection.any?
    @collection.each { |element| yield(element) }
    break if @collection.size < BATCH_SIZE
    self.response = @response.next_page
  end
end