Class: Rtml::HighLevel::VariableManager

Inherits:
Object
  • Object
show all
Defined in:
lib/rtml/high_level/variable_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVariableManager

Returns a new instance of VariableManager.



4
5
6
7
8
9
# File 'lib/rtml/high_level/variable_manager.rb', line 4

def initialize()
  @vars = HashWithIndifferentAccess.new
  Rtml::Test::BuiltinVariables::BUILTIN_VARIABLES.each do |name, options|
    @vars[name] = Rtml::HighLevel::ManagedVariable.new(name)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/rtml/high_level/variable_manager.rb', line 30

def method_missing(name, *args, &block)
  if args.empty? && !block_given?
    self.send(:[], name)
  elsif name.to_s[-1] == ?= && args.length == 1 && !block_given?
    self.send(:[]=, name.to_s[0...-1], *args)
  else
    super
  end
end

Instance Attribute Details

#screenObject

Returns the value of attribute screen.



2
3
4
# File 'lib/rtml/high_level/variable_manager.rb', line 2

def screen
  @screen
end

Instance Method Details

#[](variable_name) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/rtml/high_level/variable_manager.rb', line 11

def [](variable_name)
  variable_name = variable_name.to_s unless variable_name.kind_of?(String)
  if !@vars.key?(variable_name)
    vardec = screen.document.variable_names.include?(variable_name)
    raise Rtml::Errors::MissingVariableError, "Could not find a TML variable named #{variable_name.inspect}. "+
            "Did you forget to declare it?" unless vardec
  end
  @vars[variable_name] ||= Rtml::HighLevel::ManagedVariable.new(variable_name)
end

#[]=(target, other) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/rtml/high_level/variable_manager.rb', line 21

def []=(target, other)
  case other
    when Rtml::HighLevel::ManagedVariable
      screen.set self[target].name => "tmlvar:#{other.name}"
    else
      screen.set self[target].name => other
  end
end