Class: Stannum::Constraints::Properties::MatchProperty

Inherits:
Matching show all
Defined in:
lib/stannum/constraints/properties/match_property.rb

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.

Examples:

Using an Properties::Match constraint

ConfirmPassword = Struct.new(:password, :confirmation)
constraint    = Stannum::Constraints::Properties::MatchProperty.new(
  :password,
  :confirmation
)

params = ConfirmPassword.new('tronlives', 'ifightfortheusers')
constraint.matches?(params)
#=> false
constraint.errors_for(params)
#=> [
  {
    path: [:confirmation],
    type: 'stannum.constraints.is_not_equal_to',
    data: { expected: '[FILTERED]', actual: '[FILTERED]' }
  }
]

params = ConfirmPassword.new('tronlives', 'tronlives')
constraint.matches?(params)
#=> true

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

Base::FILTERED_PARAMETERS

Instance Attribute Summary

Attributes inherited from Matching

#reference_name

Attributes inherited from Base

#property_names

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Matching

#initialize

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.

Returns:

  • (true, false)

    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

Note:

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.

Examples:

Generating errors for a non-matching object.

constraint = CustomConstraint.new
object     = NonMatchingObject.new
errors     = constraint.errors_for(object)

errors.class #=> Stannum::Errors
errors.to_a  #=> [{ type: 'some_error', message: 'some error message' }]

Parameters:

  • actual (Object)

    The object to generate errors for.

  • errors (Stannum::Errors) (defaults to: nil)

    The errors object to append errors to. If an errors object is not given, a new errors object will be created.

Returns:

See Also:



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:  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.

Returns:

  • (true, false)

    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

Note:

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.

Examples:

Generating errors for a matching object.

constraint = CustomConstraint.new
object     = MatchingObject.new
errors     = constraint.negated_errors_for(object)

errors.class #=> Stannum::Errors
errors.to_a  #=> [{ type: 'some_error', message: 'some error message' }]

Parameters:

  • actual (Object)

    The object to generate errors for.

  • errors (Stannum::Errors) (defaults to: nil)

    The errors object to append errors to. If an errors object is not given, a new errors object will be created.

Returns:

See Also:



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: negated_message)
  end

  errors
end