Class: TPPlus::Namespace

Inherits:
Object
  • Object
show all
Defined in:
lib/tp_plus/namespace.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, block) ⇒ Namespace

Returns a new instance of Namespace.



3
4
5
6
7
8
9
10
11
# File 'lib/tp_plus/namespace.rb', line 3

def initialize(name, block)
  @name       = name
  @block      = block
  @namespaces = {}
  @variables  = {}
  @constants  = {}

  define!
end

Instance Method Details

#add_constant(identifier, node) ⇒ Object



24
25
26
27
28
# File 'lib/tp_plus/namespace.rb', line 24

def add_constant(identifier, node)
  raise "Constant (#{identifier}) already defined within namespace #{@name}" unless @constants[identifier.to_sym].nil?

  @constants[identifier.to_sym] = node
end

#add_namespace(identifier, block) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/tp_plus/namespace.rb', line 30

def add_namespace(identifier, block)
  if @namespaces[identifier.to_sym].nil?
    @namespaces[identifier.to_sym] = TPPlus::Namespace.new("#{@name} #{identifier}", block)
  else
    @namespaces[identifier.to_sym].reopen!(block)
  end
end

#add_var(identifier, node) ⇒ Object



38
39
40
41
42
43
# File 'lib/tp_plus/namespace.rb', line 38

def add_var(identifier, node)
  raise "Variable (#{identifier}) already defined within namespace #{@name}" unless @variables[identifier.to_sym].nil?

  @variables[identifier.to_sym] = node
  node.comment = "#{@name} #{identifier}"
end

#define!Object



13
14
15
16
17
# File 'lib/tp_plus/namespace.rb', line 13

def define!
  @block.flatten.select {|n| [TPPlus::Nodes::DefinitionNode, TPPlus::Nodes::NamespaceNode].include? n.class }.each do |node|
    node.eval(self)
  end
end

#get_constant(identifier) ⇒ Object



45
46
47
48
49
# File 'lib/tp_plus/namespace.rb', line 45

def get_constant(identifier)
  raise "Constant (#{identifier}) not defined within namespace #{@name}" if @constants[identifier.to_sym].nil?

  @constants[identifier.to_sym]
end

#get_namespace(identifier) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/tp_plus/namespace.rb', line 58

def get_namespace(identifier)
  if ns = @namespaces[identifier.to_sym]
    return ns
  end

  false
end

#get_var(identifier) ⇒ Object



51
52
53
54
55
56
# File 'lib/tp_plus/namespace.rb', line 51

def get_var(identifier)
  return get_constant(identifier) if identifier.upcase == identifier
  raise "Variable (#{identifier}) not defined within namespace #{@name}" if @variables[identifier.to_sym].nil?

  @variables[identifier.to_sym]
end

#reopen!(block) ⇒ Object



19
20
21
22
# File 'lib/tp_plus/namespace.rb', line 19

def reopen!(block)
  @block = block
  define!
end