Class: Plucky::Pagination::Paginator
- Defined in:
- lib/plucky/pagination/paginator.rb
Instance Attribute Summary collapse
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
-
#per_page ⇒ Object
(also: #limit)
readonly
Returns the value of attribute per_page.
-
#total_entries ⇒ Object
readonly
Returns the value of attribute total_entries.
Instance Method Summary collapse
-
#initialize(total, page, per_page = nil) ⇒ Paginator
constructor
A new instance of Paginator.
- #next_page ⇒ Object
- #out_of_bounds? ⇒ Boolean
- #previous_page ⇒ Object
- #skip ⇒ Object (also: #offset)
- #total_pages ⇒ Object
Constructor Details
#initialize(total, page, per_page = nil) ⇒ Paginator
Returns a new instance of Paginator.
7 8 9 10 11 |
# File 'lib/plucky/pagination/paginator.rb', line 7 def initialize(total, page, per_page=nil) @total_entries = total.to_i @current_page = [page.to_i, 1].max @per_page = (per_page || 25).to_i end |
Instance Attribute Details
#current_page ⇒ Object (readonly)
Returns the value of attribute current_page.
5 6 7 |
# File 'lib/plucky/pagination/paginator.rb', line 5 def current_page @current_page end |
#per_page ⇒ Object (readonly) Also known as: limit
Returns the value of attribute per_page.
5 6 7 |
# File 'lib/plucky/pagination/paginator.rb', line 5 def per_page @per_page end |
#total_entries ⇒ Object (readonly)
Returns the value of attribute total_entries.
5 6 7 |
# File 'lib/plucky/pagination/paginator.rb', line 5 def total_entries @total_entries end |
Instance Method Details
#next_page ⇒ Object
25 26 27 |
# File 'lib/plucky/pagination/paginator.rb', line 25 def next_page current_page < total_pages ? (current_page + 1) : nil end |
#out_of_bounds? ⇒ Boolean
17 18 19 |
# File 'lib/plucky/pagination/paginator.rb', line 17 def out_of_bounds? current_page > total_pages end |
#previous_page ⇒ Object
21 22 23 |
# File 'lib/plucky/pagination/paginator.rb', line 21 def previous_page current_page > 1 ? (current_page - 1) : nil end |
#skip ⇒ Object Also known as: offset
29 30 31 |
# File 'lib/plucky/pagination/paginator.rb', line 29 def skip (current_page - 1) * per_page end |
#total_pages ⇒ Object
13 14 15 |
# File 'lib/plucky/pagination/paginator.rb', line 13 def total_pages (total_entries / per_page.to_f).ceil end |