Class: Pageturner
- Inherits:
-
Object
- Object
- Pageturner
- Defined in:
- lib/pageturner.rb
Defined Under Namespace
Classes: Exception
Constant Summary collapse
- ASC =
"asc"- COMPARATOR_INTERNAL_BUG =
"sort_direction input has an unexpected value. Contact Pageturner mantainer for support."- DESC =
"desc"- GREATER_THAN_OPERATOR =
">"- LESS_THAN_OPERATOR =
"<"- LOOKAHEAD =
1
Instance Method Summary collapse
- #has_next_page? ⇒ Boolean
-
#initialize(anchor_column:, anchor_value:, ar_relation:, sort_direction:, page_size:, anchor_id:, anchor_column_active_record_identifier: nil, anchor_column_sql_identifier: nil) ⇒ Pageturner
constructor
A new instance of Pageturner.
- #next_cursor ⇒ Object
- #resources ⇒ Object
Constructor Details
#initialize(anchor_column:, anchor_value:, ar_relation:, sort_direction:, page_size:, anchor_id:, anchor_column_active_record_identifier: nil, anchor_column_sql_identifier: nil) ⇒ Pageturner
Returns a new instance of Pageturner.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/pageturner.rb', line 16 def initialize(anchor_column:, anchor_value:, ar_relation:, sort_direction:, page_size:, anchor_id:, anchor_column_active_record_identifier: nil, anchor_column_sql_identifier: nil) @anchor_column = anchor_column @anchor_column_active_record_identifier = anchor_column_active_record_identifier || @anchor_column @anchor_column_sql_identifier = anchor_column_sql_identifier || @anchor_column @anchor_value = anchor_value @ar_relation = ar_relation # Always select id to use it as secondary sort to prevent non-deterministic ordering when primary sort is on a non-unique column. unless primary_key_selected?(@ar_relation) raise Exception::PrimaryKeyNotSelected, "The provided ActiveRecord::Relation does not have the primary key selected. Cursor pagination requires a primary key to paginate undeterministic columns." end @sort_direction = sort_direction @page_size = page_size @anchor_id = anchor_id unless [Pageturner::ASC, Pageturner::DESC].include?(@sort_direction) raise Exception::InvalidSortDirection, "The provided order is not supported. Supported order values are: '#{ASC}', '#{DESC}'" end end |
Instance Method Details
#has_next_page? ⇒ Boolean
40 41 42 43 44 45 |
# File 'lib/pageturner.rb', line 40 def has_next_page? # Load the current page. self.resources @has_next_page end |
#next_cursor ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/pageturner.rb', line 54 def next_cursor { anchor_column: @anchor_column, anchor_id: self.resources.last&.id, anchor_value: try_chain(self.resources.last, @anchor_column_active_record_identifier), page_size: @page_size, sort_direction: @sort_direction } end |
#resources ⇒ Object
47 48 49 50 51 52 |
# File 'lib/pageturner.rb', line 47 def resources # Memoize expensive querying. return @result if defined?(@result) @result = calculate_resources end |