Sha256: b6a46462d07d6e9a2b820d8f891b45d25beb3a8c5cf7008693b5d7edf365a5d8

Contents?: true

Size: 865 Bytes

Versions: 14

Compression:

Stored size: 865 Bytes

Contents

module Ro
  module Pagination
    def paginate(*args)
      options = Map.options_for!(args)

      page = Integer(args.shift || options[:page] || 1)
      per = Integer(args.shift || options[:per] || options[:size] || 10)

      page = [page.abs, 1].max
      per = [per.abs, 1].max

      offset = (page - 1) * per
      length = per 

      slice = dup.slice(offset, length)
      slice.page = page
      slice.per = per
      slice.num_pages = (size.to_f / per).ceil
      slice
    end

    def page=(page)
      @page = page
    end

    def page(*args)
      @page
    end

    def current_page
      @page
    end

    def per=(per)
      @per = per
    end

    def per
      @per
    end

    def num_pages=(num_pages)
      @num_pages = num_pages
    end

    def num_pages
      @num_pages
    end

    def total_pages
      num_pages
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
ro-1.4.6 lib/ro/pagination.rb
ro-1.4.4 lib/ro/pagination.rb
ro-1.4.3 lib/ro/pagination.rb
ro-1.4.2 lib/ro/pagination.rb
ro-1.4.1 lib/ro/pagination.rb
ro-1.4.0 lib/ro/pagination.rb
ro-1.3.8 lib/ro/pagination.rb
ro-1.3.7 lib/ro/pagination.rb
ro-1.3.6 lib/ro/pagination.rb
ro-1.3.5 lib/ro/pagination.rb
ro-1.3.4 lib/ro/pagination.rb
ro-1.3.3 lib/ro/pagination.rb
ro-1.3.2 lib/ro/pagination.rb
ro-1.3.1 lib/ro/pagination.rb