Class: Mudguard::Domain::Consts

Inherits:
Object
  • Object
show all
Defined in:
lib/mudguard/domain/consts.rb

Overview

Knows all constants of the project

Instance Method Summary collapse

Constructor Details

#initialize(sources:) ⇒ Consts

Returns a new instance of Consts.



9
10
11
# File 'lib/mudguard/domain/consts.rb', line 9

def initialize(sources:)
  @consts = sources.flat_map(&:find_consts).each_with_object({}, &method(:add_const))
end

Instance Method Details

#resolve(module_name, const_name) ⇒ Object

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mudguard/domain/consts.rb', line 13

def resolve(module_name, const_name)
  raise Error, "const_name is undefined" if const_name.empty?

  path = split_hierarchy(module_name)
  if module_name.empty?
    # not in a module therefor const can only be defined in the root module (::)
    qualified_path(const_name)
  else
    # analyse module hierarchy to find fully qualified const name
    # resolve_in_modules(const_name, path)
    const_path = const_name.split(SEPARATOR).drop(1)
    find_const_deeper("", path, @consts, const_path) || qualified_path(const_name)
  end
end