Module: Sequel::DatasetPagination

Defined in:
lib/sequel/extensions/pagination.rb

Instance Method Summary collapse

Instance Method Details

#each_page(page_size) ⇒ Object

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.

Raises:


55
56
57
58
59
60
61
62
# File 'lib/sequel/extensions/pagination.rb', line 55

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) ⇒ Object

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.

Raises:


40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sequel/extensions/pagination.rb', line 40

def paginate(page_no, page_size, record_count=nil)
  raise(Error, "You cannot paginate a dataset that already has a limit") if @opts[:limit]

  record_count ||= count
  page_count = (record_count / page_size.to_f).ceil
  page_count = 1 if page_count == 0

  limit(page_size, (page_no - 1) * page_size).
    with_extend(Dataset::Pagination).
    clone(:page_size=>page_size, :current_page=>page_no, :pagination_record_count=>record_count, :page_count=>page_count)
end