Class: Stannum::Constraints::Type
- Includes:
- Support::Optional
- Defined in:
- lib/stannum/constraints/type.rb
Overview
A type constraint asserts that the object is of the expected type.
Direct Known Subclasses
Stannum::Constraints::Types::ArrayType, Stannum::Constraints::Types::BigDecimalType, Stannum::Constraints::Types::DateTimeType, Stannum::Constraints::Types::DateType, Stannum::Constraints::Types::FloatType, Stannum::Constraints::Types::HashType, Stannum::Constraints::Types::IntegerType, Stannum::Constraints::Types::NilType, Stannum::Constraints::Types::ProcType, Stannum::Constraints::Types::StringType, Stannum::Constraints::Types::SymbolType, Stannum::Constraints::Types::TimeType
Constant Summary collapse
- NEGATED_TYPE =
The :type of the error generated for a matching object.
'stannum.constraints.is_type'
- TYPE =
The :type of the error generated for a non-matching object.
'stannum.constraints.is_not_type'
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_type ⇒ Class, ...
The type the object is expected to belong to.
-
#initialize(expected_type, optional: nil, required: nil, **options) ⇒ Type
constructor
A new instance of Type.
-
#matches?(actual) ⇒ true, false
(also: #match?)
Checks that the object is an instance of the expected type.
-
#negated_errors_for(actual, errors: nil) ⇒ Stannum::Errors
Generates an errors object for the given object when negated.
-
#with_options(**options) ⇒ Stannum::Constraints::Base
Creates a copy of the constraint and updates the copy’s options.
Methods included from Support::Optional
#optional?, #required?, resolve
Methods inherited from Base
#==, #clone, #does_not_match?, #dup, #match, #message, #negated_match, #negated_message, #negated_type, #type
Constructor Details
#initialize(expected_type, optional: nil, required: nil, **options) ⇒ Type
Returns a new instance of Type.
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/stannum/constraints/type.rb', line 46 def initialize(expected_type, optional: nil, required: nil, **) expected_type = resolve_expected_type(expected_type) super( expected_type: expected_type, **resolve_required_option( optional: optional, required: required, ** ) ) 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.
60 61 62 |
# File 'lib/stannum/constraints/type.rb', line 60 def errors_for(actual, errors: nil) # rubocop:disable Lint/UnusedMethodArgument (errors || Stannum::Errors.new).add(type, **error_properties) end |
#expected_type ⇒ Class, ...
Returns the type the object is expected to belong to.
66 67 68 |
# File 'lib/stannum/constraints/type.rb', line 66 def expected_type [:expected_type] end |
#matches?(actual) ⇒ true, false Also known as: match?
Checks that the object is an instance of the expected type.
76 77 78 |
# File 'lib/stannum/constraints/type.rb', line 76 def matches?(actual) matches_type?(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.
82 83 84 |
# File 'lib/stannum/constraints/type.rb', line 82 def negated_errors_for(actual, errors: nil) # rubocop:disable Lint/UnusedMethodArgument (errors || Stannum::Errors.new).add(negated_type, **error_properties) end |
#with_options(**options) ⇒ Stannum::Constraints::Base
Creates a copy of the constraint and updates the copy’s options.
87 88 89 90 91 |
# File 'lib/stannum/constraints/type.rb', line 87 def (**) = .merge(required_by_default: required?) super(**resolve_required_option(**)) end |