Class: Remocon::ConditionFileInterpreter

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

Constant Summary collapse

SUPPORTED_ATTRIBUTED =
%i(name expression tagColor).freeze

Instance Method Summary collapse

Constructor Details

#initialize(filepath) ⇒ ConditionFileInterpreter

Returns a new instance of ConditionFileInterpreter.



7
8
9
10
# File 'lib/remocon/interpreter/condition_file_interpreter.rb', line 7

def initialize(filepath)
  # conditions should be Array
  @yaml = YAML.safe_load(File.open(filepath).read).map(&:with_indifferent_access)
end

Instance Method Details

#read(opts = {}) ⇒ Object



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

def read(opts = {})
  errors = []
  json_array = @yaml.dup

  keys = []

  @yaml.each do |hash|
    begin
      raise Remocon::EmptyNameError, "name must not be empty" unless hash[:name]
      raise Remocon::EmptyExpressionError, "expression must not be empty" unless hash[:expression]
      raise Remocon::DuplicateKeyError, "#{hash[:name]} is duplicated" if keys.include?(hash[:name])

      keys.push(hash[:name])
    rescue Remocon::ValidationError => e
      raise e unless opts[:validate_only]
      errors.push(e)
    end
  end

  [json_array, errors]
end