Class: Stannum::Constraints::Properties::MatchProperty
Overview
Compares the properties of the given object with the specified property.
If all of the property values equal the expected value, the constraint will match the object; otherwise, if there are any non-matching values, the constraint will not match.
Constant Summary collapse
- NEGATED_TYPE =
The :type of the error generated for a matching object.
Stannum::Constraints::Equality::NEGATED_TYPE
- TYPE =
The :type of the error generated for a non-matching object.
Stannum::Constraints::Equality::TYPE
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Matching
Attributes inherited from Base
Attributes inherited from Base
Instance Method Summary collapse
-
#does_not_match?(actual) ⇒ true, false
False if any of the property values match the reference property value; otherwise true.
-
#errors_for(actual, errors: nil) ⇒ Stannum::Errors
Generates an errors object for the given object.
-
#matches?(actual) ⇒ true, false
(also: #match?)
True if the property values match the reference property value; otherwise false.
-
#negated_errors_for(actual, errors: nil) ⇒ Stannum::Errors
Generates an errors object for the given object when negated.
Methods inherited from Matching
Methods inherited from Base
#allow_empty?, #allow_nil?, #initialize
Methods inherited from Base
#==, #clone, #dup, #initialize, #match, #message, #negated_match, #negated_message, #negated_type, #type, #with_options
Constructor Details
This class inherits a constructor from Stannum::Constraints::Properties::Matching
Instance Method Details
#does_not_match?(actual) ⇒ true, false
Returns false if any of the property values match the reference property value; otherwise true.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/stannum/constraints/properties/match_property.rb', line 44 def does_not_match?(actual) return false unless can_match_properties?(actual) expected = expected_value(actual) return false if skip_property?(expected) each_matching_property( actual: actual, expected: expected, include_all: true ) .none? 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.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/stannum/constraints/properties/match_property.rb', line 60 def errors_for(actual, errors: nil) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength errors ||= Stannum::Errors.new return invalid_object_errors(errors) unless can_match_properties?(actual) expected = expected_value(actual) matching = each_non_matching_property(actual: actual, expected: expected) return generic_errors(errors) if matching.count.zero? matching.each do |property_name, value| errors[property_name].add( type, message: , expected: filter_parameters? ? '[FILTERED]' : expected_value(actual), actual: filter_parameters? ? '[FILTERED]' : value ) end errors end |
#matches?(actual) ⇒ true, false Also known as: match?
Returns true if the property values match the reference property value; otherwise false.
84 85 86 87 88 89 90 91 92 |
# File 'lib/stannum/constraints/properties/match_property.rb', line 84 def matches?(actual) return false unless can_match_properties?(actual) expected = expected_value(actual) return true if skip_property?(expected) each_non_matching_property(actual: actual, expected: expected).none? 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.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/stannum/constraints/properties/match_property.rb', line 96 def negated_errors_for(actual, errors: nil) # rubocop:disable Metrics/MethodLength errors ||= Stannum::Errors.new return invalid_object_errors(errors) unless can_match_properties?(actual) expected = expected_value(actual) matching = each_matching_property( actual: actual, expected: expected, include_all: true ) return generic_errors(errors) if matching.count.zero? matching.each do |property_name, _| errors[property_name].add(negated_type, message: ) end errors end |