Class: HornOfPlenty::Paginator

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

Instance Method Summary collapse

Constructor Details

#initialize(client:, request:, items_per_page: nil, current_page: nil, autopage: true) ⇒ Paginator

Returns a new instance of Paginator.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/horn_of_plenty/paginator.rb', line 17

def initialize(client:,
               request:,
               items_per_page: nil,
               current_page:   nil,
               autopage:       true)

  self.client         = client
  self.request        = request

  self.items_per_page = items_per_page || 500
  self.current_page   = current_page   || 1
  self.autopage       = autopage
end

Instance Method Details

#allObject



31
32
33
# File 'lib/horn_of_plenty/paginator.rb', line 31

def all
  items
end

#each(_options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/horn_of_plenty/paginator.rb', line 35

def each(_options = {})
  loop do
    items.each do |item|
      yield item
    end

    break unless autopage && go_to_next_page
  end
end

#each_pageObject



45
46
47
48
49
50
51
# File 'lib/horn_of_plenty/paginator.rb', line 45

def each_page
  loop do
    yield items

    break unless go_to_next_page
  end
end

#go_to_next_pageObject



53
54
55
56
57
# File 'lib/horn_of_plenty/paginator.rb', line 53

def go_to_next_page
  return false if items.count < items_per_page

  go_to_page(current_page + 1)
end

#go_to_page(page_number) ⇒ Object



63
64
65
66
67
# File 'lib/horn_of_plenty/paginator.rb', line 63

def go_to_page(page_number)
  page_number = 1 if page_number < 1

  self.current_page = page_number
end

#go_to_previous_pageObject



59
60
61
# File 'lib/horn_of_plenty/paginator.rb', line 59

def go_to_previous_page
  go_to_page(current_page - 1)
end

#itemsObject



69
70
71
# File 'lib/horn_of_plenty/paginator.rb', line 69

def items
  @items ||= retrieval_result.result
end