Module: Sequel::Dataset::Pagination

Defined in:
lib/sequel_core/dataset/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

Instance Method Summary collapse

Instance Attribute Details

#current_pageObject

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



36
37
38
# File 'lib/sequel_core/dataset/pagination.rb', line 36

def current_page
  @current_page
end

#page_countObject

The number of pages in the dataset before pagination, of which this paginated dataset is one.



33
34
35
# File 'lib/sequel_core/dataset/pagination.rb', line 33

def page_count
  @page_count
end

#page_sizeObject

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



29
30
31
# File 'lib/sequel_core/dataset/pagination.rb', line 29

def page_size
  @page_size
end

#pagination_record_countObject

The total number of records in the dataset before pagination.



39
40
41
# File 'lib/sequel_core/dataset/pagination.rb', line 39

def pagination_record_count
  @pagination_record_count
end

Instance Method Details

#current_page_record_countObject

Returns the number of records in the current page



52
53
54
55
56
57
58
59
# File 'lib/sequel_core/dataset/pagination.rb', line 52

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_rangeObject

Returns the record range for the current page



42
43
44
45
46
47
48
49
# File 'lib/sequel_core/dataset/pagination.rb', line 42

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

Returns:

  • (Boolean)


62
63
64
# File 'lib/sequel_core/dataset/pagination.rb', line 62

def first_page?
  @current_page == 1
end

#last_page?Boolean

Returns true if the current page is the last page

Returns:

  • (Boolean)


67
68
69
# File 'lib/sequel_core/dataset/pagination.rb', line 67

def last_page?
  @current_page == @page_count
end

#next_pageObject

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



72
73
74
# File 'lib/sequel_core/dataset/pagination.rb', line 72

def next_page
  current_page < page_count ? (current_page + 1) : nil
end

#page_rangeObject

Returns the page range



77
78
79
# File 'lib/sequel_core/dataset/pagination.rb', line 77

def page_range
  1..page_count
end

#prev_pageObject

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



82
83
84
# File 'lib/sequel_core/dataset/pagination.rb', line 82

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.



87
88
89
90
91
92
93
# File 'lib/sequel_core/dataset/pagination.rb', line 87

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