Class: Multimethod::Method
- Inherits:
-
Object
- Object
- Multimethod::Method
- 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
-
#impl_name ⇒ Object
The Method’s underlying method name.
-
#multimethod ⇒ Object
The Method’s Multimethod.
-
#signature ⇒ Object
The Method’s Signature used for relative scoring of applicability to an argument list.
Instance Method Summary collapse
-
#<=>(x) ⇒ Object
Returns 0.
-
#initialize(impl_name, *args) ⇒ Method
constructor
Initialize a new Method.
-
#inspect ⇒ Object
Same as #to_s.
-
#matches_signature(signature) ⇒ Object
Returns true if this Method matches the Signature.
-
#parameter ⇒ Object
Parameters.
-
#remove_implementation ⇒ Object
Remove the method implementation from the receiver Module.
-
#score(args) ⇒ Object
Score of this Method based on the argument types.
-
#score_cached(args) ⇒ Object
Score this Method based on the argument types using a cache.
-
#to_ruby_arg ⇒ Object
Returns a string representing the Ruby parameters.
-
#to_ruby_def(name = nil) ⇒ Object
Returns the “def foo(…)” string using the implementation name by default.
-
#to_ruby_signature(name = nil) ⇒ Object
Returns a ruby signature using the implementation name by default.
-
#to_s(name = nil) ⇒ Object
Returns a string representation using the implementation name.
Constructor Details
#initialize(impl_name, *args) ⇒ Method
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_name ⇒ Object
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 |
#multimethod ⇒ Object
The Method’s Multimethod.
21 22 23 |
# File 'lib/multimethod/method.rb', line 21 def multimethod @multimethod end |
#signature ⇒ Object
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 |
#inspect ⇒ Object
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 |
#parameter ⇒ Object
Parameters
64 65 66 |
# File 'lib/multimethod/method.rb', line 64 def parameter @signature.parameter end |
#remove_implementation ⇒ Object
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_arg ⇒ Object
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 |