Skip to content

Commit

Permalink
small improvement: to_i called only once in core variables set
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed May 27, 2018
1 parent d6659ca commit 860c579
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def self.root; Pathname.new(__FILE__).dirname end
def initialize(vars)
@vars = VARS.merge(vars.delete_if{|k,v| v.nil? or v == '' }) # default vars + cleaned instance vars
{ count:0, items:1, outset:0, page:1 }.each do |k,min| # validate core variables
(@vars[k] && @vars[k].to_i >= min) or raise(ArgumentError, "expected :#{k} >= #{min}; got #{@vars[k].inspect}")
instance_variable_set(:"@#{k}", @vars.delete(k).to_i) # set instance variables
(@vars[k] && instance_variable_set(:"@#{k}", @vars.delete(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
(1..@last).cover?(@page) || raise(OutOfRangeError, "expected :page in 1..#{@last}; got #{@page.inspect}")
Expand Down

0 comments on commit 860c579

Please sign in to comment.