class Cursed::Page

Attributes

cursor[R]
relation[R]

Public Class Methods

new(relation:, cursor:) click to toggle source
# File lib/cursed/page.rb, line 7
def initialize(relation:, cursor:)
  @relation = relation
  @cursor = cursor
end

Public Instance Methods

any?() click to toggle source

@return [Boolean] if there are records on this page

# File lib/cursed/page.rb, line 30
def any?
  count > 0
end
count() click to toggle source

@return [Integer] the number of records in this page

# File lib/cursed/page.rb, line 25
def count
  @count ||= @collection.try(:length) || relation.count
end
each(*args, &block) click to toggle source

Iterates through each element in the current page

# File lib/cursed/page.rb, line 13
def each(*args, &block)
  collection.each(*args, &block)
end
invalidate!() click to toggle source

Invalidates the local cache of the current page - the next call to {#each} (or any Enumerable method that calls it) will fetch a fresh page.

# File lib/cursed/page.rb, line 19
def invalidate!
  @collection = nil
  @count = nil
end
maximum_id() click to toggle source

@return [Integer] the maximum cursor index in the current page

# File lib/cursed/page.rb, line 35
def maximum_id
  collection.map(&cursor.attribute).max
end
minimum_id() click to toggle source

@return [Integer] the minimum cursor index in the current page

# File lib/cursed/page.rb, line 40
def minimum_id
  collection.map(&cursor.attribute).min
end
next_page_cursor() click to toggle source

@return [Cursor] with the values to fetch the next page

# File lib/cursed/page.rb, line 67
def next_page_cursor
  params = next_page_params.merge(
    maximum: cursor.maximum,
    direction: cursor.direction,
    attribute: cursor.attribute
  )

  Cursor.new(params)
end
next_page_params() click to toggle source

Returns a hash of parameters which should be used for generating a next page link. @return [Hash] a hash containing any combination of :before, :after, :limit

# File lib/cursed/page.rb, line 47
def next_page_params
  if cursor.forward?
    after_maximum_params
  else
    before_minimum_params
  end
end
prev_page_cursor() click to toggle source

@return [Cursor] with the values to fetch the previous page

# File lib/cursed/page.rb, line 78
def prev_page_cursor
  params = prev_page_params.merge(
    maximum: cursor.maximum,
    direction: cursor.direction,
    attribute: cursor.attribute
  )

  Cursor.new(params)
end
prev_page_params() click to toggle source

Returns a hash of parameters which should be used for generating a previous page link. @return [Hash] a hash containing any combination of :before, :after, :limit

# File lib/cursed/page.rb, line 58
def prev_page_params
  if cursor.forward?
    before_minimum_params
  else
    after_maximum_params
  end
end

Private Instance Methods

after_maximum_params() click to toggle source
# File lib/cursed/page.rb, line 96
def after_maximum_params
  { after: maximum_id || cursor.after, limit: cursor.clamped_limit }
end
before_minimum_params() click to toggle source
# File lib/cursed/page.rb, line 100
def before_minimum_params
  { before: minimum_id || cursor.before, limit: cursor.clamped_limit }
end
collection() click to toggle source
# File lib/cursed/page.rb, line 92
def collection
  @collection ||= relation.to_a
end