Class: Txray::MethodIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/txray/method_index.rb

Constant Summary collapse

MAX_RESOLUTION_DEPTH =
4

Instance Method Summary collapse

Constructor Details

#initializeMethodIndex

Returns a new instance of MethodIndex.



13
14
15
16
17
# File 'lib/txray/method_index.rb', line 13

def initialize
  @instance = {}
  @singleton = {}
  @includes = {}
end

Instance Method Details

#index(source) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/txray/method_index.rb', line 19

def index(source)
  Namespaces.each(source.root) do |namespace, node|
    case node
    when Prism::DefNode then add_method(namespace, node, source)
    when Prism::CallNode
      add_include(namespace, node)
      add_defined_method(namespace, node, source)
    end
  end
end

#lookup(namespace, name, singleton: false, depth: 0) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/txray/method_index.rb', line 35

def lookup(namespace, name, singleton: false, depth: 0)
  return nil if namespace.nil? || namespace.empty? || depth > MAX_RESOLUTION_DEPTH

  table = singleton ? @singleton : @instance
  direct = table.dig(namespace, name)
  return direct if direct

  from_modules(namespace, name, singleton, depth) || from_lexical_parent(namespace, name, singleton, depth)
end

#unique(name) ⇒ Object



30
31
32
33
# File 'lib/txray/method_index.rb', line 30

def unique(name)
  matches = @instance.each_value.filter_map { |methods| methods[name] }
  matches.first if matches.size == 1
end