Class: Stannum::Constraints::Base
- Inherits:
-
Object
- Object
- Stannum::Constraints::Base
- Defined in:
- lib/stannum/constraints/base.rb
Overview
A constraint codifies a particular expectation about an object.
Direct Known Subclasses
Stannum::Constraint, Absence, Anything, Boolean, Delegator, Enum, Equality, Format, Hashes::ExtraKeys, Hashes::IndifferentKey, Identity, Nothing, Presence, Properties::Base, Signature, Tuples::ExtraItems, Type, Union, Stannum::Contracts::Base
Defined Under Namespace
Classes: Builder
Constant Summary collapse
- NEGATED_TYPE =
The :type of the error generated for a matching object.
'stannum.constraints.valid'
- TYPE =
The :type of the error generated for a non-matching object.
'stannum.constraints.invalid'
Instance Attribute Summary collapse
-
#options ⇒ Hash<Symbol, Object>
readonly
Configuration options for the constraint.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Performs an equality comparison.
-
#clone(freeze: nil) ⇒ Stannum::Constraints::Base
Produces a shallow copy of the constraint.
-
#does_not_match?(actual) ⇒ true, false
Checks that the given object does not match the constraint.
-
#dup ⇒ Stannum::Constraints::Base
Produces a shallow copy of the constraint.
-
#errors_for(actual, errors: nil) ⇒ Stannum::Errors
Generates an errors object for the given object.
-
#initialize(**options) ⇒ Base
constructor
A new instance of Base.
-
#match(actual) ⇒ Object
Checks the given object against the constraint and returns errors, if any.
-
#matches?(actual) ⇒ true, false
(also: #match?)
Checks that the given object matches the constraint.
-
#message ⇒ String?
The default error message generated for a non-matching object.
-
#negated_errors_for(actual, errors: nil) ⇒ Stannum::Errors
Generates an errors object for the given object when negated.
-
#negated_match(actual) ⇒ Object
Checks the given object against the constraint and returns errors, if any.
-
#negated_message ⇒ String?
The default error message generated for a matching object.
-
#negated_type ⇒ String
The error type generated for a matching object.
-
#type ⇒ String
The error type generated for a non-matching object.
-
#with_options(**options) ⇒ Stannum::Constraints::Base
Creates a copy of the constraint and updates the copy’s options.
Constructor Details
#initialize(**options) ⇒ Base
Returns a new instance of Base.
33 34 35 |
# File 'lib/stannum/constraints/base.rb', line 33 def initialize(**) self. = end |
Instance Attribute Details
#options ⇒ Hash<Symbol, Object>
Returns Configuration options for the constraint.
38 39 40 |
# File 'lib/stannum/constraints/base.rb', line 38 def @options end |
Instance Method Details
#==(other) ⇒ true, false
Performs an equality comparison.
46 47 48 |
# File 'lib/stannum/constraints/base.rb', line 46 def ==(other) other.class == self.class && == other. end |
#clone(freeze: nil) ⇒ Stannum::Constraints::Base
Produces a shallow copy of the constraint.
57 58 59 |
# File 'lib/stannum/constraints/base.rb', line 57 def clone(freeze: nil) super.copy_properties(self) end |
#does_not_match?(actual) ⇒ true, false
Checks that the given object does not match the constraint.
81 82 83 |
# File 'lib/stannum/constraints/base.rb', line 81 def does_not_match?(actual) # rubocop:disable Naming/PredicatePrefix !matches?(actual) end |
#dup ⇒ Stannum::Constraints::Base
Produces a shallow copy of the constraint.
88 89 90 |
# File 'lib/stannum/constraints/base.rb', line 88 def dup super.copy_properties(self) end |
#errors_for(actual, errors: nil) ⇒ Stannum::Errors
This method should only be called for an object that does not match the constraint. Generating errors for a matching object can result in undefined behavior.
Generates an errors object for the given object.
The errors object represents the difference between the given object and the expected properties or behavior. It may be the same for all objects, or different based on the details of the object or the constraint.
118 119 120 |
# File 'lib/stannum/constraints/base.rb', line 118 def errors_for(actual, errors: nil) # rubocop:disable Lint/UnusedMethodArgument (errors || Stannum::Errors.new).add(type, message:) end |
#match(actual) ⇒ Object
Checks the given object against the constraint and returns errors, if any.
This method checks the given object against the expected properties or behavior. If the object matches the constraint, #match will return true. If the object does not match the constraint, #match will return false and the generated errors for that object.
150 151 152 153 154 |
# File 'lib/stannum/constraints/base.rb', line 150 def match(actual) return [true, Stannum::Errors.new] if matches?(actual) [false, errors_for(actual)] end |
#matches?(actual) ⇒ true, false Also known as: match?
177 178 179 |
# File 'lib/stannum/constraints/base.rb', line 177 def matches?(_actual) false end |
#message ⇒ String?
Returns the default error message generated for a non-matching object.
184 185 186 |
# File 'lib/stannum/constraints/base.rb', line 184 def [:message] end |
#negated_errors_for(actual, errors: nil) ⇒ Stannum::Errors
This method should only be called for an object that matches the constraint. Generating errors for a matching object can result in undefined behavior.
Generates an errors object for the given object when negated.
The errors object represents the difference between the given object and the expected properties or behavior when the constraint is negated. It may be the same for all objects, or different based on the details of the object or the constraint.
215 216 217 218 |
# File 'lib/stannum/constraints/base.rb', line 215 def negated_errors_for(actual, errors: nil) # rubocop:disable Lint/UnusedMethodArgument (errors || Stannum::Errors.new) .add(negated_type, message: ) end |
#negated_match(actual) ⇒ Object
Checks the given object against the constraint and returns errors, if any.
This method checks the given object against the expected properties or behavior. If the object matches the constraint, #negated_match will return false and the generated errors for that object. If the object does not match the constraint, #negated_match will return true.
249 250 251 252 253 |
# File 'lib/stannum/constraints/base.rb', line 249 def negated_match(actual) return [true, Stannum::Errors.new] if does_not_match?(actual) [false, negated_errors_for(actual)] end |
#negated_message ⇒ String?
Returns The default error message generated for a matching object.
257 258 259 |
# File 'lib/stannum/constraints/base.rb', line 257 def [:negated_message] end |
#negated_type ⇒ String
Returns the error type generated for a matching object.
262 263 264 |
# File 'lib/stannum/constraints/base.rb', line 262 def negated_type .fetch(:negated_type, self.class::NEGATED_TYPE) end |
#type ⇒ String
Returns the error type generated for a non-matching object.
267 268 269 |
# File 'lib/stannum/constraints/base.rb', line 267 def type .fetch(:type, self.class::TYPE) end |
#with_options(**options) ⇒ Stannum::Constraints::Base
Creates a copy of the constraint and updates the copy’s options.
276 277 278 |
# File 'lib/stannum/constraints/base.rb', line 276 def (**) dup.copy_properties(self, options: self..merge()) end |