lib/pagy.rb in pagy-1.0.0 vs lib/pagy.rb in pagy-1.1.0

- old
+ new

@@ -1,11 +1,11 @@ # See Pagy API documentation: https://ddnexus.github.io/pagy/api/pagy # frozen_string_literal: true require 'pathname' -class Pagy ; VERSION = '1.0.0' +class Pagy ; VERSION = '1.1.0' class OverflowError < 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 @@ -13,11 +13,11 @@ # default vars VARS = { page:1, items:20, outset:0, size:[1,4,4,1], page_param: :page, params:{}, anchor:'', link_extra:'', item_path:'pagy.info.item_name' } 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 + # Merge and validate the options, do some simple arithmetic and set the instance variables def initialize(vars) @vars = VARS.merge(vars.delete_if{|_,v| v.nil? || v == '' }) # default vars + cleaned vars { 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 #{@vars[k].inspect}") @@ -32,11 +32,11 @@ @next = (@page+1 unless @page == @last) # nil if no next page end # Return the array of page numbers and :gap items e.g. [1, :gap, 7, 8, "9", 10, 11, :gap, 36] def series(size=@vars[:size]) + (series = []) and size.empty? and return series 4.times{|i| (size[i]>=0 rescue nil) or raise(ArgumentError, "expected 4 items >= 0 in :size; got #{size.inspect}")} - series = [] [*0..size[0], *@page-size[1]..@page+size[2], *@last-size[3]+1..@last+1].sort!.each_cons(2) do |a, b| if a<0 || a==b || a>@last # skip out of range and duplicates elsif a+1 == b; series.push(a) # no gap -> no additions elsif a+2 == b; series.push(a, a+1) # 1 page gap -> fill with missing page else series.push(a, :gap) # n page gap -> add :gap