Class: Pagy::Countless

Inherits:
Pagy
  • Object
show all
Defined in:
lib/pagy/countless.rb,
lib/pagy/extras/overflow.rb

Constant Summary

Constants inherited from Pagy

COUNTLESS, ELASTICSEARCH_RAILS, I18n, OVERFLOW, SEARCHKICK, VARS, VERSION

Instance Attribute Summary

Attributes inherited from Pagy

#count, #from, #items, #last, #next, #offset, #page, #pages, #prev, #to, #vars

Instance Method Summary collapse

Methods inherited from Pagy

#initialize_with_overflow, new_from_elasticsearch_rails, new_from_searchkick, #overflow?, root, #sequels, #series

Constructor Details

#initialize(vars = {}) ⇒ Countless

Merge and validate the options, do some simple arithmetic and set a few instance variables



11
12
13
14
15
16
17
18
# File 'lib/pagy/countless.rb', line 11

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_with_overflow(items) ⇒ Object Also known as: finalize



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pagy/extras/overflow.rb', line 52

def finalize_with_overflow(items)
  @overflow = false
  finalize_without_overflow(items)
rescue OverflowError
  @overflow = true                            # add the overflow flag
  case @vars[:overflow]
  when :exception
    raise                                     # same as without the extra
  when :empty_page
    @offset = @items = @from = @to = 0        # vars relative to the actual page
    @vars[:size] = []                         # no page in the series
    self
  else
    raise ArgumentError, "expected :overflow variable in [:empty_page, :exception]; got #{@vars[:overflow].inspect}"
  end
end