Class: Philiprehberger::GuardClause::Guard
- Inherits:
-
Object
- Object
- Philiprehberger::GuardClause::Guard
- Defined in:
- lib/philiprehberger/guard_clause/guard.rb
Overview
Guard object that performs validation checks on a value
Instance Attribute Summary collapse
-
#errors ⇒ Array<String>
readonly
Collected errors (soft mode only).
-
#value ⇒ Object
readonly
The guarded value.
Instance Method Summary collapse
-
#gte(n, message = nil) ⇒ Guard
Assert the value is greater than or equal to n.
-
#initialize(value, soft: false) ⇒ Guard
constructor
A new instance of Guard.
-
#lte(n, message = nil) ⇒ Guard
Assert the value is less than or equal to n.
-
#matches(regex, message = nil) ⇒ Guard
Assert the value matches a regex pattern.
-
#not_empty(message = nil) ⇒ Guard
Assert the value is not empty.
-
#not_equal(other, message = nil) ⇒ Guard
Assert the value is not equal to another value.
-
#not_nil(message = nil) ⇒ Guard
Assert the value is not nil.
-
#one_of(arr, message = nil) ⇒ Guard
Assert the value is one of the given options.
-
#positive(message = nil) ⇒ Guard
Assert the value is positive.
-
#valid? ⇒ Boolean
True if no errors were collected.
Constructor Details
#initialize(value, soft: false) ⇒ Guard
Returns a new instance of Guard.
9 10 11 12 13 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 9 def initialize(value, soft: false) @value = value @soft = soft @errors = [] end |
Instance Attribute Details
#errors ⇒ Array<String> (readonly)
Returns collected errors (soft mode only).
19 20 21 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 19 def errors @errors end |
#value ⇒ Object (readonly)
Returns the guarded value.
16 17 18 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 16 def value @value end |
Instance Method Details
#gte(n, message = nil) ⇒ Guard
Assert the value is greater than or equal to n
64 65 66 67 68 69 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 64 def gte(n, = nil) if @value.respond_to?(:>=) && !(@value >= n) handle_violation( || "value must be greater than or equal to #{n}") end self end |
#lte(n, message = nil) ⇒ Guard
Assert the value is less than or equal to n
76 77 78 79 80 81 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 76 def lte(n, = nil) if @value.respond_to?(:<=) && !(@value <= n) handle_violation( || "value must be less than or equal to #{n}") end self end |
#matches(regex, message = nil) ⇒ Guard
Assert the value matches a regex pattern
88 89 90 91 92 93 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 88 def matches(regex, = nil) unless regex.match?(@value.to_s) handle_violation( || "value must match #{regex.inspect}") end self end |
#not_empty(message = nil) ⇒ Guard
Assert the value is not empty
41 42 43 44 45 46 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 41 def not_empty( = nil) if @value.respond_to?(:empty?) && @value.empty? handle_violation( || 'value must not be empty') end self end |
#not_equal(other, message = nil) ⇒ Guard
Assert the value is not equal to another value
112 113 114 115 116 117 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 112 def not_equal(other, = nil) if @value == other handle_violation( || "value must not be equal to #{other.inspect}") end self end |
#not_nil(message = nil) ⇒ Guard
Assert the value is not nil
30 31 32 33 34 35 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 30 def not_nil( = nil) if @value.nil? handle_violation( || 'value must not be nil') end self end |
#one_of(arr, message = nil) ⇒ Guard
Assert the value is one of the given options
100 101 102 103 104 105 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 100 def one_of(arr, = nil) unless arr.include?(@value) handle_violation( || "value must be one of #{arr.inspect}") end self end |
#positive(message = nil) ⇒ Guard
Assert the value is positive
52 53 54 55 56 57 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 52 def positive( = nil) if @value.respond_to?(:>) && !(@value > 0) handle_violation( || 'value must be positive') end self end |
#valid? ⇒ Boolean
Returns true if no errors were collected.
22 23 24 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 22 def valid? @errors.empty? end |