Class: Orthoses::DelegateClass
- Inherits:
-
Object
- Object
- Orthoses::DelegateClass
- Defined in:
- lib/orthoses/delegate_class.rb
Defined Under Namespace
Modules: Hook
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(loader) ⇒ DelegateClass
constructor
A new instance of DelegateClass.
- #temporary_type_params(name) ⇒ Object
Constructor Details
#initialize(loader) ⇒ DelegateClass
Returns a new instance of DelegateClass.
11 12 13 |
# File 'lib/orthoses/delegate_class.rb', line 11 def initialize(loader) @loader = loader end |
Instance Method Details
#call ⇒ Object
15 16 17 18 19 20 21 22 23 24 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 |
# File 'lib/orthoses/delegate_class.rb', line 15 def call require 'delegate' ::Class.prepend(Hook) inherited = CallTracer.new delegate_class_super_map = {} delegate_class_tracer = TracePoint.new(:return) do |tp| return_value = tp.return_value superclass = tp.binding.local_variable_get(:superclass) delegate_class_super_map[return_value] = superclass end store = delegate_class_tracer.enable(target: method(:DelegateClass)) do inherited.trace(Hook.instance_method(:inherited)) do @loader.call end end store["Class"].body.delete("prepend Orthoses::DelegateClass::Hook") inherited.captures.each do |capture| superclass = capture.method.receiver if delegate_to_class = delegate_class_super_map[superclass] subclass = capture.argument[:subclass] subclass_name = Utils.module_name(subclass) next unless subclass_name delegate_to_class_name = Utils.module_name(delegate_to_class) next unless delegate_to_class_name header = "class #{subclass_name} < ::#{delegate_to_class_name}#{temporary_type_params(delegate_to_class_name)}" store[subclass_name].header = header end end store end |
#temporary_type_params(name) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/orthoses/delegate_class.rb', line 54 def temporary_type_params(name) Utils.known_type_params(name)&.then do |params| if params.empty? nil else "[#{params.map { :untyped }.join(', ')}]" end end end |