Class: Multimethod::Table
- Inherits:
-
Object
- Object
- Multimethod::Table
- Defined in:
- lib/multimethod/table.rb
Constant Summary collapse
- @@instance =
nil- @@hook_var =
:@@__multimethods- @@match_def =
/^\s*def\s+(\w+)([(](.*)[)])?/
Instance Attribute Summary collapse
-
#method ⇒ Object
Returns the value of attribute method.
Class Method Summary collapse
Instance Method Summary collapse
-
#dispatch(name, rcvr, args) ⇒ Object
Interface to code generated by #install_dispatch.
- #find_method(x) ⇒ Object
- #find_multimethod(x) ⇒ Object
-
#initialize(*opts) ⇒ Table
constructor
A new instance of Table.
-
#install_method(mod, body, file = nil, line = nil) ⇒ Object
Interface to Multimethod::Module mixin multimethod.
- #lookup_multimethod(name) ⇒ Object
-
#name_to_object(name, scope = nil, file = nil, line = nil) ⇒ Object
Support.
- #remove_method(signature) ⇒ Object
Constructor Details
#initialize(*opts) ⇒ Table
Returns a new instance of Table.
14 15 16 17 18 19 20 |
# File 'lib/multimethod/table.rb', line 14 def initialize(*opts) @multimethod_by_name = { } @multimethod = [ ] # Type name lookup cache @name_to_object = { } end |
Instance Attribute Details
#method ⇒ Object
Returns the value of attribute method.
13 14 15 |
# File 'lib/multimethod/table.rb', line 13 def method @method end |
Class Method Details
.instance ⇒ Object
5 6 7 8 9 |
# File 'lib/multimethod/table.rb', line 5 def self.instance # THREAD CRITICAL BEGIN @@instance ||= self.new # TRREAD CRITICAL END end |
Instance Method Details
#dispatch(name, rcvr, args) ⇒ Object
Interface to code generated by #install_dispatch.
114 115 116 117 118 119 |
# File 'lib/multimethod/table.rb', line 114 def dispatch(name, rcvr, args) unless mm = @multimethod_by_name[name] raise NameError, 'No method for multmethod #{name}' unless mm end mm.dispatch(rcvr, args) end |
#find_method(x) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/multimethod/table.rb', line 72 def find_method(x) case x when String signature = Signature.new(:string => x) when Method signature = x.signature end x = @multimethod.select{|mm| mm.matches_signature(signature)} # $stderr.puts "find_method(#{x}) => #{x.inspect}" x = x.collect{|mm| mm.find_method(signature)}.flatten # $stderr.puts "find_method(#{x}) => #{x.inspect}" x end |
#find_multimethod(x) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/multimethod/table.rb', line 58 def find_multimethod(x) case x when String signature = Signature.new(:string => x) when Method signature = x.signature end x = @multimethod.select{|mm| mm.matches_signature(signature)} x end |
#install_method(mod, body, file = nil, line = nil) ⇒ Object
Interface to Multimethod::Module mixin multimethod
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/multimethod/table.rb', line 25 def install_method(mod, body, file = nil, line = nil) file ||= __FILE__ line ||= __LINE__ verbose = nil #if body =~ /def bar\(x, y\)/ # verbose = 1 #end # Parse the signature from the method body signature = Signature.new signature.mod = mod signature.verbose = verbose signature.file = file signature.line = line new_body = signature.scan_string(body.clone) # Get our Multimethod for this mm = lookup_multimethod(signature.name) mm.install_dispatch(mod) m = mm.new_method_from_signature(signature) # Replace the multimethod signature with a plain Ruby signature. new_body = m.to_ruby_def + new_body #if true || m.signature.restarg # $stderr.puts "install_method(#{mod}) => #{m.to_ruby_signature}:\n#{new_body}" #end # Evaluate the new method body. mod.module_eval(new_body, file, line) end |
#lookup_multimethod(name) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/multimethod/table.rb', line 97 def lookup_multimethod(name) name = name.to_s # THREAD CRITICAL BEGIN unless mm = @multimethod_by_name[name] mm = Multimethod.new(name) mm.table = self @multimethod_by_name[name] = mm @multimethod << mm end # THREAD CRITICAL END mm end |
#name_to_object(name, scope = nil, file = nil, line = nil) ⇒ Object
Support
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/multimethod/table.rb', line 126 def name_to_object(name, scope = nil, file = nil, line = nil) scope ||= Kernel # THREAD CRITICAL BEGIN unless x = (@name_to_object[scope] ||= { })[name] # $stderr.puts " name_to_object(#{name.inspect}) in #{scope}" x = @name_to_object[scope][name] = scope.module_eval(name, file || __FILE__, line || __LINE__) end # THREAD CRITICAL END x end |
#remove_method(signature) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/multimethod/table.rb', line 89 def remove_method(signature) x = find_method(signature) raise("Found #{x.size} multimethods: #{x.inspect}") if x.size > 1 x = x[0] x.multimethod.remove_method(x) end |