class Stannum::Constraints::Signature
Constraint
for matching objects by the methods they respond to.
@example
constraint = Stannum::Constraints::Signature.new(:[], :keys) constraint.matches?(Object.new) #=> false constraint.matches?([]) #=> false constraint.matches?({}) #=> true
Constants
- NEGATED_TYPE
The :type of the error generated for a matching object.
- TYPE
The :type of the error generated for a non-matching object.
Attributes
expected_methods[R]
@return [Array<String, Symbol>] the methods the object is expected to
respond to.
Public Class Methods
new(*expected_methods, **options)
click to toggle source
@param expected_methods
[Array<String, Symbol>] The methods the object is
expected to respond to.
@param options [Hash<Symbol, Object>] Configuration options for the
constraint. Defaults to an empty Hash.
Calls superclass method
Stannum::Constraints::Base::new
# 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
Public Instance Methods
does_not_match?(actual)
click to toggle source
@return [true, false] true if the object does not respond to any of the
expected methods; otherwise false.
# 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)
click to toggle source
(see Stannum::Constraints::Base#errors_for
)
# 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)
click to toggle source
@return [true, false] true if the object responds to all of the expected
methods; otherwise false.
# File lib/stannum/constraints/signature.rb, line 55 def matches?(actual) each_missing_method(actual).none? end
Also aliased as: match?
negated_errors_for(actual, errors: nil)
click to toggle source
(see Stannum::Constraints::Base#negated_errors_for
)
# 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
Private Instance Methods
each_missing_method(actual) { |method_name| ... }
click to toggle source
# File lib/stannum/constraints/signature.rb, line 72 def each_missing_method(actual) return enum_for(:each_missing_method, actual) unless block_given? expected_methods.each do |method_name| yield method_name unless actual.respond_to?(method_name) end end
validate_expected_methods(expected_methods)
click to toggle source
# File lib/stannum/constraints/signature.rb, line 80 def validate_expected_methods(expected_methods) if expected_methods.empty? raise ArgumentError, 'expected methods can\'t be blank', caller(1..-1) end return if expected_methods.all? do |method_name| method_name.is_a?(String) || method_name.is_a?(Symbol) end raise ArgumentError, 'expected method must be a String or Symbol' end