Module: CursorPagination::PageScopeMethods

Defined in:
lib/cursor_pagination/page_scope_methods.rb

Instance Method Summary collapse

Instance Method Details

#first_page?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/cursor_pagination/page_scope_methods.rb', line 7

def first_page?
  previous_cursor.value == -1
end

#last_page?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/cursor_pagination/page_scope_methods.rb', line 11

def last_page?
  next_cursor.value == -1
end

#next_cursorObject



37
38
39
40
41
42
43
44
45
# File 'lib/cursor_pagination/page_scope_methods.rb', line 37

def next_cursor
  Cursor.new(if last.nil?
    -1
  else
    # try to get something after last cursor
    cursor = Cursor.from_entity last, cursor_options[:columns]
    _origin_scope.cursor(cursor, cursor_options).per(1).count.zero? ? -1 : cursor.value
  end)
end

#per(num) ⇒ Object



3
4
5
# File 'lib/cursor_pagination/page_scope_methods.rb', line 3

def per(num)
  limit(num)
end

#previous_cursorObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cursor_pagination/page_scope_methods.rb', line 15

def previous_cursor
  Cursor.new(if current_cursor.empty?
    -1
  else
    scope = _origin_scope.limit(limit_value+1).reverse_order
    columns = cursor_options[:columns]

    cursor_value = [*current_cursor.value]
    scope = scope.where _cursor_to_where(columns, cursor_value, true)
    result = scope.to_a

    case result.size
    when limit_value+1
      Cursor.value_from_entity result.last, columns
    when 0
      -1 #no previous page
    else
      nil #first page, incomplete
    end
  end)
end