Class: BreweryDB::Collection

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

Constant Summary collapse

BATCH_SIZE =
50

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Collection



10
11
12
# File 'lib/brewery_db/collection.rb', line 10

def initialize(response)
  self.response = response
end

Instance Attribute Details

#sizeObject (readonly) Also known as: length

Returns the value of attribute size.



7
8
9
# File 'lib/brewery_db/collection.rb', line 7

def size
  @size
end

Instance Method Details

#count(*args, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/brewery_db/collection.rb', line 14

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



24
25
26
27
28
29
30
31
32
# File 'lib/brewery_db/collection.rb', line 24

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