lib/pagy.rb in pagy-0.9.0 vs lib/pagy.rb in pagy-0.9.1

- old
+ new

@@ -1,18 +1,18 @@ # See Pagy API documentation: https://ddnexus.github.io/pagy/api/pagy require 'pathname' -class Pagy ; VERSION = '0.9.0' +class Pagy ; VERSION = '0.9.1' - class OutOfRangeError < StandardError; end + class OutOfRangeError < StandardError; attr_reader :pagy; def initialize(pagy) @pagy = pagy end; end # Root pathname to get the path of Pagy files like templates or dictionaries def self.root; Pathname.new(__FILE__).dirname end # default vars - VARS = { page:1, items:20, outset:0, size:[1,4,4,1], page_param: :page, params: {}, anchor: ''.freeze, link_extra: ''.freeze, item_path: 'pagy.info.item_name'.freeze } + VARS = { page:1, items:20, outset:0, size:[1,4,4,1], page_param: :page, params:{}, anchor:''.freeze, link_extra:''.freeze, item_path:'pagy.info.item_name'.freeze } attr_reader :count, :page, :items, :vars, :pages, :last, :offset, :from, :to, :prev, :next # Merge and validate the options, do some simple aritmetic and set the instance variables def initialize(vars) @@ -20,10 +20,10 @@ { count:0, 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 #{instance_variable_get(:"@#{k}").inspect}") end @pages = @last = [(@count.to_f / @items).ceil, 1].max # cardinal and ordinal meanings - @page >= 1 && @page <= @last or raise(OutOfRangeError, "expected :page in 1..#{@last}; got #{@page.inspect}") + @page >= 1 && @page <= @last or raise(OutOfRangeError.new(self), "expected :page in 1..#{@last}; got #{@page.inspect}") @offset = @items * (@page - 1) + @outset # pagination offset + outset (initial offset) @items = @count - ((@pages-1) * @items) if @page == @last # adjust items for last page @from = @count == 0 ? 0 : @offset+1 - @outset # page begins from item @to = @offset + @items - @outset # page ends to item @prev = (@page-1 unless @page == 1) # nil if no prev page