Class: Orbacle::ConstantsTree

Inherits:
Object
  • Object
show all
Defined in:
lib/orbacle/constants_tree.rb

Defined Under Namespace

Classes: ScopeLevel

Instance Method Summary collapse

Constructor Details

#initializeConstantsTree

Returns a new instance of ConstantsTree.



15
16
17
# File 'lib/orbacle/constants_tree.rb', line 15

def initialize
  @tree = ScopeLevel.build_empty_hash
end

Instance Method Details

#add_element(scope, name, element) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/orbacle/constants_tree.rb', line 19

def add_element(scope, name, element)
  current_children = @tree
  scope.elems.each do |scope_level|
    current_children = current_children[scope_level].children
  end
  current_children[name].elements << element
end

#find(&block) ⇒ Object



46
47
48
# File 'lib/orbacle/constants_tree.rb', line 46

def find(&block)
  find_in_children(@tree, &block)
end

#find_by_const_name(const_name) ⇒ Object



27
28
29
30
# File 'lib/orbacle/constants_tree.rb', line 27

def find_by_const_name(const_name)
  scope_children = children_of_scope(const_name.scope)
  scope_children[const_name.name].elements.first
end

#find_by_const_ref(const_ref) ⇒ Object



42
43
44
# File 'lib/orbacle/constants_tree.rb', line 42

def find_by_const_ref(const_ref)
  select_by_const_ref(const_ref).first
end

#select_by_const_ref(const_ref) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/orbacle/constants_tree.rb', line 32

def select_by_const_ref(const_ref)
  nesting = const_ref.nesting
  while !nesting.empty?
    result = select_by_scope_and_name(nesting.to_scope.increase_by_ref(const_ref).decrease, const_ref.name)
    return result if !result.empty?
    nesting = nesting.decrease_nesting
  end
  select_by_scope_and_name(Scope.empty.increase_by_ref(const_ref).decrease, const_ref.name)
end