Class: Rtml::Test::VariableScope::Variable
- Inherits:
-
Object
- Object
- Rtml::Test::VariableScope::Variable
- Defined in:
- lib/rtml/test/variable_scope.rb
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#perms ⇒ Object
readonly
Returns the value of attribute perms.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #cast_value(value) ⇒ Object
-
#initialize(name, options = {}) ⇒ Variable
constructor
A new instance of Variable.
-
#perform_operation(op) ⇒ Object
TODO: implement :format option.
Constructor Details
#initialize(name, options = {}) ⇒ Variable
Returns a new instance of Variable.
7 8 9 10 11 12 13 14 15 |
# File 'lib/rtml/test/variable_scope.rb', line 7 def initialize(name, = {}) .stringify_keys!.reverse_merge() @name = name.to_s %w(type perms format).each do |key| instance_variable_set("@#{key}", [key].to_s) if .key?(key) instance_variable_set("@#{key}", [key]) unless instance_variable_get("@#{key}") end self.value = .delete('value') if .key?('value') end |
Instance Attribute Details
#format ⇒ Object (readonly)
Returns the value of attribute format.
3 4 5 |
# File 'lib/rtml/test/variable_scope.rb', line 3 def format @format end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/rtml/test/variable_scope.rb', line 5 def name @name end |
#perms ⇒ Object (readonly)
Returns the value of attribute perms.
3 4 5 |
# File 'lib/rtml/test/variable_scope.rb', line 3 def perms @perms end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
3 4 5 |
# File 'lib/rtml/test/variable_scope.rb', line 3 def type @type end |
#value ⇒ Object
Returns the value of attribute value.
4 5 6 |
# File 'lib/rtml/test/variable_scope.rb', line 4 def value @value end |
Instance Method Details
#cast_value(value) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/rtml/test/variable_scope.rb', line 41 def cast_value(value) case type when 'integer' then value.to_i when 'string' then value.to_s when 'opaque' then value.to_s when 'date' then value.kind_of?(DateTime) ? value : DateTime.parse(value.to_s) else raise "Unknown type: #{type.inspect}" end end |
#perform_operation(op) ⇒ Object
TODO: implement :format option
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rtml/test/variable_scope.rb', line 18 def perform_operation(op) if op.keys?(:lo, :op) && op[:op] == 'number' # list length raise Rtml::Errors::VariableError, "No :ro expected" if op.key?(:ro) self.value = (op[:lo].to_s.split(/\;/).length) elsif op.key?(:lo) && !op.keys?(:op, :ro) # assignment self.value = op[:lo] elsif op.keys?(:lo, :op, :ro) # operation self.value = case op[:op] when 'plus' then cast_value(op[:lo]) + cast_value(op[:ro]) when 'minus' then cast_value(op[:lo]) - cast_value(op[:ro]) when 'format' then raise Rtml::Errors::VariableError, "setvar[op=format] not yet implemented" when 'item' then op[:lo].to_s.split(/\;/)[op[:ro].to_i] else raise Rtml::Errors::VariableError, "Unknown operation: #{op[:op].inspect}" end else raise Rtml::Errors::VariableError, "Expected :lo, :op, and :ro; or just :lo for assignment" end end |