Class: Stannum::Constraints::Hashes::IndifferentKey
- Defined in:
- lib/stannum/constraints/hashes/indifferent_key.rb
Overview
Constraint for validating an indifferent hash key.
To be a valid indifferent Hash key, an object must be a String or a Symbol and cannot be empty.
Constant Summary collapse
- NEGATED_TYPE =
The :type of the error generated for a matching object.
'stannum.constraints.hashes.is_string_or_symbol'
- TYPE =
The :type of the error generated for a non-matching object.
'stannum.constraints.hashes.is_not_string_or_symbol'
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.
-
#matches?(actual) ⇒ true, false
(also: #match?)
True if the object is a non-empty String or Symbol.
Methods inherited from Base
#==, #clone, #does_not_match?, #dup, #initialize, #match, #message, #negated_errors_for, #negated_match, #negated_message, #negated_type, #type, #with_options
Constructor Details
This class inherits a constructor from Stannum::Constraints::Base
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.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/stannum/constraints/hashes/indifferent_key.rb', line 50 def errors_for(actual, errors: nil) errors ||= Stannum::Errors.new return errors.add(Stannum::Constraints::Presence::TYPE) if actual.nil? return super unless indifferent_key_type?(actual) return errors unless actual.empty? errors.add(Stannum::Constraints::Presence::TYPE) end |
#matches?(actual) ⇒ true, false Also known as: match?
Returns true if the object is a non-empty String or Symbol.
63 64 65 |
# File 'lib/stannum/constraints/hashes/indifferent_key.rb', line 63 def matches?(actual) indifferent_key_type?(actual) && !actual.empty? end |