Class: RR::WildcardMatchers::DuckType

Inherits:
Object
  • Object
show all
Defined in:
lib/rr/wildcard_matchers/duck_type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*required_methods) ⇒ DuckType



6
7
8
# File 'lib/rr/wildcard_matchers/duck_type.rb', line 6

def initialize(*required_methods)
  @required_methods = required_methods
end

Instance Attribute Details

#required_methodsObject

Returns the value of attribute required_methods



4
5
6
# File 'lib/rr/wildcard_matchers/duck_type.rb', line 4

def required_methods
  @required_methods
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



25
26
27
28
# File 'lib/rr/wildcard_matchers/duck_type.rb', line 25

def ==(other)
  return false unless other.is_a?(self.class)
  self.required_methods == other.required_methods
end

#inspectObject



18
19
20
21
22
23
# File 'lib/rr/wildcard_matchers/duck_type.rb', line 18

def inspect
  formatted_required_methods = required_methods.collect do |method_name|
    method_name.inspect
  end.join(', ')
  "duck_type(#{formatted_required_methods})"
end

#wildcard_match?(other) ⇒ Boolean



10
11
12
13
14
15
16
# File 'lib/rr/wildcard_matchers/duck_type.rb', line 10

def wildcard_match?(other)
  return true if self == other
  required_methods.each do |m|
    return false unless other.respond_to?(m)
  end
  return true
end