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 ⇒ 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) ⇒ 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) ⇒ Page
Returns a new instance of Page.
13 14 15 16 17 18 |
# File 'lib/activerecord_cursor_paginate/page.rb', line 13 def initialize(records, order_columns:, has_previous: false, has_next: false) @records = records @order_columns = order_columns @has_previous = has_previous @has_next = has_next end |
Instance Attribute Details
#records ⇒ 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.
23 24 25 |
# File 'lib/activerecord_cursor_paginate/page.rb', line 23 def count records.size end |
#cursor_for(record) ⇒ String
Returns cursor for a specific record.
68 69 70 |
# File 'lib/activerecord_cursor_paginate/page.rb', line 68 def cursor_for(record) cursor_for_record(record) end |
#cursors ⇒ Array<String>
Returns cursors for all the records on this page.
75 76 77 |
# File 'lib/activerecord_cursor_paginate/page.rb', line 75 def cursors records.map { |record| cursor_for_record(record) } end |
#empty? ⇒ Boolean
Whether this page is empty.
30 31 32 |
# File 'lib/activerecord_cursor_paginate/page.rb', line 30 def empty? count == 0 end |
#has_next? ⇒ Boolean
Whether this page has a next page.
59 60 61 |
# File 'lib/activerecord_cursor_paginate/page.rb', line 59 def has_next? @has_next end |
#has_previous? ⇒ Boolean
Whether this page has a previous page.
52 53 54 |
# File 'lib/activerecord_cursor_paginate/page.rb', line 52 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.
37 38 39 |
# File 'lib/activerecord_cursor_paginate/page.rb', line 37 def next_cursor cursor_for_record(records.last) end |
#previous_cursor ⇒ String
Returns the cursor, which can be used to retrieve the previous page.
45 46 47 |
# File 'lib/activerecord_cursor_paginate/page.rb', line 45 def previous_cursor cursor_for_record(records.first) end |