class Cursed::Cursor

Cursor is a value object that has all the parameters required to determine how to fetch a page of records using the cursor pattern.

Constants

DIRECTIONS

Attributes

after[R]
attribute[R]
before[R]
direction[R]
limit[R]
maximum[R]

Public Class Methods

new(after: nil, before: nil, limit: 10, maximum: 20, attribute: :cursor, direction: :forward) click to toggle source

@param [Integer] after The cursor index to retrieve records after @param [Integer] before The cursor index to retrive records before @param [Integer] limit The maximum number of records to retrieve @param [Integer] maximum The maximum value that :limit can have @param [Symbol] attribute The name of the attribute to cursor upon @param [:forward or :backward] direction The direction the cursor will advance in the collection

# File lib/cursed/cursor.rb, line 17
def initialize(after: nil, before: nil, limit: 10, maximum: 20, attribute: :cursor, direction: :forward)
  @after = Integer(after) unless after.nil?
  @before = Integer(before) unless before.nil?
  @limit = Integer(limit)
  @maximum = Integer(maximum)
  @attribute = attribute
  @direction = direction
  raise ArgumentError, "#{direction} is not a valid direction" unless DIRECTIONS.include?(direction)
end

Public Instance Methods

after?() click to toggle source

returns true when the after parameter is set to a non-nil value

# File lib/cursed/cursor.rb, line 48
def after?
  !after.nil?
end
backward?() click to toggle source

returns true when the direction is backward

# File lib/cursed/cursor.rb, line 38
def backward?
  direction == :backward
end
before?() click to toggle source

returns true when the before parameter is set to a non-nil value

# File lib/cursed/cursor.rb, line 43
def before?
  !before.nil?
end
clamped_limit() click to toggle source

returns the value of {#limit} clamped into the range of 1..{#maximum} (inclusive)

# File lib/cursed/cursor.rb, line 28
def clamped_limit
  [1, limit, maximum].sort[1]
end
forward?() click to toggle source

returns true when the direction is forward

# File lib/cursed/cursor.rb, line 33
def forward?
  direction == :forward
end