Class: CodeExplorer::ConstBinding

Inherits:
Object
  • Object
show all
Defined in:
lib/code_explorer/const_binding.rb

Overview

tracks what constants are resolvable

Instance Method Summary collapse

Constructor Details

#initialize(fqname, parent = nil) ⇒ ConstBinding

Returns a new instance of ConstBinding.



4
5
6
7
8
# File 'lib/code_explorer/const_binding.rb', line 4

def initialize(fqname, parent = nil)
  @fqname = fqname
  @parent = parent
  @known = {}
end

Instance Method Details

#close_namespaceConstBinding

Returns the parent scope.

Returns:



23
24
25
# File 'lib/code_explorer/const_binding.rb', line 23

def close_namespace
  @parent
end

#declare_const(fqname) ⇒ Object



27
28
29
30
31
32
# File 'lib/code_explorer/const_binding.rb', line 27

def declare_const(fqname)
  if @known[fqname]
    #      puts "warning: #{fqname} already declared"
  end
  @known[fqname] = :const
end

#open_namespace(fqname) ⇒ ConstBinding

Returns the new scope.

Returns:



11
12
13
14
15
16
17
18
19
20
# File 'lib/code_explorer/const_binding.rb', line 11

def open_namespace(fqname)
  ns = @known[fqname]
  if ns.is_a? ConstBinding
    # puts "(reopening #{fqname})"
  else
    ns = self.class.new(fqname, self)
    @known[fqname] = ns
  end
  ns
end

#resolve_declared_const(name) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/code_explorer/const_binding.rb', line 34

def resolve_declared_const(name)
  if @fqname.empty?
    name
  else
    "#{@fqname}::#{name}"
  end
end

#resolve_used_const(name) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/code_explorer/const_binding.rb', line 42

def resolve_used_const(name)
  #    puts "resolving #{name} in #{@fqname}, known #{@known.inspect}"
  candidate = resolve_declared_const(name)
  if @known.include?(candidate)
    candidate
  elsif @parent
    @parent.resolve_used_const(name)
  else
    name
  end
end