Class: DesignByContract::Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/design_by_contract/interface.rb

Instance Method Summary collapse

Constructor Details

#initialize(method_specifications) ⇒ Interface

Returns a new instance of Interface.



2
3
4
5
6
# File 'lib/design_by_contract/interface.rb', line 2

def initialize(method_specifications)
  @method_specifications = method_specifications.reduce({}) do |ms, (name, raw_signature)|
    ms.merge(name => DesignByContract::Signature.new(raw_signature))
  end
end

Instance Method Details

#==(oth_interface) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/design_by_contract/interface.rb', line 30

def ==(oth_interface)
  return false unless @method_specifications.length == oth_interface.method_specifications.length

  @method_specifications.each do |name, spec|
    return false unless oth_interface.method_specifications[name] && oth_interface.method_specifications[name] == spec
  end

  return true
end

#fulfilled_by?(object) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
# File 'lib/design_by_contract/interface.rb', line 16

def fulfilled_by?(object)
  @method_specifications.each do |name, signature|
    return false unless object.respond_to?(name)
    return false unless signature.match?(object.method(name))
  end
  true
end

#implemented_by?(implementator_class) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
# File 'lib/design_by_contract/interface.rb', line 8

def implemented_by?(implementator_class)
  @method_specifications.each do |name, signature|
    return false unless implementator_class.method_defined?(name)
    return false unless signature.match?(implementator_class.instance_method(name))
  end
  true
end

#match?(method) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/design_by_contract/interface.rb', line 24

def match?(method)
  signature = @method_specifications[method.original_name]

  signature.match?(method)
end

#rawObject



40
41
42
43
44
# File 'lib/design_by_contract/interface.rb', line 40

def raw
  @method_specifications.reduce({}) do |hash, (k,v)|
    hash.merge(k => v.raw)
  end
end