Class: Tacokit::Collection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/tacokit/collection.rb

Constant Summary collapse

MAX =
1000

Instance Method Summary collapse

Constructor Details

#initialize(client, method, path, options) ⇒ Collection

Returns a new instance of Collection.


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/tacokit/collection.rb', line 12

def initialize(client, method, path, options)
  @client  = client
  @method  = method
  @path    = path
  @options = options

  @before = options.fetch(:before, nil)
  @limit  = options.fetch(:limit, 50)
  @max    = options.fetch(:max, MAX)

  @collection = []

  fetch_next_page
end

Instance Method Details

#each(start = 0) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tacokit/collection.rb', line 27

def each(start = 0)
  return to_enum(:each, start) unless block_given?
  Array(@collection[start..-1]).each do |element|
    yield(element)
  end
  unless last?
    start = [@collection.size, start].max
    fetch_next_page
    each(start, &Proc.new)
  end
  self
end