class Cursed::Page
Attributes
Public Class Methods
# File lib/cursed/page.rb, line 7 def initialize(relation:, cursor:) @relation = relation @cursor = cursor end
Public Instance Methods
@return [Boolean] if there are records on this page
# File lib/cursed/page.rb, line 30 def any? count > 0 end
@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
Iterates through each element in the current page
# File lib/cursed/page.rb, line 13 def each(*args, &block) collection.each(*args, &block) end
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
@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
@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
@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
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
@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
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
# File lib/cursed/page.rb, line 96 def after_maximum_params { after: maximum_id || cursor.after, limit: cursor.clamped_limit } end
# File lib/cursed/page.rb, line 100 def before_minimum_params { before: minimum_id || cursor.before, limit: cursor.clamped_limit } end
# File lib/cursed/page.rb, line 92 def collection @collection ||= relation.to_a end