Class: Paginator
- Inherits:
-
Object
- Object
- Paginator
- Defined in:
- lib/gems-cli/paginator.rb
Instance Attribute Summary collapse
-
#ary ⇒ Object
Returns the value of attribute ary.
-
#page ⇒ Object
Returns the value of attribute page.
-
#paged_results ⇒ Object
Returns the value of attribute paged_results.
-
#pages ⇒ Object
readonly
Returns the value of attribute pages.
-
#per_page ⇒ Object
Returns the value of attribute per_page.
-
#total_entries ⇒ Object
readonly
Returns the value of attribute total_entries.
Instance Method Summary collapse
- #[](*args) ⇒ Object
-
#calculate_pagination ⇒ Object
run calculation if you’ve initialized without arguments.
-
#current_page ⇒ Object
return the current page of results.
-
#first_page ⇒ Object
return the first page of results.
-
#first_page? ⇒ Boolean
test if the current page is on the first page of results.
-
#initialize(args = {}) ⇒ Paginator
constructor
A new instance of Paginator.
-
#last_page ⇒ Object
return the last page of results.
-
#last_page? ⇒ Boolean
test if the current page is on the last page of results.
-
#next_page ⇒ Object
move the page index forward.
-
#previous_page ⇒ Object
move the page index back.
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
#ary ⇒ Object
Returns the value of attribute ary.
5 6 7 |
# File 'lib/gems-cli/paginator.rb', line 5 def ary @ary end |
#page ⇒ Object
Returns the value of attribute page.
5 6 7 |
# File 'lib/gems-cli/paginator.rb', line 5 def page @page end |
#paged_results ⇒ Object
Returns the value of attribute paged_results.
5 6 7 |
# File 'lib/gems-cli/paginator.rb', line 5 def paged_results @paged_results end |
#pages ⇒ Object (readonly)
Returns the value of attribute pages.
4 5 6 |
# File 'lib/gems-cli/paginator.rb', line 4 def pages @pages end |
#per_page ⇒ Object
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_entries ⇒ Object (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_pagination ⇒ Object
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_page ⇒ Object
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_page ⇒ Object
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
33 34 35 |
# File 'lib/gems-cli/paginator.rb', line 33 def first_page? @page.eql? 1 end |
#last_page ⇒ Object
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
28 29 30 |
# File 'lib/gems-cli/paginator.rb', line 28 def last_page? @page.eql? @pages end |
#next_page ⇒ Object
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_page ⇒ Object
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 |