module Sequel::DatasetPagination

  1. lib/sequel/extensions/pagination.rb
Parent: Sequel

Methods

Public Instance

  1. each_page
  2. paginate

Public Instance methods

each_page (page_size)

Yields a paginated dataset for each page and returns the receiver. Does a count to find the total number of records for this dataset. Returns an enumerator if no block is given.

[show source]
# File lib/sequel/extensions/pagination.rb, line 33
def each_page(page_size)
  raise(Error, "You cannot paginate a dataset that already has a limit") if @opts[:limit]
  return to_enum(:each_page, page_size) unless block_given?
  record_count = count
  total_pages = (record_count / page_size.to_f).ceil
  (1..total_pages).each{|page_no| yield paginate(page_no, page_size, record_count)}
  self
end
paginate (page_no, page_size, record_count=nil)

Returns a paginated dataset. The returned dataset is limited to the page size at the correct offset, and extended with the Pagination module. If a record count is not provided, does a count of total number of records for this dataset.

[show source]
# File lib/sequel/extensions/pagination.rb, line 23
def paginate(page_no, page_size, record_count=nil)
  raise(Error, "You cannot paginate a dataset that already has a limit") if @opts[:limit]
  paginated = limit(page_size, (page_no - 1) * page_size)
  paginated.extend(Dataset::Pagination)
  paginated.set_pagination_info(page_no, page_size, record_count || count)
end