Class: Opto::Resolvers::Evaluate

Inherits:
Opto::Resolver show all
Defined in:
lib/opto/resolvers/evaluate.rb

Overview

Geneerate a new random number. Requires :min and :max in hint to define range.

Instance Attribute Summary

Attributes inherited from Opto::Resolver

#hint, #option

Instance Method Summary collapse

Methods inherited from Opto::Resolver

for, inherited, #initialize, origin, #origin, resolvers

Constructor Details

This class inherits a constructor from Opto::Resolver

Instance Method Details

#resolveObject

Raises:

  • (TypeError)

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/opto/resolvers/evaluate.rb', line 11

def resolve
  raise TypeError, "String required" unless hint.kind_of?(String)
  interpolated_hint = hint.gsub(/(?<!\$)\$(?!\$)\{?[\w\.]+\}?/) do |v|
    var = v.tr('${}', '')
    if option.group.nil? || option.group.option(var).nil?
      raise RuntimeError, "Variable #{var} not declared"
    end
    if option.value_of(var).nil?
      raise RuntimeError, "No value for #{var}, note that the order is meaningful"
    end
    option.value_of(var)
  end.gsub(/\s+/, "")

  if interpolated_hint =~ /\A[\(\)\-\+\/\*0-9\.]+\z/
    eval(interpolated_hint)
  else
    raise TypeError, "Syntax error: '#{interpolated_hint}' does not look like a number or a calculation"
  end
end