Class: ActiveRecordCursorPaginate::Page
- Inherits:
-
Object
- Object
- ActiveRecordCursorPaginate::Page
- Defined in:
- lib/activerecord_cursor_paginate/page.rb
Overview
Represents a batch of records retrieved via a single iteration of cursor-based pagination.
Instance Attribute Summary collapse
-
#records ⇒ Array<ActiveRecord::Base>
readonly
Records this page contains.
Instance Method Summary collapse
-
#count ⇒ Integer
Number of records in this page.
-
#cursor_for(record) ⇒ String
Returns cursor for a specific record.
-
#cursors ⇒ Array<String>
Returns cursors for all the records on this page.
-
#empty? ⇒ Boolean
Whether this page is empty.
-
#has_next? ⇒ Boolean
Whether this page has a next page.
-
#has_previous? ⇒ Boolean
Whether this page has a previous page.
-
#initialize(records, order_columns:, has_previous: false, has_next: false, nullable_columns: nil) ⇒ Page
constructor
A new instance of Page.
-
#next_cursor ⇒ String
(also: #cursor)
Returns the cursor, which can be used to retrieve the next page.
-
#previous_cursor ⇒ String
Returns the cursor, which can be used to retrieve the previous page.
Constructor Details
#initialize(records, order_columns:, has_previous: false, has_next: false, nullable_columns: nil) ⇒ Page
Returns a new instance of Page.
13 14 15 16 17 18 19 |
# File 'lib/activerecord_cursor_paginate/page.rb', line 13 def initialize(records, order_columns:, has_previous: false, has_next: false, nullable_columns: nil) @records = records @order_columns = order_columns @has_previous = has_previous @has_next = has_next @nullable_columns = nullable_columns end |
Instance Attribute Details
#records ⇒ Array<ActiveRecord::Base> (readonly)
Records this page contains.
11 12 13 |
# File 'lib/activerecord_cursor_paginate/page.rb', line 11 def records @records end |
Instance Method Details
#count ⇒ Integer
Number of records in this page.
24 25 26 |
# File 'lib/activerecord_cursor_paginate/page.rb', line 24 def count records.size end |
#cursor_for(record) ⇒ String
Returns cursor for a specific record.
69 70 71 |
# File 'lib/activerecord_cursor_paginate/page.rb', line 69 def cursor_for(record) cursor_for_record(record) end |
#cursors ⇒ Array<String>
Returns cursors for all the records on this page.
76 77 78 |
# File 'lib/activerecord_cursor_paginate/page.rb', line 76 def cursors records.map { |record| cursor_for_record(record) } end |
#empty? ⇒ Boolean
Whether this page is empty.
31 32 33 |
# File 'lib/activerecord_cursor_paginate/page.rb', line 31 def empty? count == 0 end |
#has_next? ⇒ Boolean
Whether this page has a next page.
60 61 62 |
# File 'lib/activerecord_cursor_paginate/page.rb', line 60 def has_next? @has_next end |
#has_previous? ⇒ Boolean
Whether this page has a previous page.
53 54 55 |
# File 'lib/activerecord_cursor_paginate/page.rb', line 53 def has_previous? @has_previous end |
#next_cursor ⇒ String Also known as: cursor
Returns the cursor, which can be used to retrieve the next page.
38 39 40 |
# File 'lib/activerecord_cursor_paginate/page.rb', line 38 def next_cursor cursor_for_record(records.last) end |
#previous_cursor ⇒ String
Returns the cursor, which can be used to retrieve the previous page.
46 47 48 |
# File 'lib/activerecord_cursor_paginate/page.rb', line 46 def previous_cursor cursor_for_record(records.first) end |