Sha256: 016fa8da37c69085f3cb26cd95e7d6f20de8f4a233cffae73f021a87ac883320

Contents?: true

Size: 783 Bytes

Versions: 20

Compression:

Stored size: 783 Bytes

Contents

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

      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

      def total_pages
        (total_entries / per_page.to_f).ceil
      end

      def out_of_bounds?
        current_page > total_pages
      end

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

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

      def skip
        (current_page - 1) * per_page
      end

      alias :limit :per_page
      alias :offset :skip
    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
plucky-0.5.2 lib/plucky/pagination/paginator.rb
plucky-0.5.1 lib/plucky/pagination/paginator.rb
plucky-0.5.0 lib/plucky/pagination/paginator.rb
plucky-0.4.4 lib/plucky/pagination/paginator.rb
plucky-0.4.3 lib/plucky/pagination/paginator.rb
plucky-0.4.2 lib/plucky/pagination/paginator.rb
jonbell-plucky-0.3.8 lib/plucky/pagination/paginator.rb
plucky-0.4.1 lib/plucky/pagination/paginator.rb
plucky-0.4.0 lib/plucky/pagination/paginator.rb
plucky-0.3.8 lib/plucky/pagination/paginator.rb
plucky-0.3.7 lib/plucky/pagination/paginator.rb
plucky-0.3.6 lib/plucky/pagination/paginator.rb
plucky-0.3.5 lib/plucky/pagination/paginator.rb
plucky-0.3.4 lib/plucky/pagination/paginator.rb
plucky-0.3.3 lib/plucky/pagination/paginator.rb
plucky-0.3.2 lib/plucky/pagination/paginator.rb
plucky-0.3.1 lib/plucky/pagination/paginator.rb
plucky-0.3.0 lib/plucky/pagination/paginator.rb
plucky-0.2.1 lib/plucky/pagination/paginator.rb
plucky-0.2 lib/plucky/pagination/paginator.rb