Class: Cursed::Collection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/cursed/collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(relation:, cursor:, adapter: nil) ⇒ Collection

Returns a new instance of Collection.

Parameters:

  • relation (ActiveRecord::Relation or Sequel::Dataset)

    the relation to cursor on

  • cursor (Cursor)

    the value object containing parameters for the cursor

  • adapter (Adapter) (defaults to: nil)

    an object which plays the Adapter role



17
18
19
20
21
# File 'lib/cursed/collection.rb', line 17

def initialize(relation:, cursor:, adapter: nil)
  @relation = relation
  @cursor = cursor
  @adapter = Cursed::Adapter(adapter || relation)
end

Instance Method Details

#current_pagePage

Returns the current page.

Returns:

  • (Page)

    the current page



30
31
32
# File 'lib/cursed/collection.rb', line 30

def current_page
  @current_page ||= build_page(cursor)
end

#invalidate!Object

invalidates the #current_page, #next_page and #prev_page

See Also:



25
26
27
# File 'lib/cursed/collection.rb', line 25

def invalidate!
  [prev_page, next_page, current_page].each(&:invalidate!)
end

#next_pagePage

Returns the page following this one.

Returns:

  • (Page)

    the page following this one



35
36
37
# File 'lib/cursed/collection.rb', line 35

def next_page
  @next_page ||= build_page(current_page.next_page_cursor)
end

#next_page?Boolean

Returns true if there are records that follow records in the current page.

Returns:

  • (Boolean)

    true if there are records that follow records in the current page



45
46
47
# File 'lib/cursed/collection.rb', line 45

def next_page?
  next_page.any?
end

#prev_pagePage

Returns the page previous to this one.

Returns:

  • (Page)

    the page previous to this one



40
41
42
# File 'lib/cursed/collection.rb', line 40

def prev_page
  @prev_page ||= build_page(current_page.prev_page_cursor)
end

#prev_page?Boolean

Returns true if there are records that preceede records in the current page.

Returns:

  • (Boolean)

    true if there are records that preceede records in the current page



50
51
52
# File 'lib/cursed/collection.rb', line 50

def prev_page?
  prev_page.any?
end