module Sequel::Dataset::Pagination

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

Holds methods that only relate to paginated datasets. Paginated dataset have pages starting at 1 (page 1 is offset 0, page 1 is offset #page_size).

Attributes

current_page [RW]

The current page of the dataset, starting at 1 and not 0.

page_count [RW]

The number of pages in the dataset before pagination, of which this paginated dataset is one. Empty datasets are considered to have a single page.

page_size [RW]

The number of records per page (the final page may have fewer than this number of records).

pagination_record_count [RW]

The total number of records in the dataset before pagination.

Public Instance methods

current_page_record_count ()

Returns the number of records in the current page

[show source]
# File lib/sequel/extensions/pagination.rb, line 73
def current_page_record_count
  return 0 if @current_page > @page_count
  
  a = 1 + (@current_page - 1) * @page_size
  b = a + @page_size - 1
  b = @pagination_record_count if b > @pagination_record_count
  b - a + 1
end
current_page_record_range ()

Returns the record range for the current page

[show source]
# File lib/sequel/extensions/pagination.rb, line 63
def current_page_record_range
  return (0..0) if @current_page > @page_count
  
  a = 1 + (@current_page - 1) * @page_size
  b = a + @page_size - 1
  b = @pagination_record_count if b > @pagination_record_count
  a..b
end
first_page? ()

Returns true if the current page is the first page

[show source]
# File lib/sequel/extensions/pagination.rb, line 83
def first_page?
  @current_page == 1
end
last_page? ()

Returns true if the current page is the last page

[show source]
# File lib/sequel/extensions/pagination.rb, line 88
def last_page?
  @current_page == @page_count
end
next_page ()

Returns the next page number or nil if the current page is the last page

[show source]
# File lib/sequel/extensions/pagination.rb, line 93
def next_page
  current_page < page_count ? (current_page + 1) : nil
end
page_range ()

Returns the page range

[show source]
# File lib/sequel/extensions/pagination.rb, line 98
def page_range
  1..page_count
end
prev_page ()

Returns the previous page number or nil if the current page is the first

[show source]
# File lib/sequel/extensions/pagination.rb, line 103
def prev_page
  current_page > 1 ? (current_page - 1) : nil
end
set_pagination_info (page_no, page_size, record_count)

Sets the pagination info for this paginated dataset, and returns self.

[show source]
# File lib/sequel/extensions/pagination.rb, line 108
def set_pagination_info(page_no, page_size, record_count)
  @current_page = page_no
  @page_size = page_size
  @pagination_record_count = record_count
  @page_count = (record_count / page_size.to_f).ceil
  @page_count = 1 if @page_count == 0
  self
end