Class: Stannum::Constraints::Enum
- Defined in:
- lib/stannum/constraints/enum.rb
Overview
An enum constraint asserts that the object is one of the given values.
Constant Summary collapse
- NEGATED_TYPE =
The :type of the error generated for a matching object.
'stannum.constraints.is_in_list'
- TYPE =
The :type of the error generated for a non-matching object.
'stannum.constraints.is_not_in_list'
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#errors_for(actual, errors: nil) ⇒ Stannum::Errors
Generates an errors object for the given object.
-
#expected_values ⇒ Array
The possible values for the object.
-
#initialize(*expected_values, **options) ⇒ Enum
constructor
A new instance of Enum.
-
#matches?(actual) ⇒ true, false
(also: #match?)
Checks that the object is in the list of expected values.
-
#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_values, **options) ⇒ Enum
Returns a new instance of Enum.
26 27 28 29 30 31 32 |
# File 'lib/stannum/constraints/enum.rb', line 26 def initialize(first, *rest, **) expected_values = rest.unshift(first) super(expected_values: expected_values, **) @matching_values = Set.new(expected_values) 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.
35 36 37 |
# File 'lib/stannum/constraints/enum.rb', line 35 def errors_for(actual, errors: nil) # rubocop:disable Lint/UnusedMethodArgument (errors || Stannum::Errors.new).add(type, values: expected_values) end |
#expected_values ⇒ Array
Returns the possible values for the object.
40 41 42 |
# File 'lib/stannum/constraints/enum.rb', line 40 def expected_values [:expected_values] end |
#matches?(actual) ⇒ true, false Also known as: match?
Checks that the object is in the list of expected values.
55 56 57 |
# File 'lib/stannum/constraints/enum.rb', line 55 def matches?(actual) @matching_values.include?(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.
45 46 47 |
# File 'lib/stannum/constraints/enum.rb', line 45 def negated_errors_for(actual, errors: nil) # rubocop:disable Lint/UnusedMethodArgument (errors || Stannum::Errors.new).add(negated_type, values: expected_values) end |