class Cursed::Collection

Attributes

adapter[R]
cursor[R]
relation[R]

Public Class Methods

new(relation:, cursor:, adapter: nil) click to toggle source

@param relation [ActiveRecord::Relation or Sequel::Dataset] the relation to cursor on @param cursor [Cursor] the value object containing parameters for the cursor @param adapter [Adapter] an object which plays the Adapter role

# File lib/cursed/collection.rb, line 17
def initialize(relation:, cursor:, adapter: nil)
  @relation = relation
  @cursor = cursor
  @adapter = Cursed::Adapter(adapter || relation)
end

Public Instance Methods

current_page() click to toggle source

@return [Page] the current page

# File lib/cursed/collection.rb, line 30
def current_page
  @current_page ||= build_page(cursor)
end
invalidate!() click to toggle source

invalidates the {#current_page}, {#next_page} and {#prev_page} @see Page#invalidate!

# File lib/cursed/collection.rb, line 25
def invalidate!
  [prev_page, next_page, current_page].each(&:invalidate!)
end
next_page() click to toggle source

@return [Page] the page following this one

# File lib/cursed/collection.rb, line 35
def next_page
  @next_page ||= build_page(current_page.next_page_cursor)
end
next_page?() click to toggle source

@return [Boolean] true if there are records that follow records in the current page

# File lib/cursed/collection.rb, line 45
def next_page?
  next_page.any?
end
prev_page() click to toggle source

@return [Page] the page previous to this one

# File lib/cursed/collection.rb, line 40
def prev_page
  @prev_page ||= build_page(current_page.prev_page_cursor)
end
prev_page?() click to toggle source

@return [Boolean] true if there are records that preceede records in the current page

# File lib/cursed/collection.rb, line 50
def prev_page?
  prev_page.any?
end

Private Instance Methods

build_page(cursor) click to toggle source
# File lib/cursed/collection.rb, line 58
def build_page(cursor)
  Page.new(relation: adapter.new(relation.dup).apply_to(cursor), cursor: cursor)
end