# See Pagy API documentation: https://ddnexus.github.io/pagy/docs/api/pagy# frozen_string_literal: truerequire'pathname'require_relative'pagy/shared_methods'# Top superclass: it should define only what's common to all the subclassesclassPagyVERSION='9.0.9'# Core default: constant for easy access, but mutable for customizable defaultsDEFAULT={count_args: [:all],# rubocop:disable Style/MutableConstantends: true,limit: 20,outset: 0,page: 1,page_param: :page,size: 7}# AR friendly# Gem root pathname to get the path of Pagy files stylesheets, javascripts, apps, locales, etc.defself.root@root||=Pathname.new(__dir__).parent.freezeendincludeSharedMethodsattr_reader:count,:from,:in,:last,:next,:offset,:prev,:toaliaspageslast# Merge and validate the options, do some simple arithmetic and set the instance variablesdefinitialize(**vars)assign_vars(DEFAULT,vars)assign_and_check(count: 0,page: 1,outset: 0)assign_limitassign_offsetassign_lastcheck_overflow@from=[@offset-@outset+1,@count].min@to=[@offset-@outset+@limit,@count].min@in=[@to-@from+1,@count].minassign_prev_and_nextend# Setup @last (overridden by the gearbox extra)defassign_last@last=[(@count.to_f/@limit).ceil,1].max@last=@vars[:max_pages]if@vars[:max_pages]&&@last>vars[:max_pages]end# Assign @offset (overridden by the gearbox extra)defassign_offset@offset=(@limit*(@page-1))+@outset# may be already set from gear_boxend# Assign @prev and @nextdefassign_prev_and_next@prev=(@page-1unless@page==1)@next=@page==@last?(1if@vars[:cycle]):@page+1end# Checks the @page <= @lastdefcheck_overflowraiseOverflowError.new(self,:page,"in 1..#{@last}",@page)if@page>@lastend# Label for the current page. Allow the customization of the output (overridden by the calendar extra)deflabel=@page.to_s# Label for any page. Allow the customization of the output (overridden by the calendar extra)deflabel_for(page)=page.to_s# Return the array of page numbers and :gap e.g. [1, :gap, 8, "9", 10, :gap, 36]defseries(size: @vars[:size],**_)raiseVariableError.new(self,:size,'to be an Integer >= 0',size)\unlesssize.is_a?(Integer)&&size>=0return[]ifsize.zero?[].tapdo|series|ifsize>=@lastseries.push(*1..@last)elseleft=((size-1)/2.0).floor# left half might be 1 page shorter for even sizestart=if@page<=left# beginning pages1elsif@page>(@last-size+left)# end pages@last-size+1else# intermediate pages@page-leftendseries.push(*start...start+size)# Set first and last pages plus gaps when needed, respecting the sizeifvars[:ends]&&size>=7series[0]=1unlessseries[0]==1series[1]=:gapunlessseries[1]==2series[-2]=:gapunlessseries[-2]==@last-1series[-1]=@lastunlessseries[-1]==@lastendendseries[series.index(@page)]=@page.to_sendendendrequire_relative'pagy/backend'require_relative'pagy/frontend'require_relative'pagy/exceptions'