Sha256: 5cbc8b308523536c164331686c8cbdd58966ff0d8308091150a220c1b0dc82b9

Contents?: true

Size: 1.82 KB

Versions: 11

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

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

    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

    private

    def read_value(body)
      body[:file] ? File.open(body[:file]).read : body[:value]
    end

    def parse_value_body(key, value_body)
      case value_body
      when Hash
        value = read_value(value_body)
        options = { key: key }.merge(value_body[:options] || {})
        normalizer = TypeNormalizerFactory.get(value_body[:normalizer]).new(value, options)
        normalizer.process
        normalizer.content
      else # includes Array
        # use raw value
        value_body
      end
    end

    def parse_condition_body(condition_names, key, condition_body)
      condition_body.each_with_object({}) do |(cond_key, body), hash|
        raise Remocon::NotFoundConditionKey, "The condition '#{cond_key}' is not defined" unless condition_names.include?(cond_key.to_s)

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
remocon-0.5.1 lib/remocon/interpreter/parameter_file_interpreter.rb
remocon-0.5.0 lib/remocon/interpreter/parameter_file_interpreter.rb
remocon-0.4.4 lib/remocon/interpreter/parameter_file_interpreter.rb
remocon-0.4.3 lib/remocon/interpreter/parameter_file_interpreter.rb
remocon-0.4.2 lib/remocon/interpreter/parameter_file_interpreter.rb
remocon-0.4.1 lib/remocon/interpreter/parameter_file_interpreter.rb
remocon-0.4.0 lib/remocon/interpreter/parameter_file_interpreter.rb
remocon-0.4.0.pre.1 lib/remocon/interpreter/parameter_file_interpreter.rb
remocon-0.3.1 lib/remocon/interpreter/parameter_file_interpreter.rb
remocon-0.3.0 lib/remocon/interpreter/parameter_file_interpreter.rb
remocon-0.2.0 lib/remocon/interpreter/parameter_file_interpreter.rb