Sha256: 14b6f1a58389d2e5d70409285795f706a9348fa002c1c4af90608c916a8c53d6

Contents?: true

Size: 947 Bytes

Versions: 18

Compression:

Stored size: 947 Bytes

Contents

module Nazrin
  class PaginatedArray < Array
    attr_reader :current_page, :per_page, :total_count

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

    # first page of the collections?
    def first_page?
      current_page == 1
    end

    # last page of the collections?
    def last_page?
      current_page >= total_pages
    end

    # total number of pages
    def total_pages
      (total_count.to_f / per_page).ceil
    end

    # previous page number in the collections
    def previous_page
      current_page - 1 unless first_page? || out_of_bounds?
    end

    # next page number in the collections
    def next_page
      current_page + 1 unless last_page? || out_of_bounds?
    end

    # out of bounds of the collections?
    def out_of_bounds?
      current_page > total_pages
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
nazrin-2.7.0 lib/nazrin/paginated_array.rb
nazrin-2.6.3 lib/nazrin/paginated_array.rb
nazrin-2.6.2 lib/nazrin/paginated_array.rb
nazrin-2.6.1 lib/nazrin/paginated_array.rb
nazrin-2.6.0 lib/nazrin/paginated_array.rb
nazrin-2.5.0 lib/nazrin/paginated_array.rb
nazrin-2.4.0 lib/nazrin/paginated_array.rb
nazrin-2.3.0 lib/nazrin/paginated_array.rb
nazrin-2.2.0 lib/nazrin/paginated_array.rb
nazrin-2.1.2 lib/nazrin/paginated_array.rb
nazrin-2.1.1 lib/nazrin/paginated_array.rb
nazrin-2.1.0 lib/nazrin/paginated_array.rb
nazrin-2.0.0 lib/nazrin/paginated_array.rb
nazrin-2.0.0.rc2 lib/nazrin/paginated_array.rb
nazrin-2.0.0.rc1 lib/nazrin/paginated_array.rb
nazrin-1.0.1 lib/nazrin/paginated_array.rb
nazrin-1.0.0 lib/nazrin/paginated_array.rb
nazrin-0.2.0 lib/nazrin/paginated_array.rb