Module: Sequel::Dataset::Pagination
- Defined in:
- lib/sequel/extensions/pagination.rb
Overview
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).
Instance Attribute Summary collapse
-
#current_page ⇒ Object
The current page of the dataset, starting at 1 and not 0.
-
#page_count ⇒ Object
The number of pages in the dataset before pagination, of which this paginated dataset is one.
-
#page_size ⇒ Object
The number of records per page (the final page may have fewer than this number of records).
-
#pagination_record_count ⇒ Object
The total number of records in the dataset before pagination.
Instance Method Summary collapse
-
#current_page_record_count ⇒ Object
Returns the number of records in the current page.
-
#current_page_record_range ⇒ Object
Returns the record range for the current page.
-
#first_page? ⇒ Boolean
Returns true if the current page is the first page.
-
#last_page? ⇒ Boolean
Returns true if the current page is the last page.
-
#next_page ⇒ Object
Returns the next page number or nil if the current page is the last page.
-
#page_range ⇒ Object
Returns the page range.
-
#prev_page ⇒ Object
Returns the previous page number or nil if the current page is the first.
-
#set_pagination_info(page_no, page_size, record_count) ⇒ Object
Sets the pagination info for this paginated dataset, and returns self.
Instance Attribute Details
#current_page ⇒ Object
The current page of the dataset, starting at 1 and not 0.
40 41 42 |
# File 'lib/sequel/extensions/pagination.rb', line 40 def current_page @current_page end |
#page_count ⇒ Object
The number of pages in the dataset before pagination, of which this paginated dataset is one.
37 38 39 |
# File 'lib/sequel/extensions/pagination.rb', line 37 def page_count @page_count end |
#page_size ⇒ Object
The number of records per page (the final page may have fewer than this number of records).
33 34 35 |
# File 'lib/sequel/extensions/pagination.rb', line 33 def page_size @page_size end |
#pagination_record_count ⇒ Object
The total number of records in the dataset before pagination.
43 44 45 |
# File 'lib/sequel/extensions/pagination.rb', line 43 def pagination_record_count @pagination_record_count end |
Instance Method Details
#current_page_record_count ⇒ Object
Returns the number of records in the current page
56 57 58 59 60 61 62 63 |
# File 'lib/sequel/extensions/pagination.rb', line 56 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 ⇒ Object
Returns the record range for the current page
46 47 48 49 50 51 52 53 |
# File 'lib/sequel/extensions/pagination.rb', line 46 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? ⇒ Boolean
Returns true if the current page is the first page
66 67 68 |
# File 'lib/sequel/extensions/pagination.rb', line 66 def first_page? @current_page == 1 end |
#last_page? ⇒ Boolean
Returns true if the current page is the last page
71 72 73 |
# File 'lib/sequel/extensions/pagination.rb', line 71 def last_page? @current_page == @page_count end |
#next_page ⇒ Object
Returns the next page number or nil if the current page is the last page
76 77 78 |
# File 'lib/sequel/extensions/pagination.rb', line 76 def next_page current_page < page_count ? (current_page + 1) : nil end |
#page_range ⇒ Object
Returns the page range
81 82 83 |
# File 'lib/sequel/extensions/pagination.rb', line 81 def page_range 1..page_count end |
#prev_page ⇒ Object
Returns the previous page number or nil if the current page is the first
86 87 88 |
# File 'lib/sequel/extensions/pagination.rb', line 86 def prev_page current_page > 1 ? (current_page - 1) : nil end |
#set_pagination_info(page_no, page_size, record_count) ⇒ Object
Sets the pagination info for this paginated dataset, and returns self.
91 92 93 94 95 96 97 |
# File 'lib/sequel/extensions/pagination.rb', line 91 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 self end |