Class: GearedPagination::PortionAtCursor

Inherits:
Object
  • Object
show all
Defined in:
lib/geared_pagination/portions/portion_at_cursor.rb

Defined Under Namespace

Classes: Equal, Inequal, Match, Predicate, Selection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cursor: Cursor.new, ordered_by:, per_page: Ratios.new) ⇒ PortionAtCursor

Returns a new instance of PortionAtCursor.



10
11
12
# File 'lib/geared_pagination/portions/portion_at_cursor.rb', line 10

def initialize(cursor: Cursor.new, ordered_by:, per_page: Ratios.new)
  @cursor, @orders, @ratios = cursor, ordered_by, per_page
end

Instance Attribute Details

#cursorObject (readonly)

Returns the value of attribute cursor.



7
8
9
# File 'lib/geared_pagination/portions/portion_at_cursor.rb', line 7

def cursor
  @cursor
end

#ordersObject (readonly)

Returns the value of attribute orders.



7
8
9
# File 'lib/geared_pagination/portions/portion_at_cursor.rb', line 7

def orders
  @orders
end

#ratiosObject (readonly)

Returns the value of attribute ratios.



7
8
9
# File 'lib/geared_pagination/portions/portion_at_cursor.rb', line 7

def ratios
  @ratios
end

Instance Method Details

#cache_keyObject



27
28
29
# File 'lib/geared_pagination/portions/portion_at_cursor.rb', line 27

def cache_key
  "#{page_number}:#{ratios.cache_key}"
end

#from(scope) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/geared_pagination/portions/portion_at_cursor.rb', line 14

def from(scope)
  if scope.order_values.none? && scope.limit_value.nil?
    selection_from(scope).order(orderings).limit(limit)
  else
    raise ArgumentError, "Can't paginate relation with ORDER BY or LIMIT clauses (got #{scope.to_sql})"
  end
end

#next_param(scope) ⇒ Object



22
23
24
# File 'lib/geared_pagination/portions/portion_at_cursor.rb', line 22

def next_param(scope)
  Cursor.encode page_number: page_number + 1, values: from(scope).last&.slice(*attributes) || {}
end