Sha256: d316ae640cda99aee1ecd9f7d902f83269cb0077487411f9158a43704aa42785

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

class Rtml::HighLevel::VariableManager
  attr_accessor :screen

  def initialize()
    @vars = HashWithIndifferentAccess.new
  end

  def [](variable_name)
    variable_name = variable_name.to_s
    vardec = screen.document.variable_definitions.select { |i| i['name'] == variable_name }
    vardec = vardec.collect { |i| i['name'] }.first
    raise Rtml::Errors::MissingVariableError, "Could not find a TML variable named #{variable_name}. "+
            "Did you forget to declare it?" unless vardec
    @vars[variable_name] ||= Rtml::HighLevel::ManagedVariable.new(variable_name)
  end

  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

  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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rtml-2.0.3 lib/rtml/high_level/variable_manager.rb
rtml-2.0.2 lib/rtml/high_level/variable_manager.rb
rtml-2.0.1 lib/rtml/high_level/variable_manager.rb
rtml-2.0.0.alpha.1 lib/rtml/high_level/variable_manager.rb