Class: Cursed::Cursor

Inherits:
Object
  • Object
show all
Defined in:
lib/cursed/cursor.rb

Overview

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

Constant Summary collapse

DIRECTIONS =
%i(forward backward).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(after: nil, before: nil, limit: 10, maximum: 20, attribute: :cursor, direction: :forward) ⇒ Cursor

Returns a new instance of Cursor.

Parameters:

  • after (Integer) (defaults to: nil)

    The cursor index to retrieve records after

  • before (Integer) (defaults to: nil)

    The cursor index to retrive records before

  • limit (Integer) (defaults to: 10)

    The maximum number of records to retrieve

  • maximum (Integer) (defaults to: 20)

    The maximum value that :limit can have

  • attribute (Symbol) (defaults to: :cursor)

    The name of the attribute to cursor upon

  • direction (:forward or :backward) (defaults to: :forward)

    The direction the cursor will advance in the collection

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
# 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

Instance Attribute Details

#afterObject (readonly)

Returns the value of attribute after.



9
10
11
# File 'lib/cursed/cursor.rb', line 9

def after
  @after
end

#attributeObject (readonly)

Returns the value of attribute attribute.



9
10
11
# File 'lib/cursed/cursor.rb', line 9

def attribute
  @attribute
end

#beforeObject (readonly)

Returns the value of attribute before.



9
10
11
# File 'lib/cursed/cursor.rb', line 9

def before
  @before
end

#directionObject (readonly)

Returns the value of attribute direction.



9
10
11
# File 'lib/cursed/cursor.rb', line 9

def direction
  @direction
end

#limitObject (readonly)

Returns the value of attribute limit.



9
10
11
# File 'lib/cursed/cursor.rb', line 9

def limit
  @limit
end

#maximumObject (readonly)

Returns the value of attribute maximum.



9
10
11
# File 'lib/cursed/cursor.rb', line 9

def maximum
  @maximum
end

Instance Method Details

#after?Boolean

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

Returns:

  • (Boolean)


48
49
50
# File 'lib/cursed/cursor.rb', line 48

def after?
  !after.nil?
end

#backward?Boolean

returns true when the direction is backward

Returns:

  • (Boolean)


38
39
40
# File 'lib/cursed/cursor.rb', line 38

def backward?
  direction == :backward
end

#before?Boolean

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

Returns:

  • (Boolean)


43
44
45
# File 'lib/cursed/cursor.rb', line 43

def before?
  !before.nil?
end

#clamped_limitObject

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



28
29
30
# File 'lib/cursed/cursor.rb', line 28

def clamped_limit
  [1, limit, maximum].sort[1]
end

#forward?Boolean

returns true when the direction is forward

Returns:

  • (Boolean)


33
34
35
# File 'lib/cursed/cursor.rb', line 33

def forward?
  direction == :forward
end