Sha256: cd370e612adc2d5856d755f4896f2eda0ad2c4abd93d946126ebe4f0c09aed43

Contents?: true

Size: 718 Bytes

Versions: 1

Compression:

Stored size: 718 Bytes

Contents

require 'geared_pagination/ratios'
require 'geared_pagination/page'

module GearedPagination
  class Recordset
    attr_reader :records, :ratios

    def initialize(records, per_page: nil)
      @records = records
      @ratios  = GearedPagination::Ratios.new(per_page)
    end

    def page(number)
      GearedPagination::Page.new(number, from: self)
    end

    def page_count
      @page_count ||= begin
        count    = 0
        residual = records_count

        while residual > 0
          count   += 1
          residual = residual - ratios[count]
        end

        count
      end
    end

    def records_count
      @records_count ||= records.unscope(:limit).unscope(:offset).count
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
geared_pagination-0.1 lib/geared_pagination/recordset.rb