Class: ROM::SQL::Plugin::Pagination::Pager
- Inherits:
-
Object
- Object
- ROM::SQL::Plugin::Pagination::Pager
- Extended by:
- Initializer
- Defined in:
- lib/rom/sql/plugin/pagination.rb
Overview
Pager object provides the underlying pagination API for relations
Instance Attribute Summary collapse
-
#current_page ⇒ Integer
readonly
Current page number.
-
#dataset ⇒ Sequel::Dataset
readonly
Relation’s dataset.
-
#per_page ⇒ Integer
(also: #limit_value)
readonly
Current per-page number.
Instance Method Summary collapse
- #at(dataset, current_page, per_page = self.per_page) ⇒ Object private
-
#first_in_page ⇒ Integer
Return one-based index of first tuple in page.
-
#last_in_page ⇒ Integer
Return one-based index of last tuple in page.
-
#next_page ⇒ Integer
Return next page number.
-
#prev_page ⇒ Integer
Return previous page number.
-
#total ⇒ Integer
Return total number of tuples.
-
#total_pages ⇒ Integer
Return total number of pages.
Instance Attribute Details
#current_page ⇒ Integer (readonly)
Returns Current page number.
25 |
# File 'lib/rom/sql/plugin/pagination.rb', line 25 option :current_page, default: -> { 1 } |
#dataset ⇒ Sequel::Dataset (readonly)
Returns Relation’s dataset.
21 |
# File 'lib/rom/sql/plugin/pagination.rb', line 21 param :dataset |
#per_page ⇒ Integer (readonly) Also known as: limit_value
Returns Current per-page number.
29 |
# File 'lib/rom/sql/plugin/pagination.rb', line 29 option :per_page |
Instance Method Details
#at(dataset, current_page, per_page = self.per_page) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
98 99 100 101 102 103 104 105 106 |
# File 'lib/rom/sql/plugin/pagination.rb', line 98 def at(dataset, current_page, per_page = self.per_page) current_page = current_page.to_i per_page = per_page.to_i self.class.new( dataset.offset((current_page-1)*per_page).limit(per_page), current_page: current_page, per_page: per_page ) end |
#first_in_page ⇒ Integer
Return one-based index of first tuple in page
82 83 84 |
# File 'lib/rom/sql/plugin/pagination.rb', line 82 def first_in_page ((current_page - 1) * per_page) + 1 end |
#last_in_page ⇒ Integer
Return one-based index of last tuple in page
91 92 93 94 95 |
# File 'lib/rom/sql/plugin/pagination.rb', line 91 def last_in_page return total if current_page == total_pages current_page * per_page end |
#next_page ⇒ Integer
Return next page number
40 41 42 43 |
# File 'lib/rom/sql/plugin/pagination.rb', line 40 def next_page num = current_page + 1 num if total_pages >= num end |
#prev_page ⇒ Integer
Return previous page number
54 55 56 57 |
# File 'lib/rom/sql/plugin/pagination.rb', line 54 def prev_page num = current_page - 1 num if num > 0 end |
#total ⇒ Integer
Return total number of tuples
64 65 66 |
# File 'lib/rom/sql/plugin/pagination.rb', line 64 def total dataset.unlimited.count end |
#total_pages ⇒ Integer
Return total number of pages
73 74 75 |
# File 'lib/rom/sql/plugin/pagination.rb', line 73 def total_pages (total / per_page.to_f).ceil end |