Class: Ippon::Paginator
- Inherits:
-
Object
- Object
- Ippon::Paginator
- Defined in:
- lib/ippon/paginator.rb
Overview
Paginator represents a pagination of a collection. You initiailize it with the total number of entries, how many entries you want per page, and the current page, and it provides various helpers for working with pages.
Instance Attribute Summary collapse
-
#current_page ⇒ Object
The current page number.
-
#entries_per_page ⇒ Object
(also: #limit)
readonly
The number of entries per page.
-
#total_entries ⇒ Object
readonly
The total number of entries.
Instance Method Summary collapse
- #each_page {|num| ... } ⇒ Object
-
#first_page ⇒ Integer
This always returns 1, but is provided to complement #last_page.
-
#first_page? ⇒ Boolean
True if the paginator is currently on the first page.
-
#initialize(total_entries, entries_per_page, current_page = 1) ⇒ Paginator
constructor
A new instance of Paginator.
-
#last_page ⇒ Integer
The page number of the last page.
-
#last_page? ⇒ Boolean
True if the paginator is currently on the last page.
-
#next_page ⇒ Integer?
The page number of the next page (if it exists).
-
#offset ⇒ Integer
Calculates the number of entries you need to skip in order to reach the current page.
-
#prev_page ⇒ Integer?
The page number of the previous page (if it exists).
Constructor Details
#initialize(total_entries, entries_per_page, current_page = 1) ⇒ Paginator
Returns a new instance of Paginator.
60 61 62 63 64 |
# File 'lib/ippon/paginator.rb', line 60 def initialize(total_entries, entries_per_page, current_page = 1) @total_entries = total_entries @entries_per_page = entries_per_page self.current_page = current_page end |
Instance Attribute Details
#current_page ⇒ Object
The current page number. This value is always guaranteed to be within #first_page and #last_page.
58 59 60 |
# File 'lib/ippon/paginator.rb', line 58 def current_page @current_page end |
#entries_per_page ⇒ Object (readonly) Also known as: limit
54 55 56 |
# File 'lib/ippon/paginator.rb', line 54 def entries_per_page @entries_per_page end |
#total_entries ⇒ Object (readonly)
The total number of entries.
50 51 52 |
# File 'lib/ippon/paginator.rb', line 50 def total_entries @total_entries end |
Instance Method Details
#each_page {|num| ... } ⇒ Object
112 113 114 |
# File 'lib/ippon/paginator.rb', line 112 def each_page(&blk) (first_page .. last_page).each(&blk) end |
#first_page ⇒ Integer
This always returns 1, but is provided to complement #last_page.
81 82 83 |
# File 'lib/ippon/paginator.rb', line 81 def first_page 1 end |
#first_page? ⇒ Boolean
Returns true if the paginator is currently on the first page.
86 87 88 |
# File 'lib/ippon/paginator.rb', line 86 def first_page? current_page == first_page end |
#last_page ⇒ Integer
Returns the page number of the last page.
91 92 93 |
# File 'lib/ippon/paginator.rb', line 91 def last_page @last_page ||= [(total_entries / entries_per_page.to_f).ceil, first_page].max end |
#last_page? ⇒ Boolean
Returns true if the paginator is currently on the last page.
96 97 98 |
# File 'lib/ippon/paginator.rb', line 96 def last_page? current_page == last_page end |
#next_page ⇒ Integer?
Returns the page number of the next page (if it exists).
106 107 108 |
# File 'lib/ippon/paginator.rb', line 106 def next_page current_page + 1 unless last_page? end |
#offset ⇒ Integer
Calculates the number of entries you need to skip in order to reach the current page.
122 123 124 |
# File 'lib/ippon/paginator.rb', line 122 def offset (current_page - 1) * entries_per_page end |
#prev_page ⇒ Integer?
Returns the page number of the previous page (if it exists).
101 102 103 |
# File 'lib/ippon/paginator.rb', line 101 def prev_page current_page - 1 unless first_page? end |