Sha256: aba5a8897c1e7c805f73833ad1e42087e0ef7fe909b67d5dff3b34940e9dc408

Contents?: true

Size: 1.52 KB

Versions: 18

Compression:

Stored size: 1.52 KB

Contents

module Paginate

  class InvalidPage < ArgumentError
    def initialize(page, page_num)
      super "#{page.inspect} given as value, which translates to '#{page_num}' as page number"
    end
  end
  
  class Collection < Array
    attr_reader :current_page, :per_page, :total_entries, :total_pages

    def initialize(page, per_page, total = nil)
      @current_page = page.to_i
      raise InvalidPage.new(page, @current_page) if @current_page < 1
      @per_page = per_page.to_i
      raise ArgumentError, "`per_page` setting cannot be less than 1 (#{@per_page} given)" if @per_page < 1
      
      self.total_entries = total if total
    end

    def self.create(page, per_page, total = nil)
      pager = new(page, per_page, total)
      yield pager
      pager
    end

    def out_of_bounds?
      current_page > total_pages
    end

    def offset
      (current_page - 1) * per_page
    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 total_entries=(number)
      @total_entries = number.to_i
      @total_pages   = (@total_entries / per_page.to_f).ceil
    end

    def replace(array)
      result = super
      
      # The collection is shorter then page limit? Rejoice, because
      # then we know that we are on the last page!
      if total_entries.nil? and length < per_page and (current_page == 1 or length > 0)
        self.total_entries = offset + length
      end

      result
    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
grippy-doozer-0.1.0 lib/doozer/plugins/paginate/lib/paginate/collection.rb
grippy-doozer-0.1.1 lib/doozer/plugins/paginate/lib/paginate/collection.rb
grippy-doozer-0.1.2 lib/doozer/plugins/paginate/lib/paginate/collection.rb
grippy-doozer-0.1.3 lib/doozer/plugins/paginate/lib/paginate/collection.rb
grippy-doozer-0.1.4 lib/doozer/plugins/paginate/lib/paginate/collection.rb
grippy-doozer-0.1.5 lib/doozer/plugins/paginate/lib/paginate/collection.rb
doozer-0.4.4 lib/doozer/plugins/paginate/lib/paginate/collection.rb
doozer-0.4.3 lib/doozer/plugins/paginate/lib/paginate/collection.rb
doozer-0.4.2 lib/doozer/plugins/paginate/lib/paginate/collection.rb
doozer-0.4.1 lib/doozer/plugins/paginate/lib/paginate/collection.rb
doozer-0.4.0 lib/doozer/plugins/paginate/lib/paginate/collection.rb
doozer-0.3.1 lib/doozer/plugins/paginate/lib/paginate/collection.rb
doozer-0.3.0 lib/doozer/plugins/paginate/lib/paginate/collection.rb
doozer-0.2.6 lib/doozer/plugins/paginate/lib/paginate/collection.rb
doozer-0.2.5 lib/doozer/plugins/paginate/lib/paginate/collection.rb
doozer-0.2.2 lib/doozer/plugins/paginate/lib/paginate/collection.rb
doozer-0.2.1 lib/doozer/plugins/paginate/lib/paginate/collection.rb
doozer-0.2.0 lib/doozer/plugins/paginate/lib/paginate/collection.rb