Sha256: abe049f7c6eca2136ea7372ba17e303416c6bfb0232da89885489720953eec62

Contents?: true

Size: 702 Bytes

Versions: 5

Compression:

Stored size: 702 Bytes

Contents

require 'geared_pagination/ratios'

module GearedPagination
  class PortionAtOffset
    attr_reader :page_number, :ratios

    def initialize(page_number: 1, per_page: Ratios.new)
      @page_number, @ratios = page_number, per_page
    end

    def from(scope)
      scope.limit(limit).offset(offset)
    end

    def limit
      ratios[page_number]
    end

    def offset
      variable = [(page_number - 1), ratios.size - 1].min.times.sum { |index| ratios[index + 1] }
      fixed = [page_number - ratios.size, 0].max * ratios.fixed

      variable + fixed
    end

    def next_param(*)
      page_number + 1
    end


    def cache_key
      "#{page_number}:#{ratios.cache_key}"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
geared_pagination-1.2.0 lib/geared_pagination/portions/portion_at_offset.rb
geared_pagination-1.1.2 lib/geared_pagination/portions/portion_at_offset.rb
geared_pagination-1.1.1 lib/geared_pagination/portions/portion_at_offset.rb
geared_pagination-1.1.0 lib/geared_pagination/portions/portion_at_offset.rb
geared_pagination-1.0.2 lib/geared_pagination/portions/portion_at_offset.rb