Class: Rtml::Test::VariableScope::Variable

Inherits:
Object
  • Object
show all
Defined in:
lib/rtml/test/variable_scope.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  options.stringify_keys!.reverse_merge(default_options)
  @name = name.to_s
  %w(type perms format).each do |key|
    instance_variable_set("@#{key}", options[key].to_s) if options.key?(key)
    instance_variable_set("@#{key}", default_options[key]) unless instance_variable_get("@#{key}")
  end
  self.value = options.delete('value') if options.key?('value')
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



3
4
5
# File 'lib/rtml/test/variable_scope.rb', line 3

def format
  @format
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/rtml/test/variable_scope.rb', line 5

def name
  @name
end

#permsObject (readonly)

Returns the value of attribute perms.



3
4
5
# File 'lib/rtml/test/variable_scope.rb', line 3

def perms
  @perms
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/rtml/test/variable_scope.rb', line 3

def type
  @type
end

#valueObject

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