Class: Stannum::Constraints::Union
- Defined in:
- lib/stannum/constraints/union.rb
Overview
Asserts that the object matches one of the given constraints.
Constant Summary collapse
- NEGATED_TYPE =
The :type of the error generated for a matching object.
'stannum.constraints.is_in_union'
- TYPE =
The :type of the error generated for a non-matching object.
'stannum.constraints.is_not_in_union'
Instance Attribute Summary collapse
-
#expected_constraints ⇒ Array<Stannum::Constraints::Base>
readonly
The possible values for the object.
Attributes inherited from Base
Instance Method Summary collapse
-
#errors_for(actual, errors: nil) ⇒ Stannum::Errors
Generates an errors object for the given object.
-
#initialize(*expected_constraints, **options) ⇒ Union
constructor
A new instance of Union.
-
#matches?(actual) ⇒ true, false
(also: #match?)
Checks that the object matches at least one of the given constraints.
-
#negated_errors_for(actual, errors: nil) ⇒ Stannum::Errors
Generates an errors object for the given object when negated.
Methods inherited from Base
#==, #clone, #does_not_match?, #dup, #match, #message, #negated_match, #negated_message, #negated_type, #type, #with_options
Constructor Details
#initialize(*expected_constraints, **options) ⇒ Union
Returns a new instance of Union.
31 32 33 34 35 36 37 |
# File 'lib/stannum/constraints/union.rb', line 31 def initialize(first, *rest, **) expected_constraints = rest.unshift(first) super(expected_constraints: expected_constraints, **) @expected_constraints = expected_constraints end |
Instance Attribute Details
#expected_constraints ⇒ Array<Stannum::Constraints::Base> (readonly)
Returns the possible values for the object.
41 42 43 |
# File 'lib/stannum/constraints/union.rb', line 41 def expected_constraints @expected_constraints end |
Instance Method Details
#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.
44 45 46 |
# File 'lib/stannum/constraints/union.rb', line 44 def errors_for(actual, errors: nil) # rubocop:disable Lint/UnusedMethodArgument (errors || Stannum::Errors.new).add(type, constraints: expected_values) end |
#matches?(actual) ⇒ true, false Also known as: match?
Checks that the object matches at least one of the given constraints.
54 55 56 |
# File 'lib/stannum/constraints/union.rb', line 54 def matches?(actual) expected_constraints.any? { |constraint| constraint.matches?(actual) } 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.
60 61 62 63 |
# File 'lib/stannum/constraints/union.rb', line 60 def negated_errors_for(actual, errors: nil) # rubocop:disable Lint/UnusedMethodArgument (errors || Stannum::Errors.new) .add(negated_type, constraints: negated_values) end |