Class: Ufo::Yaml::Validator

Inherits:
Object
  • Object
show all
Includes:
Utils::Logging
Defined in:
lib/ufo/yaml/validator.rb

Instance Method Summary collapse

Methods included from Utils::Logging

#logger

Constructor Details

#initialize(path) ⇒ Validator

Returns a new instance of Validator.



5
6
7
# File 'lib/ufo/yaml/validator.rb', line 5

def initialize(path)
  @path = path
end

Instance Method Details

#handle_yaml_syntax_error(e, path) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ufo/yaml/validator.rb', line 22

def handle_yaml_syntax_error(e, path)
  logger.error "ERROR: #{e.message}".color(:red)
  logger.error "Invalid yaml. Output written for debugging: #{path}".color(:red)

  # Grab line info.  Example error:
  #   ERROR: (<unknown>): could not find expected ':' while scanning a simple key at line 2 column 1
  md = e.message.match(/at line (\d+) column (\d+)/)
  line = md[1].to_i

  DslEvaluator.print_code(path, line)
  exit 1
end

#validate!Object



9
10
11
# File 'lib/ufo/yaml/validator.rb', line 9

def validate!
  validate_yaml(@path)
end

#validate_yaml(path) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/ufo/yaml/validator.rb', line 13

def validate_yaml(path)
  text = IO.read(path)
  begin
    YAML.load(text)
  rescue Psych::SyntaxError => e
    handle_yaml_syntax_error(e, path)
  end
end