Sha256: 01f96263ef13d05ca998a5e2e7232e0b1ac55f16f48a1ef6321ef70cc8ce5f08

Contents?: true

Size: 918 Bytes

Versions: 6

Compression:

Stored size: 918 Bytes

Contents

# encoding: UTF-8
module MarkMapper
  module Pagination
    class Paginator
      attr_reader :total_entries, :current_page, :per_page

      # Public
      def initialize(total, page, per_page=nil)
        @total_entries = total.to_i
        @current_page  = [page.to_i, 1].max
        @per_page      = (per_page || 25).to_i
      end

      # Public
      def total_pages
        (@total_entries / @per_page.to_f).ceil
      end

      # Public
      def out_of_bounds?
        @current_page > total_pages
      end

      # Public
      def previous_page
        @current_page > 1 ? (@current_page - 1) : nil
      end

      # Public
      def next_page
        @current_page < total_pages ? (@current_page + 1) : nil
      end

      # Public
      def skip
        (@current_page - 1) * @per_page
      end

      # Public
      alias :limit :per_page

      # Public
      alias :offset :skip
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mark_mapper-0.0.6 lib/mark_mapper/pagination/paginator.rb
mark_mapper-0.0.5 lib/mark_mapper/pagination/paginator.rb
mark_mapper-0.0.4 lib/mark_mapper/pagination/paginator.rb
mark_mapper-0.0.3 lib/mark_mapper/pagination/paginator.rb
mark_mapper-0.0.2 lib/mark_mapper/pagination/paginator.rb
mark_mapper-0.0.1 lib/mark_mapper/pagination/paginator.rb