Class: Paginator
- Inherits:
-
Object
- Object
- Paginator
- Includes:
- Enumerable
- Defined in:
- lib/core_extended/paginator.rb
Instance Attribute Summary collapse
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #each(&block) ⇒ Object
- #first? ⇒ Boolean
-
#initialize(collection, options = {}) ⇒ Paginator
constructor
A new instance of Paginator.
- #last? ⇒ Boolean
- #pages ⇒ Object
- #total ⇒ Object
Constructor Details
#initialize(collection, options = {}) ⇒ Paginator
Returns a new instance of Paginator.
7 8 9 10 11 |
# File 'lib/core_extended/paginator.rb', line 7 def initialize(collection, ={}) @collection = collection @page = [:page] ? [:page].to_i : 1 @size = [:size] ? [:size].to_i : 10 end |
Instance Attribute Details
#page ⇒ Object (readonly)
Returns the value of attribute page.
5 6 7 |
# File 'lib/core_extended/paginator.rb', line 5 def page @page end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
5 6 7 |
# File 'lib/core_extended/paginator.rb', line 5 def size @size end |
Instance Method Details
#each(&block) ⇒ Object
13 14 15 16 |
# File 'lib/core_extended/paginator.rb', line 13 def each(&block) from = size * (page - 1) (@collection.slice(from, size) || []).each(&block) end |
#first? ⇒ Boolean
26 27 28 |
# File 'lib/core_extended/paginator.rb', line 26 def first? page == 1 end |
#last? ⇒ Boolean
30 31 32 |
# File 'lib/core_extended/paginator.rb', line 30 def last? page == pages end |
#pages ⇒ Object
22 23 24 |
# File 'lib/core_extended/paginator.rb', line 22 def pages (total / size.to_f).ceil end |
#total ⇒ Object
18 19 20 |
# File 'lib/core_extended/paginator.rb', line 18 def total @collection.count end |