Class: Stannum::Constraints::Types::HashType
- Inherits:
-
Stannum::Constraints::Type
- Object
- Base
- Stannum::Constraints::Type
- Stannum::Constraints::Types::HashType
- Defined in:
- lib/stannum/constraints/types/hash_type.rb
Overview
A Hash type constraint asserts that the object is a Hash.
Direct Known Subclasses
HashWithIndifferentKeys, HashWithStringKeys, HashWithSymbolKeys
Constant Summary
Constants inherited from Stannum::Constraints::Type
Stannum::Constraints::Type::NEGATED_TYPE, Stannum::Constraints::Type::TYPE
Constants inherited from Base
Base::NEGATED_TYPE, Base::TYPE
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#allow_empty? ⇒ true, false
If false, then the constraint will not match against a Hash with no keys.
-
#does_not_match?(actual) ⇒ true, false
Checks that the object is not a Hash instance.
-
#errors_for(actual, errors: nil) ⇒ Stannum::Errors
Generates an errors object for the given object.
-
#initialize(allow_empty: true, key_type: nil, value_type: nil, **options) ⇒ HashType
constructor
A new instance of HashType.
-
#key_type ⇒ Stannum::Constraints::Base?
The expected type for the keys in the hash.
-
#matches?(actual) ⇒ true, false
(also: #match?)
Checks that the object is a Hash instance and that the keys/values match.
-
#value_type ⇒ Stannum::Constraints::Base?
The expected type for the values in the hash.
Methods inherited from Stannum::Constraints::Type
#expected_type, #negated_errors_for, #with_options
Methods included from Support::Optional
#optional?, #required?, resolve
Methods inherited from Base
#==, #clone, #dup, #match, #message, #negated_errors_for, #negated_match, #negated_message, #negated_type, #type, #with_options
Constructor Details
#initialize(allow_empty: true, key_type: nil, value_type: nil, **options) ⇒ HashType
Returns a new instance of HashType.
54 55 56 57 58 59 60 61 62 |
# File 'lib/stannum/constraints/types/hash_type.rb', line 54 def initialize(allow_empty: true, key_type: nil, value_type: nil, **) super( ::Hash, allow_empty: !!allow_empty, key_type: coerce_key_type(key_type), value_type: coerce_value_type(value_type), ** ) end |
Instance Method Details
#allow_empty? ⇒ true, false
Returns if false, then the constraint will not match against a Hash with no keys.
66 67 68 |
# File 'lib/stannum/constraints/types/hash_type.rb', line 66 def allow_empty? [:allow_empty] end |
#does_not_match?(actual) ⇒ true, false
Checks that the object is not a Hash instance.
76 77 78 |
# File 'lib/stannum/constraints/types/hash_type.rb', line 76 def does_not_match?(actual) !matches_type?(actual) 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.
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/stannum/constraints/types/hash_type.rb', line 81 def errors_for(actual, errors: nil) return super unless actual.is_a?(expected_type) errors ||= Stannum::Errors.new return add_presence_error(errors) unless presence_matches?(actual) update_key_errors_for(actual: actual, errors: errors) update_value_errors_for(actual: actual, errors: errors) errors end |
#key_type ⇒ Stannum::Constraints::Base?
Returns the expected type for the keys in the hash.
97 98 99 |
# File 'lib/stannum/constraints/types/hash_type.rb', line 97 def key_type [:key_type] end |
#matches?(actual) ⇒ true, false Also known as: match?
Checks that the object is a Hash instance and that the keys/values match.
If the constraint was configured with a key_type, each key in the hash will be compared to the expected type. Likewise, if the constraint was configured with a value_type, each value in the hash will be compared. If any keys and/or values do not match the expectation, then #matches? will return false.
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/stannum/constraints/types/hash_type.rb', line 113 def matches?(actual) return false unless super return false unless presence_matches?(actual) return false unless key_type_matches?(actual) return false unless value_type_matches?(actual) true end |
#value_type ⇒ Stannum::Constraints::Base?
Returns the expected type for the values in the hash.
128 129 130 |
# File 'lib/stannum/constraints/types/hash_type.rb', line 128 def value_type [:value_type] end |