Class: Fish0::Paginator
- Inherits:
-
Object
- Object
- Fish0::Paginator
- Includes:
- Enumerable
- Defined in:
- lib/fish0/paginator.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
Instance Method Summary collapse
- #current_page ⇒ Object
-
#initialize(collection, page_number: 1, per_page: 22, padding: 0) ⇒ Paginator
constructor
A new instance of Paginator.
- #last_page? ⇒ Boolean
- #padding(value) ⇒ Object
- #page(value = @page) ⇒ Object
- #pages ⇒ Object
- #per(value = @per) ⇒ Object
- #total ⇒ Object
Constructor Details
#initialize(collection, page_number: 1, per_page: 22, padding: 0) ⇒ Paginator
Returns a new instance of Paginator.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/fish0/paginator.rb', line 8 def initialize(collection, page_number: 1, per_page: 22, padding: 0) raise ArgumentError, 'you can paginate only Fish0::Repository' unless collection.is_a?(Fish0::Repository) @collection = collection @per = per_page @page = page_number @padding = padding per(per_page) page(page_number) end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
6 7 8 |
# File 'lib/fish0/paginator.rb', line 6 def collection @collection end |
Instance Method Details
#current_page ⇒ Object
43 44 45 |
# File 'lib/fish0/paginator.rb', line 43 def current_page @page end |
#last_page? ⇒ Boolean
51 52 53 |
# File 'lib/fish0/paginator.rb', line 51 def last_page? current_page >= pages end |
#padding(value) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/fish0/paginator.rb', line 32 def padding(value) @padding = value page skip self end |
#page(value = @page) ⇒ Object
19 20 21 22 23 |
# File 'lib/fish0/paginator.rb', line 19 def page(value = @page) @page = value skip((@page - 1) * @per + @padding) self end |
#pages ⇒ Object
47 48 49 |
# File 'lib/fish0/paginator.rb', line 47 def pages (total.to_f / @per).ceil end |
#per(value = @per) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/fish0/paginator.rb', line 25 def per(value = @per) @per = value skip((@page - 1) * @per + @padding) limit(@per) self end |
#total ⇒ Object
39 40 41 |
# File 'lib/fish0/paginator.rb', line 39 def total collection.count end |