Class: Cursed::Page

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/cursed/page.rb

Instance Method Summary collapse

Constructor Details

#initialize(relation:, cursor:) ⇒ Page

Returns a new instance of Page.



7
8
9
10
# File 'lib/cursed/page.rb', line 7

def initialize(relation:, cursor:)
  @relation = relation
  @cursor = cursor
end

Instance Method Details

#any?Boolean

Returns if there are records on this page.

Returns:

  • (Boolean)

    if there are records on this page



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

def any?
  count > 0
end

#countInteger

Returns the number of records in this page.

Returns:

  • (Integer)

    the number of records in this page



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

def count
  @count ||= @collection.try(:length) || relation.count
end

#each(*args, &block) ⇒ Object

Iterates through each element in the current page



13
14
15
# File 'lib/cursed/page.rb', line 13

def each(*args, &block)
  collection.each(*args, &block)
end

#invalidate!Object

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.



19
20
21
22
# File 'lib/cursed/page.rb', line 19

def invalidate!
  @collection = nil
  @count = nil
end

#maximum_idInteger

Returns the maximum cursor index in the current page.

Returns:

  • (Integer)

    the maximum cursor index in the current page



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

def maximum_id
  collection.map(&cursor.attribute).max
end

#minimum_idInteger

Returns the minimum cursor index in the current page.

Returns:

  • (Integer)

    the minimum cursor index in the current page



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

def minimum_id
  collection.map(&cursor.attribute).min
end

#next_page_cursorCursor

Returns with the values to fetch the next page.

Returns:

  • (Cursor)

    with the values to fetch the next page



67
68
69
70
71
72
73
74
75
# 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_paramsHash

Returns a hash of parameters which should be used for generating a next page link.

Returns:

  • (Hash)

    a hash containing any combination of :before, :after, :limit



47
48
49
50
51
52
53
# 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_cursorCursor

Returns with the values to fetch the previous page.

Returns:

  • (Cursor)

    with the values to fetch the previous page



78
79
80
81
82
83
84
85
86
# 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_paramsHash

Returns a hash of parameters which should be used for generating a previous page link.

Returns:

  • (Hash)

    a hash containing any combination of :before, :after, :limit



58
59
60
61
62
63
64
# File 'lib/cursed/page.rb', line 58

def prev_page_params
  if cursor.forward?
    before_minimum_params
  else
    after_maximum_params
  end
end