Class: Stannum::Constraints::Signature

Inherits:
Base
  • Object
show all
Defined in:
lib/stannum/constraints/signature.rb

Overview

Constraint for matching objects by the methods they respond to.

Examples:

constraint = Stannum::Constraints::Signature.new(:[], :keys)

constraint.matches?(Object.new) #=> false
constraint.matches?([])         #=> false
constraint.matches?({})         #=> true

Constant Summary collapse

NEGATED_TYPE =

The :type of the error generated for a matching object.

'stannum.constraints.has_methods'
TYPE =

The :type of the error generated for a non-matching object.

'stannum.constraints.does_not_have_methods'

Instance Attribute Summary collapse

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#==, #clone, #dup, #match, #message, #negated_match, #negated_message, #negated_type, #type, #with_options

Constructor Details

#initialize(*expected_methods, **options) ⇒ Signature

Returns a new instance of Signature.

Parameters:

  • expected_methods (Array<String, Symbol>)

    The methods the object is expected to respond to.

  • options (Hash<Symbol, Object>)

    Configuration options for the constraint. Defaults to an empty Hash.



25
26
27
28
29
30
31
# File 'lib/stannum/constraints/signature.rb', line 25

def initialize(*expected_methods, **options)
  validate_expected_methods(expected_methods)

  @expected_methods = expected_methods

  super(expected_methods: expected_methods, **options)
end

Instance Attribute Details

#expected_methodsArray<String, Symbol> (readonly)

Returns the methods the object is expected to respond to.

Returns:

  • (Array<String, Symbol>)

    the methods the object is expected to respond to.



35
36
37
# File 'lib/stannum/constraints/signature.rb', line 35

def expected_methods
  @expected_methods
end

Instance Method Details

#does_not_match?(actual) ⇒ true, false

Returns true if the object does not respond to any of the expected methods; otherwise false.

Returns:

  • (true, false)

    true if the object does not respond to any of the expected methods; otherwise false.



39
40
41
# File 'lib/stannum/constraints/signature.rb', line 39

def does_not_match?(actual)
  each_missing_method(actual).to_a == expected_methods
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:



44
45
46
47
48
49
50
51
# File 'lib/stannum/constraints/signature.rb', line 44

def errors_for(actual, errors: nil)
  (errors || Stannum::Errors.new)
    .add(
      type,
      methods: expected_methods,
      missing: each_missing_method(actual).to_a
    )
end

#matches?(actual) ⇒ true, false Also known as: match?

Returns true if the object responds to all of the expected methods; otherwise false.

Returns:

  • (true, false)

    true if the object responds to all of the expected methods; otherwise false.



55
56
57
# File 'lib/stannum/constraints/signature.rb', line 55

def matches?(actual)
  each_missing_method(actual).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:



61
62
63
64
65
66
67
68
# File 'lib/stannum/constraints/signature.rb', line 61

def negated_errors_for(actual, errors: nil)
  (errors || Stannum::Errors.new)
    .add(
      negated_type,
      methods: expected_methods,
      missing: each_missing_method(actual).to_a
    )
end