Sha256: 8e837ab4b79329ca6cc5f28ef10fbea3111e4fa4a14cd1c5d2124761118dce5b

Contents?: true

Size: 953 Bytes

Versions: 1

Compression:

Stored size: 953 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
d
      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)
      paginate unless @page
      @page
    end

    def current_page
      page
    end

    def per=(per)
      @per = per
    end

    def per
      paginate unless @per
      @per
    end

    def num_pages=(num_pages)
      @num_pages = num_pages
    end

    def num_pages
      paginate unless @num_pages
      @num_pages
    end

    def total_pages
      num_pages
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ro-4.2.0 lib/ro/pagination.rb