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, 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 60 61 |
# File 'lib/stannum/constraints/base.rb', line 57 def clone(freeze: nil) freeze = true if freeze.nil? && RUBY_VERSION <= '3.0.0' super(freeze: freeze).copy_properties(self) end |
#does_not_match?(actual) ⇒ true, false
Checks that the given object does not match the constraint.
83 84 85 |
# File 'lib/stannum/constraints/base.rb', line 83 def does_not_match?(actual) !matches?(actual) end |
#dup ⇒ Stannum::Constraints::Base
Produces a shallow copy of the constraint.
90 91 92 |
# File 'lib/stannum/constraints/base.rb', line 90 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.
120 121 122 |
# File 'lib/stannum/constraints/base.rb', line 120 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.
152 153 154 155 156 |
# File 'lib/stannum/constraints/base.rb', line 152 def match(actual) return [true, Stannum::Errors.new] if matches?(actual) [false, errors_for(actual)] end |
#matches?(actual) ⇒ true, false Also known as: match?
179 180 181 |
# File 'lib/stannum/constraints/base.rb', line 179 def matches?(_actual) false end |
#message ⇒ String?
Returns the default error message generated for a non-matching object.
186 187 188 |
# File 'lib/stannum/constraints/base.rb', line 186 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.
217 218 219 220 |
# File 'lib/stannum/constraints/base.rb', line 217 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.
251 252 253 254 255 |
# File 'lib/stannum/constraints/base.rb', line 251 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.
259 260 261 |
# File 'lib/stannum/constraints/base.rb', line 259 def [:negated_message] end |
#negated_type ⇒ String
Returns the error type generated for a matching object.
264 265 266 |
# File 'lib/stannum/constraints/base.rb', line 264 def negated_type .fetch(:negated_type, self.class::NEGATED_TYPE) end |
#type ⇒ String
Returns the error type generated for a non-matching object.
269 270 271 |
# File 'lib/stannum/constraints/base.rb', line 269 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.
278 279 280 |
# File 'lib/stannum/constraints/base.rb', line 278 def (**) dup.copy_properties(self, options: self..merge()) end |