Class: Pagy::Countless
- Includes:
- CountlessOverflow
- Defined in:
- lib/pagy/countless.rb
Constant Summary
Constants inherited from Pagy
Instance Attribute Summary
Attributes inherited from Pagy
#count, #from, #items, #last, #next, #offset, #page, #pages, #prev, #to, #vars
Instance Method Summary collapse
-
#finalize(fetched) ⇒ Object
Finalize the instance variables based on the fetched items.
-
#initialize(vars = {}) ⇒ Countless
constructor
Merge and validate the options, do some simple arithmetic and set a few instance variables.
Methods inherited from Pagy
#overflow?, #responsive, root, #series, #to_h
Constructor Details
#initialize(vars = {}) ⇒ Countless
Merge and validate the options, do some simple arithmetic and set a few instance variables
8 9 10 11 12 13 14 15 |
# File 'lib/pagy/countless.rb', line 8 def initialize(vars={}) @vars ||= VARS.merge(vars.delete_if{|_,v| v.nil? || v == '' }) # default vars + cleaned vars (can be overridden) { items:1, outset:0, page:1 }.each do |k,min| # validate instance variables (@vars[k] && instance_variable_set(:"@#{k}", @vars[k].to_i) >= min) \ or raise(ArgumentError, "expected :#{k} >= #{min}; got #{@vars[k].inspect}") end @offset = @items * (@page - 1) + @outset # pagination offset + outset (initial offset) end |
Instance Method Details
#finalize(fetched) ⇒ Object
Finalize the instance variables based on the fetched items
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/pagy/countless.rb', line 18 def finalize(fetched) fetched == 0 && @page > 1 and raise(OverflowError.new(self), "page #{@page} got no items") @pages = @last = (fetched > @items ? @page + 1 : @page) # set the @pages and @last @items = fetched if fetched < @items && fetched > 0 # adjust items for last non-empty page @from = fetched == 0 ? 0 : @offset+1 - @outset # page begins from item @to = fetched == 0 ? 0 : @offset + @items - @outset # page ends to item @prev = (@page-1 unless @page == 1) # nil if no prev page @next = @page == @last ? (1 if @vars[:cycle]) : @page+1 # nil if no next page, 1 if :cycle self end |