Class: Cursed::Cursor
- Inherits:
-
Object
- Object
- Cursed::Cursor
- 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
-
#after ⇒ Object
readonly
Returns the value of attribute after.
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#before ⇒ Object
readonly
Returns the value of attribute before.
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#maximum ⇒ Object
readonly
Returns the value of attribute maximum.
Instance Method Summary collapse
-
#after? ⇒ Boolean
returns true when the after parameter is set to a non-nil value.
-
#backward? ⇒ Boolean
returns true when the direction is backward.
-
#before? ⇒ Boolean
returns true when the before parameter is set to a non-nil value.
- #clamped_limit ⇒ Object
-
#forward? ⇒ Boolean
returns true when the direction is forward.
-
#initialize(after: nil, before: nil, limit: 10, maximum: 20, attribute: :cursor, direction: :forward) ⇒ Cursor
constructor
A new instance of Cursor.
Constructor Details
#initialize(after: nil, before: nil, limit: 10, maximum: 20, attribute: :cursor, direction: :forward) ⇒ Cursor
Returns a new instance of Cursor.
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
#after ⇒ Object (readonly)
Returns the value of attribute after.
9 10 11 |
# File 'lib/cursed/cursor.rb', line 9 def after @after end |
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
9 10 11 |
# File 'lib/cursed/cursor.rb', line 9 def attribute @attribute end |
#before ⇒ Object (readonly)
Returns the value of attribute before.
9 10 11 |
# File 'lib/cursed/cursor.rb', line 9 def before @before end |
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
9 10 11 |
# File 'lib/cursed/cursor.rb', line 9 def direction @direction end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
9 10 11 |
# File 'lib/cursed/cursor.rb', line 9 def limit @limit end |
#maximum ⇒ Object (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
48 49 50 |
# File 'lib/cursed/cursor.rb', line 48 def after? !after.nil? end |
#backward? ⇒ Boolean
returns true when the direction is backward
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
43 44 45 |
# File 'lib/cursed/cursor.rb', line 43 def before? !before.nil? end |
#clamped_limit ⇒ Object
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
33 34 35 |
# File 'lib/cursed/cursor.rb', line 33 def forward? direction == :forward end |