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
-
#between(min, max, message: nil) ⇒ Guard
Assert the value is between min and max (inclusive).
-
#ends_with(suffix, message: nil) ⇒ Guard
Assert the value ends with the given suffix.
-
#format(pattern, message: nil) ⇒ Guard
Assert the value matches a pattern.
-
#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.
-
#is_a(type, message: nil) ⇒ Guard
Assert the value is an instance of the given type.
-
#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.
-
#max_length(n, message: nil) ⇒ Guard
Assert the value has a maximum length.
-
#min_length(n, message: nil) ⇒ Guard
Assert the value has a minimum length.
-
#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.
-
#present(message: nil) ⇒ Guard
Assert the value is not nil, not empty, and not blank (for strings).
-
#satisfies(message: nil, &block) ⇒ Guard
Assert the value satisfies a custom predicate.
-
#starts_with(prefix, message: nil) ⇒ Guard
Assert the value starts with the given prefix.
-
#valid? ⇒ Boolean
True if no errors were collected.
Constructor Details
#initialize(value, soft: false) ⇒ Guard
Returns a new instance of Guard.
17 18 19 20 21 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 17 def initialize(value, soft: false) @value = value @soft = soft @errors = [] end |
Instance Attribute Details
#errors ⇒ Array<String> (readonly)
Returns collected errors (soft mode only).
27 28 29 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 27 def errors @errors end |
#value ⇒ Object (readonly)
Returns the guarded value.
24 25 26 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 24 def value @value end |
Instance Method Details
#between(min, max, message: nil) ⇒ Guard
Assert the value is between min and max (inclusive)
129 130 131 132 133 134 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 129 def between(min, max, message: nil) if @value.respond_to?(:<) && @value.respond_to?(:>) && (@value < min || @value > max) handle_violation( || "value must be between #{min} and #{max}") end self end |
#ends_with(suffix, message: nil) ⇒ Guard
Assert the value ends with the given suffix
187 188 189 190 191 192 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 187 def ends_with(suffix, message: nil) if @value.respond_to?(:end_with?) && !@value.end_with?(suffix) handle_violation( || "value must end with #{suffix.inspect}") end self end |
#format(pattern, message: nil) ⇒ Guard
Assert the value matches a pattern
214 215 216 217 218 219 220 221 222 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 214 def format(pattern, message: nil) regex = if pattern.is_a?(Symbol) BUILT_IN_PATTERNS.fetch(pattern) { raise ArgumentError, "unknown built-in pattern: #{pattern.inspect}" } else pattern end handle_violation( || "value must match #{pattern.inspect} format") unless regex.match?(@value.to_s) self end |
#gte(n, message = nil) ⇒ Guard
Assert the value is greater than or equal to n
66 67 68 69 70 71 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 66 def gte(n, = nil) if @value.respond_to?(:>=) && @value < n handle_violation( || "value must be greater than or equal to #{n}") end self end |
#is_a(type, message: nil) ⇒ Guard
Assert the value is an instance of the given type
118 119 120 121 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 118 def is_a(type, message: nil) handle_violation( || "value must be a #{type}") unless @value.is_a?(type) self end |
#lte(n, message = nil) ⇒ Guard
Assert the value is less than or equal to n
78 79 80 81 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 78 def lte(n, = nil) handle_violation( || "value must be less than or equal to #{n}") if @value.respond_to?(:<=) && @value > n self end |
#matches(regex, message = nil) ⇒ Guard
Assert the value matches a regex pattern
88 89 90 91 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 88 def matches(regex, = nil) handle_violation( || "value must match #{regex.inspect}") unless regex.match?(@value.to_s) self end |
#max_length(n, message: nil) ⇒ Guard
Assert the value has a maximum length
153 154 155 156 157 158 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 153 def max_length(n, message: nil) if @value.respond_to?(:length) && @value.length > n handle_violation( || "value must have a maximum length of #{n}") end self end |
#min_length(n, message: nil) ⇒ Guard
Assert the value has a minimum length
141 142 143 144 145 146 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 141 def min_length(n, message: nil) if @value.respond_to?(:length) && @value.length < n handle_violation( || "value must have a minimum length of #{n}") end self end |
#not_empty(message = nil) ⇒ Guard
Assert the value is not empty
47 48 49 50 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 47 def not_empty( = nil) handle_violation( || 'value must not be empty') if @value.respond_to?(:empty?) && @value.empty? self end |
#not_equal(other, message = nil) ⇒ Guard
Assert the value is not equal to another value
108 109 110 111 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 108 def not_equal(other, = nil) handle_violation( || "value must not be equal to #{other.inspect}") if @value == other self end |
#not_nil(message = nil) ⇒ Guard
Assert the value is not nil
38 39 40 41 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 38 def not_nil( = nil) handle_violation( || 'value must not be nil') if @value.nil? self end |
#one_of(arr, message = nil) ⇒ Guard
Assert the value is one of the given options
98 99 100 101 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 98 def one_of(arr, = nil) handle_violation( || "value must be one of #{arr.inspect}") unless arr.include?(@value) self end |
#positive(message = nil) ⇒ Guard
Assert the value is positive
56 57 58 59 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 56 def positive( = nil) handle_violation( || 'value must be positive') if @value.respond_to?(:>) && @value <= 0 self end |
#present(message: nil) ⇒ Guard
Assert the value is not nil, not empty, and not blank (for strings)
198 199 200 201 202 203 204 205 206 207 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 198 def present(message: nil) if @value.nil? handle_violation( || 'value must be present') elsif @value.respond_to?(:empty?) && @value.empty? handle_violation( || 'value must be present') elsif @value.is_a?(String) && @value.strip.empty? handle_violation( || 'value must be present') end self end |
#satisfies(message: nil, &block) ⇒ Guard
Assert the value satisfies a custom predicate
165 166 167 168 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 165 def satisfies(message: nil, &block) handle_violation( || 'value does not satisfy the condition') unless block.call(@value) self end |
#starts_with(prefix, message: nil) ⇒ Guard
Assert the value starts with the given prefix
175 176 177 178 179 180 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 175 def starts_with(prefix, message: nil) if @value.respond_to?(:start_with?) && !@value.start_with?(prefix) handle_violation( || "value must start with #{prefix.inspect}") end self end |
#valid? ⇒ Boolean
Returns true if no errors were collected.
30 31 32 |
# File 'lib/philiprehberger/guard_clause/guard.rb', line 30 def valid? @errors.empty? end |