Class: Orthoses::ActiveSupport::Delegation

Inherits:
Object
  • Object
show all
Defined in:
lib/orthoses/active_support/delegation.rb

Defined Under Namespace

Classes: Resource

Instance Method Summary collapse

Constructor Details

#initialize(loader) ⇒ Delegation

Returns a new instance of Delegation.



6
7
8
# File 'lib/orthoses/active_support/delegation.rb', line 6

def initialize(loader)
  @loader = loader
end

Instance Method Details

#callObject

def delegate(*methods, to: nil, prefix: nil, allow_nil: nil, private: nil) def delegate_missing_to(target, allow_nil: nil)



12
13
14
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/orthoses/active_support/delegation.rb', line 12

def call
  delegate = CallTracer::Lazy.new
  store = delegate.trace('Module#delegate') do
    @loader.call
  end

  resource = Resource.new(store)

  delegate.captures.each do |capture|
    receiver_name = Utils.module_name(capture.method.receiver) or next
    receiver_content = store[receiver_name]
    prefix = capture.argument[:private] ? "private " : ""

    case capture.argument[:to]
    # delegate :foo, to: Foo
    when Module
      to_module_name = Utils.module_name(capture.argument[:to]) or next
      capture.argument[:methods].each do |arg|
        if sig = resource.build_signature(to_module_name, arg, :singleton, false)
          receiver_content << "# defined by `delegate` to: #{to_module_name}"
          receiver_content << "#{prefix}#{sig}"
        else
          Orthoses.logger.warn("[ActiveSupport::Delegation] Ignore since missing type for #{to_module_name}.#{arg.inspect} in #{capture.argument.inspect}")
        end
      end
    else
      to_name = capture.argument[:to].to_s.to_sym
      tag, to_return_type = resource.find(receiver_name, to_name, :instance, false)
      if tag == :multi
        to_return_type = if to_return_type.length == 1
          to_return_type.first.type.return_type
        else
          nil
        end
      end

      case to_return_type
      when nil, RBS::Types::Bases::Any
        # no type found
        capture.argument[:methods].each do |method|
          receiver_content << "# defined by `delegate` to: #{to_return_type}##{to_name}"
          receiver_content << "#{prefix}def #{method}: (*untyped, **untyped) -> untyped"
        end
      else
        # found return type in store or env
        to_typename = to_return_type.name.relative!.to_s
        capture.argument[:methods].each do |method|
          if sig = resource.build_signature(to_typename, method, :instance, true)
            receiver_content << "# defined by `delegate` to #{to_return_type}##{to_name}"
            receiver_content << "#{prefix}#{sig}"
          else
            Orthoses.logger.warn("[ActiveSupport::Delegation] Ignore since missing type for #{to_typename}##{method.inspect} in #{capture.argument.inspect}")
          end
        end
      end
    end
  end

  store
end