Class: Stannum::Constraints::Signature
- Defined in:
- lib/stannum/constraints/signature.rb
Overview
Constraint for matching objects by the methods they respond to.
Direct Known Subclasses
Stannum::Constraints::Signatures::Map, Stannum::Constraints::Signatures::Tuple
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
-
#expected_methods ⇒ Array<String, Symbol>
readonly
The methods the object is expected to respond to.
Attributes inherited from Base
Instance Method Summary collapse
-
#does_not_match?(actual) ⇒ true, false
True if the object does not respond to any of the expected methods; otherwise false.
-
#errors_for(actual, errors: nil) ⇒ Stannum::Errors
Generates an errors object for the given object.
-
#initialize(*expected_methods, **options) ⇒ Signature
constructor
A new instance of Signature.
-
#matches?(actual) ⇒ true, false
(also: #match?)
True if the object responds to all of the expected methods; otherwise false.
-
#negated_errors_for(actual, errors: nil) ⇒ Stannum::Errors
Generates an errors object for the given object when negated.
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.
25 26 27 28 29 30 31 |
# File 'lib/stannum/constraints/signature.rb', line 25 def initialize(*expected_methods, **) validate_expected_methods(expected_methods) @expected_methods = expected_methods super(expected_methods: expected_methods, **) end |
Instance Attribute Details
#expected_methods ⇒ Array<String, Symbol> (readonly)
Returns 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.
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
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.
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.
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
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.
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 |