Class: Multimethod::Method

Inherits:
Object
  • Object
show all
Defined in:
lib/multimethod/method.rb

Overview

Represents a Method implementation in a Multimethod.

A Method is bound to a Module using a unique implementation name for the Multimethod.

A Multimethod may have multiple implementation Methods.

The Multimethod is responsible for determining the correct Method based on the relative scoring of the Method Signature.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(impl_name, *args) ⇒ Method

Initialize a new Method.

Method.new(impl_name, signature)
Method.new(impl_name, mod, name, parameter_list)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/multimethod/method.rb', line 27

def initialize(impl_name, *args)
  if args.size == 1
    @signature = args[0]
  else
    mod, name, params = *args
    raise NameError, "multimethod method name not specified" unless name && name.to_s.size > 0
    raise NameError, "multimethod method impl_name not specified" unless impl_name && impl_name.to_s.size > 0
    
    @signature = Signature.new(:mod => mod, :name => name, :parameter => params)
  end

  impl_name = Multimethod.normalize_name(impl_name)

  @impl_name = impl_name
end

Instance Attribute Details

#impl_nameObject

The Method’s underlying method name. This method name is unique.



18
19
20
# File 'lib/multimethod/method.rb', line 18

def impl_name
  @impl_name
end

#multimethodObject

The Method’s Multimethod.



21
22
23
# File 'lib/multimethod/method.rb', line 21

def multimethod
  @multimethod
end

#signatureObject

The Method’s Signature used for relative scoring of applicability to an argument list.



14
15
16
# File 'lib/multimethod/method.rb', line 14

def signature
  @signature
end

Instance Method Details

#<=>(x) ⇒ Object

Returns 0.



58
59
60
# File 'lib/multimethod/method.rb', line 58

def <=>(x)
  0
end

#inspectObject

Same as #to_s.



113
114
115
# File 'lib/multimethod/method.rb', line 113

def inspect
  to_s
end

#matches_signature(signature) ⇒ Object

Returns true if this Method matches the Signature.



45
46
47
# File 'lib/multimethod/method.rb', line 45

def matches_signature(signature)
  @signature == signature
end

#parameterObject

Parameters



64
65
66
# File 'lib/multimethod/method.rb', line 64

def parameter
  @signature.parameter
end

#remove_implementationObject

Remove the method implementation from the receiver Module.



51
52
53
54
# File 'lib/multimethod/method.rb', line 51

def remove_implementation
  # $stderr.puts "Removing implementation for #{signature.to_s} => #{impl_name}"
  signature.mod.class_eval("remove_method #{impl_name.inspect}")
end

#score(args) ⇒ Object

Score of this Method based on the argument types. The receiver type is the first element of args.



71
72
73
# File 'lib/multimethod/method.rb', line 71

def score(args)
  @signature.score(args)
end

#score_cached(args) ⇒ Object

Score this Method based on the argument types using a cache.



78
79
80
# File 'lib/multimethod/method.rb', line 78

def score_cached(args)
  @signature.score_cached(args)
end

#to_ruby_argObject

Returns a string representing the Ruby parameters.



108
109
110
# File 'lib/multimethod/method.rb', line 108

def to_ruby_arg
  @signature.to_ruby_arg
end

#to_ruby_def(name = nil) ⇒ Object

Returns the “def foo(…)” string using the implementation name by default.



93
94
95
96
# File 'lib/multimethod/method.rb', line 93

def to_ruby_def(name = nil)
  name ||= @impl_name
  @signature.to_ruby_def(name)
end

#to_ruby_signature(name = nil) ⇒ Object

Returns a ruby signature using the implementation name by default.



101
102
103
104
# File 'lib/multimethod/method.rb', line 101

def to_ruby_signature(name = nil)
  name ||= @impl_name
  @signature.to_ruby_signature(name)
end

#to_s(name = nil) ⇒ Object

Returns a string representation using the implementation name.



85
86
87
88
# File 'lib/multimethod/method.rb', line 85

def to_s(name = nil)
  name ||= @impl_name
  @signature.to_s(name)
end