class Paginator

Attributes

ary[RW]
page[RW]
paged_results[RW]
pages[R]
per_page[RW]
total_entries[R]

Public Class Methods

new(args = {}) click to toggle source
# File lib/gems-cli/paginator.rb, line 7
def initialize(args = {})
  @ary = args[:ary] || []
  @page = 1
  @per_page = args[:per_page] || 10
  @paged_results = @ary.paginate(per_page: @per_page)
  @size = @paged_results.total_entries
  @pages = @paged_results.total_pages
end

Public Instance Methods

[](*args) click to toggle source
# File lib/gems-cli/paginator.rb, line 16
def [](*args)
  @ary[*args]
end
calculate_pagination() click to toggle source

run calculation if you've initialized without arguments

# File lib/gems-cli/paginator.rb, line 21
def calculate_pagination
  @paged_results = @ary.paginate(per_page: @per_page)
  @size = @paged_results.total_entries
  @pages = @paged_results.total_pages
end
current_page() click to toggle source

return the current page of results

# File lib/gems-cli/paginator.rb, line 48
def current_page
  @ary.paginate(per_page: @per_page, page: @page)
end
first_page() click to toggle source

return the first page of results

# File lib/gems-cli/paginator.rb, line 38
def first_page
  @ary.paginate(per_page: @per_page, page: 1)
end
first_page?() click to toggle source

test if the current page is on the first page of results

# File lib/gems-cli/paginator.rb, line 33
def first_page?
  @page.eql? 1
end
last_page() click to toggle source

return the last page of results

# File lib/gems-cli/paginator.rb, line 43
def last_page
  @ary.paginate(per_page: @per_page, page: @pages)
end
last_page?() click to toggle source

test if the current page is on the last page of results

# File lib/gems-cli/paginator.rb, line 28
def last_page?
  @page.eql? @pages
end
next_page() click to toggle source

move the page index forward

# File lib/gems-cli/paginator.rb, line 53
def next_page
  @page += 1 unless last_page?
  @ary.paginate(per_page: @per_page, page: @page)
end
previous_page() click to toggle source

move the page index back

# File lib/gems-cli/paginator.rb, line 59
def previous_page
  @page -= 1 unless first_page?
  @ary.paginate(per_page: @per_page, page: @page)
end