Skip to content

Commit

Permalink
Remove a couple of Range allocations (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
bquorning authored and ddnexus committed May 27, 2018
1 parent 39c5470 commit 46f235e
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/pagy.rb
Expand Up @@ -28,7 +28,7 @@ def initialize(vars)
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}")
@page >= 1 && @page <= @last or raise(OutOfRangeError, "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
Expand All @@ -39,7 +39,7 @@ def initialize(vars)

# 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])
(0..3).each{|i| (size[i]>=0 rescue nil) or raise(ArgumentError, "expected 4 items >= 0 in :size; got #{size.inspect}")}
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
Expand Down

0 comments on commit 46f235e

Please sign in to comment.