Sha256: 86aa24b4c1f38c19ad57f7ece86d422362783bb5bccebc3517e432a6a5c3a66e

Contents?: true

Size: 914 Bytes

Versions: 9

Compression:

Stored size: 914 Bytes

Contents

# encoding: UTF-8
module Plucky
  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

9 entries across 9 versions & 1 rubygems

Version Path
plucky-0.8.0 lib/plucky/pagination/paginator.rb
plucky-0.7.0 lib/plucky/pagination/paginator.rb
plucky-0.6.6 lib/plucky/pagination/paginator.rb
plucky-0.6.5 lib/plucky/pagination/paginator.rb
plucky-0.6.4 lib/plucky/pagination/paginator.rb
plucky-0.6.3 lib/plucky/pagination/paginator.rb
plucky-0.6.2 lib/plucky/pagination/paginator.rb
plucky-0.6.1 lib/plucky/pagination/paginator.rb
plucky-0.6.0 lib/plucky/pagination/paginator.rb