Class: Quacky::DuckTypeVerifier
- Inherits:
-
Object
- Object
- Quacky::DuckTypeVerifier
- Defined in:
- lib/quacky/duck_type_verifier.rb
Instance Method Summary collapse
-
#initialize(duck_type) ⇒ DuckTypeVerifier
constructor
A new instance of DuckTypeVerifier.
- #verify!(object) ⇒ Object
Constructor Details
#initialize(duck_type) ⇒ DuckTypeVerifier
Returns a new instance of DuckTypeVerifier.
5 6 7 |
# File 'lib/quacky/duck_type_verifier.rb', line 5 def initialize duck_type @duck_type = duck_type end |
Instance Method Details
#verify!(object) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/quacky/duck_type_verifier.rb', line 9 def verify! object duck_type_methods.each do |method| raise Quacky::DuckTypeVerificationFailure, "object does not respond to `#{method.name}'" unless object.respond_to?(method.name) begin target_method = object.public_method(method.name) return true if target_method.parameters.any? { |p| p.first == :rest } method_parameters = method.parameters.reject { |p| p.first == :block } target_method_parameters = target_method.parameters.reject { |p| p.first == :block } if target_method_parameters.count != method_parameters.count || target_method_parameters.map {|p| p.first } != method_parameters.map {|p| p.first} raise Quacky::DuckTypeVerificationFailure, "definitions of method `#{method.name}` differ in parameters accepted." end rescue NameError; end true end end |