Class: Rtml::Test::VariableScope
- Inherits:
-
Object
- Object
- Rtml::Test::VariableScope
- Defined in:
- lib/rtml/test/variable_scope.rb
Defined Under Namespace
Classes: Variable
Instance Method Summary collapse
- #declare_variable(name, options = {}) ⇒ Object
- #exist?(name) ⇒ Boolean
- #find_variable(name) ⇒ Object
-
#initialize ⇒ VariableScope
constructor
A new instance of VariableScope.
- #inspect ⇒ Object
- #literal_value(value_or_variable_name) ⇒ Object
- #perform_operation_on(variable_name, operation) ⇒ Object
-
#snapshot ⇒ Object
Returns a “snapshot” of this variable scope’s current state.
-
#true_condition?(condition) ⇒ Boolean
Accepts a hash according to the TML rules (for example, keys should be :lo, :op, :ro).
- #update(name, value) ⇒ Object (also: #[]=, #assign)
-
#update_with(hash) ⇒ Object
Accepts a hash, and updates each listed variable with the values in the hash.
- #value(name) ⇒ Object (also: #[])
- #variable_names ⇒ Object
Constructor Details
#initialize ⇒ VariableScope
Returns a new instance of VariableScope.
57 58 59 |
# File 'lib/rtml/test/variable_scope.rb', line 57 def initialize @variables = {} end |
Instance Method Details
#declare_variable(name, options = {}) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/rtml/test/variable_scope.rb', line 127 def declare_variable(name, = {}) name = name.to_s unless name.kind_of?(String) if @variables.key?(name) raise Rtml::Errors::VariableError, "Already declared variable #{name.inspect} (it's a #{@variables[name].type})" end @variables[name] = Variable.new(name, ) end |
#exist?(name) ⇒ Boolean
117 118 119 120 |
# File 'lib/rtml/test/variable_scope.rb', line 117 def exist?(name) name = name.to_s unless name.kind_of?(String) @variables[name] end |
#find_variable(name) ⇒ Object
122 123 124 125 |
# File 'lib/rtml/test/variable_scope.rb', line 122 def find_variable(name) name = name.to_s unless name.kind_of?(String) @variables[name] || raise(Rtml::Errors::VariableError, "Undeclared variable #{name.inspect}") end |
#inspect ⇒ Object
70 71 72 73 74 |
# File 'lib/rtml/test/variable_scope.rb', line 70 def inspect @variables.collect do |name, variable| "#{name}={value:#{variable.value.inspect} type:#{variable.type.inspect} perms:#{variable.perms.inspect} format:#{variable.format.inspect}}" end.join("; ") end |
#literal_value(value_or_variable_name) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/rtml/test/variable_scope.rb', line 103 def literal_value(value_or_variable_name) if value_or_variable_name.kind_of?(String) && value_or_variable_name =~ /^tmlvar\:/ value(value_or_variable_name[7..-1]) else value_or_variable_name end end |
#perform_operation_on(variable_name, operation) ⇒ Object
111 112 113 114 115 |
# File 'lib/rtml/test/variable_scope.rb', line 111 def perform_operation_on(variable_name, operation) operation[:lo] = literal_value(operation[:lo]) if operation.key?(:lo) operation[:ro] = literal_value(operation[:ro]) if operation.key?(:ro) find_variable(variable_name).perform_operation(operation) end |
#snapshot ⇒ Object
Returns a “snapshot” of this variable scope’s current state. All known variables are returned, along with their types, values, etc.
63 64 65 66 67 68 |
# File 'lib/rtml/test/variable_scope.rb', line 63 def snapshot @variables.values.collect do |variable| { :type => variable.type, :perms => variable.perms, :format => variable.format, :name => variable.name, :value => variable.value } end end |
#true_condition?(condition) ⇒ Boolean
Accepts a hash according to the TML rules (for example, keys should be :lo, :op, :ro)
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rtml/test/variable_scope.rb', line 77 def true_condition?(condition) condition.reverse_merge!(:op => 'equal') raise Rtml::Errors::VariableError, "Expected :lo, :op and :ro" unless condition.keys?(:lo, :op, :ro) lo = literal_value(condition[:lo]) ro = literal_value(condition[:ro]) case condition[:op] when 'equal' then lo == ro when 'not_equal' then lo != ro when 'less' then lo < ro when 'less_or_equal' then lo <= ro when 'contains' then lo =~ /#{Regexp::escape ro}/ else raise Rtml::Errors::VariableError, "Invalid operation: #{condition[:op].inspect}" end end |
#update(name, value) ⇒ Object Also known as: []=, assign
92 93 94 |
# File 'lib/rtml/test/variable_scope.rb', line 92 def update(name, value) find_variable(name).value = literal_value(value) end |
#update_with(hash) ⇒ Object
Accepts a hash, and updates each listed variable with the values in the hash.
97 98 99 100 101 |
# File 'lib/rtml/test/variable_scope.rb', line 97 def update_with(hash) hash.each do |name, value| update(name, value) end end |
#value(name) ⇒ Object Also known as: []
139 140 141 |
# File 'lib/rtml/test/variable_scope.rb', line 139 def value(name) find_variable(name).value end |
#variable_names ⇒ Object
135 136 137 |
# File 'lib/rtml/test/variable_scope.rb', line 135 def variable_names @variables.keys end |