Class: Cursed::Page
Instance Method Summary collapse
-
#any? ⇒ Boolean
If there are records on this page.
-
#count ⇒ Integer
The number of records in this page.
-
#each(*args, &block) ⇒ Object
Iterates through each element in the current page.
-
#initialize(relation:, cursor:) ⇒ Page
constructor
A new instance of Page.
-
#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.
-
#maximum_id ⇒ Integer
The maximum cursor index in the current page.
-
#minimum_id ⇒ Integer
The minimum cursor index in the current page.
-
#next_page_cursor ⇒ Cursor
With the values to fetch the next page.
-
#next_page_params ⇒ Hash
Returns a hash of parameters which should be used for generating a next page link.
-
#prev_page_cursor ⇒ Cursor
With the values to fetch the previous page.
-
#prev_page_params ⇒ Hash
Returns a hash of parameters which should be used for generating a previous page link.
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.
30 31 32 |
# File 'lib/cursed/page.rb', line 30 def any? count > 0 end |
#count ⇒ Integer
Returns 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_id ⇒ Integer
Returns 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_id ⇒ Integer
Returns 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_cursor ⇒ Cursor
Returns 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_params ⇒ Hash
Returns a hash of parameters which should be used for generating a next page link.
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_cursor ⇒ Cursor
Returns 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_params ⇒ Hash
Returns a hash of parameters which should be used for generating a previous page link.
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 |