class HornOfPlenty::Paginator

Attributes

autopage[RW]
client[RW]
current_offset[R]
current_page[R]
items_per_page[R]
request[RW]

Public Class Methods

new(client:, request:, items_per_page: nil, current_page: nil, autopage: true) click to toggle source
# 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

Public Instance Methods

all() click to toggle source
# File lib/horn_of_plenty/paginator.rb, line 31
def all
  items
end
each(_options = {}) { |item| ... } click to toggle source
# 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_page() { |items| ... } click to toggle source
# 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_page() click to toggle source
# 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) click to toggle source
# 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_page() click to toggle source
# File lib/horn_of_plenty/paginator.rb, line 59
def go_to_previous_page
  go_to_page(current_page - 1)
end
items() click to toggle source
# File lib/horn_of_plenty/paginator.rb, line 69
def items
  @items ||= retrieval_result.result
end

Protected Instance Methods

current_page=(value) click to toggle source
# File lib/horn_of_plenty/paginator.rb, line 87
def current_page=(value)
  @current_page     = value.to_i
  @current_offset   = (@current_page - 1) * items_per_page
  @retrieval_result = nil
  @items            = nil
end
items_per_page=(value) click to toggle source
# File lib/horn_of_plenty/paginator.rb, line 82
def items_per_page=(value)
  @items_per_page   = value.to_i
  @retrieval_result = nil
end

Private Instance Methods

pagination_parameters() click to toggle source
# File lib/horn_of_plenty/paginator.rb, line 102
def pagination_parameters
  {
    limit:  items_per_page,
    offset: current_offset,
  }
end
retrieval_result() click to toggle source
# File lib/horn_of_plenty/paginator.rb, line 96
def retrieval_result
  request.pagination = pagination_parameters

  client.request(request)
end