Class: Page
- Inherits:
-
Object
- Object
- Page
- Defined in:
- lib/rango/contrib/pagination/page.rb
Class Attribute Summary collapse
Instance Attribute Summary collapse
- #count ⇒ Object readonly
-
#number(type = :human) ⇒ Object
NOTE: offset start as normal array from 0, but page from 1!.
- #per_page ⇒ Object readonly
Class Method Summary collapse
-
.register_route_hook(callable = nil, &block) ⇒ Object
register_route_hook do …
- .route(request, page, int) ⇒ Object
Instance Method Summary collapse
- #first ⇒ Object
- #first? ⇒ Boolean
-
#initialize(params) ⇒ Page
constructor
Page.new(params, count, per_page) def initialize(current_page, count, per_page).
- #last ⇒ Object
- #last? ⇒ Boolean
-
#max ⇒ Object
Count of pages.
-
#next ⇒ Object
Current plus 1 or nil if it’ the last page.
-
#previous ⇒ Object
Current minus 1 or nil if it’ the first page.
Constructor Details
#initialize(params) ⇒ Page
Page.new(params, count, per_page) def initialize(current_page, count, per_page)
37 38 39 40 41 42 43 44 45 |
# File 'lib/rango/contrib/pagination/page.rb', line 37 def initialize(params) raise ArgumentError unless params.is_a?(Hash) @count = params[:count].to_i || raise(ArgumentError, "params[:count] must be given") @per_page = (params[:per_page] or 10).to_i # can be nil @number = (params[:current].nil? ? 1 : params[:current].to_i) @number = 1 if @number < 1 @number = max if @number > max end |
Class Attribute Details
.per_page ⇒ Object
10 11 12 |
# File 'lib/rango/contrib/pagination/page.rb', line 10 def per_page @per_page ||= 10 end |
.route_hook ⇒ Object
6 7 8 |
# File 'lib/rango/contrib/pagination/page.rb', line 6 def route_hook @route_hook end |
Instance Attribute Details
#count ⇒ Object (readonly)
32 33 34 |
# File 'lib/rango/contrib/pagination/page.rb', line 32 def count @count end |
#number(type = :human) ⇒ Object
NOTE: offset start as normal array from 0, but page from 1!
29 30 31 |
# File 'lib/rango/contrib/pagination/page.rb', line 29 def number @number end |
#per_page ⇒ Object (readonly)
32 33 34 |
# File 'lib/rango/contrib/pagination/page.rb', line 32 def per_page @per_page end |
Class Method Details
.register_route_hook(callable = nil, &block) ⇒ Object
register_route_hook do … end OR register_route_hook method(:foo)
18 19 20 21 |
# File 'lib/rango/contrib/pagination/page.rb', line 18 def register_route_hook(callable = nil, &block) callable = (callable.nil? ? block : callable) self.route_hook = callable end |
.route(request, page, int) ⇒ Object
23 24 25 |
# File 'lib/rango/contrib/pagination/page.rb', line 23 def route(request, page, int) self.route_hook.call(request, page, int) end |
Instance Method Details
#first ⇒ Object
90 91 92 |
# File 'lib/rango/contrib/pagination/page.rb', line 90 def first Page.new(current: 1, count: @count, per_page: @per_page) end |
#first? ⇒ Boolean
95 96 97 |
# File 'lib/rango/contrib/pagination/page.rb', line 95 def first? self.number == 1 end |
#last ⇒ Object
85 86 87 |
# File 'lib/rango/contrib/pagination/page.rb', line 85 def last Page.new(current: max, count: @count, per_page: @per_page) end |
#last? ⇒ Boolean
100 101 102 |
# File 'lib/rango/contrib/pagination/page.rb', line 100 def last? self.number == max end |
#max ⇒ Object
Count of pages
60 61 62 63 64 65 66 |
# File 'lib/rango/contrib/pagination/page.rb', line 60 def max if @count % @per_page == 0 @max ||= (@count / @per_page) else @max ||= (@count / @per_page) + 1 end end |