Class: Remocon::ParameterFileInterpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/remocon/interpreter/parameter_file_interpreter.rb

Instance Method Summary collapse

Constructor Details

#initialize(filepath) ⇒ ParameterFileInterpreter

Returns a new instance of ParameterFileInterpreter.



5
6
7
# File 'lib/remocon/interpreter/parameter_file_interpreter.rb', line 5

def initialize(filepath)
  @yaml = YAML.safe_load(File.open(filepath).read).with_indifferent_access
end

Instance Method Details

#read(condition_names, opts = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/remocon/interpreter/parameter_file_interpreter.rb', line 9

def read(condition_names, opts = {})
  errors = []
  json_hash = @yaml.each_with_object({}) do |(key, body), hash|
    begin
      raise Remocon::DuplicateKeyError, "#{key} is duplicated" if hash[key]

      hash[key] = {
        defaultValue: {
          value: parse_value_body(key, body)
        }
      }

      hash[key][:conditionalValues] = parse_condition_body(condition_names, key, body[:conditions]) if body[:conditions]
      hash[key][:description] = body[:description] if body[:description]
    rescue Remocon::ValidationError => e
      raise e unless opts[:validate_only]
      errors.push(e)
    end
  end

  [json_hash.with_indifferent_access, errors]
end