Class: Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/gems-cli/paginator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Paginator

Returns a new instance of Paginator.



7
8
9
10
11
12
13
14
# 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

Instance Attribute Details

#aryObject

Returns the value of attribute ary.



5
6
7
# File 'lib/gems-cli/paginator.rb', line 5

def ary
  @ary
end

#pageObject

Returns the value of attribute page.



5
6
7
# File 'lib/gems-cli/paginator.rb', line 5

def page
  @page
end

#paged_resultsObject

Returns the value of attribute paged_results.



5
6
7
# File 'lib/gems-cli/paginator.rb', line 5

def paged_results
  @paged_results
end

#pagesObject (readonly)

Returns the value of attribute pages.



4
5
6
# File 'lib/gems-cli/paginator.rb', line 4

def pages
  @pages
end

#per_pageObject

Returns the value of attribute per_page.



5
6
7
# File 'lib/gems-cli/paginator.rb', line 5

def per_page
  @per_page
end

#total_entriesObject (readonly)

Returns the value of attribute total_entries.



4
5
6
# File 'lib/gems-cli/paginator.rb', line 4

def total_entries
  @total_entries
end

Instance Method Details

#[](*args) ⇒ Object



16
17
18
# File 'lib/gems-cli/paginator.rb', line 16

def [](*args)
  @ary[*args]
end

#calculate_paginationObject

run calculation if you’ve initialized without arguments



21
22
23
24
25
# 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_pageObject

return the current page of results



48
49
50
# File 'lib/gems-cli/paginator.rb', line 48

def current_page
  @ary.paginate(per_page: @per_page, page: @page)
end

#first_pageObject

return the first page of results



38
39
40
# File 'lib/gems-cli/paginator.rb', line 38

def first_page
  @ary.paginate(per_page: @per_page, page: 1)
end

#first_page?Boolean

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

Returns:

  • (Boolean)


33
34
35
# File 'lib/gems-cli/paginator.rb', line 33

def first_page?
  @page.eql? 1
end

#last_pageObject

return the last page of results



43
44
45
# File 'lib/gems-cli/paginator.rb', line 43

def last_page
  @ary.paginate(per_page: @per_page, page: @pages)
end

#last_page?Boolean

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

Returns:

  • (Boolean)


28
29
30
# File 'lib/gems-cli/paginator.rb', line 28

def last_page?
  @page.eql? @pages
end

#next_pageObject

move the page index forward



53
54
55
56
# 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_pageObject

move the page index back



59
60
61
62
# 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