Class: ActiveRecordCursorPaginate::Page

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#recordsActiveRecord::Base (readonly)

Records this page contains.

Returns:

  • (ActiveRecord::Base)


11
12
13
# File 'lib/activerecord_cursor_paginate/page.rb', line 11

def records
  @records
end

Instance Method Details

#countInteger

Number of records in this page.

Returns:

  • (Integer)


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.

Parameters:

  • record (ActiveRecord::Base)

Returns:

  • (String)


68
69
70
# File 'lib/activerecord_cursor_paginate/page.rb', line 68

def cursor_for(record)
  cursor_for_record(record)
end

#cursorsArray<String>

Returns cursors for all the records on this page.

Returns:

  • (Array<String>)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


52
53
54
# File 'lib/activerecord_cursor_paginate/page.rb', line 52

def has_previous?
  @has_previous
end

#next_cursorString Also known as: cursor

Returns the cursor, which can be used to retrieve the next page.

Returns:

  • (String)


37
38
39
# File 'lib/activerecord_cursor_paginate/page.rb', line 37

def next_cursor
  cursor_for_record(records.last)
end

#previous_cursorString

Returns the cursor, which can be used to retrieve the previous page.

Returns:

  • (String)


45
46
47
# File 'lib/activerecord_cursor_paginate/page.rb', line 45

def previous_cursor
  cursor_for_record(records.first)
end