Sha256: ee50166ffef8280dc488f333c2c84a13c59e95843e76e2724d241e7a9e5535df

Contents?: true

Size: 1.03 KB

Versions: 4

Compression:

Stored size: 1.03 KB

Contents

module Sunspot
  module Search

    class PaginatedCollection < Array

      attr_reader :current_page, :per_page
      attr_accessor :total_count
      alias :total_entries :total_count
      alias :total_entries= :total_count=
      alias :limit_value :per_page

      def initialize(collection, page, per_page, total)
        @current_page = page
        @per_page     = per_page
        @total_count  = total
        replace collection
      end

      def total_pages
        (total_count.to_f / per_page).ceil
      end
      alias :num_pages :total_pages

      def first_page?
        current_page == 1
      end

      def last_page?
        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 out_of_bounds?
        current_page > total_pages
      end

      def offset
        (current_page - 1) * per_page
      end
      alias :offset_value :offset

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sunspot-2.1.1 lib/sunspot/search/paginated_collection.rb
sunspot-2.1.0 lib/sunspot/search/paginated_collection.rb
sunspot-2.0.0 lib/sunspot/search/paginated_collection.rb
sunspot-2.0.0.pre.130115 lib/sunspot/search/paginated_collection.rb