Class: CodeExplorer::ConstBinding
- Inherits:
-
Object
- Object
- CodeExplorer::ConstBinding
- Defined in:
- lib/code_explorer/const_binding.rb
Overview
tracks what constants are resolvable
Instance Method Summary collapse
-
#close_namespace ⇒ ConstBinding
The parent scope.
- #declare_const(fqname) ⇒ Object
-
#initialize(fqname, parent = nil) ⇒ ConstBinding
constructor
A new instance of ConstBinding.
-
#open_namespace(fqname) ⇒ ConstBinding
The new scope.
- #resolve_declared_const(name) ⇒ Object
- #resolve_used_const(name) ⇒ Object
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_namespace ⇒ ConstBinding
Returns the parent scope.
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.
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 |