Class: RSpecPuppetUtils::HieraData::YamlValidator

Inherits:
Validator
  • Object
show all
Defined in:
lib/hieradata/yaml_validator.rb

Instance Attribute Summary

Attributes inherited from Validator

#data

Instance Method Summary collapse

Methods inherited from Validator

#validate

Constructor Details

#initialize(directory, extensions = ['yaml', 'yml']) ⇒ YamlValidator

Returns a new instance of YamlValidator.

Raises:

  • (ArgumentError)


9
10
11
12
13
# File 'lib/hieradata/yaml_validator.rb', line 9

def initialize(directory, extensions = ['yaml', 'yml'])
  raise ArgumentError, 'extensions should be an Array' unless extensions.is_a? Array
  @directory = directory
  @extensions = extensions.map {|ext| ext =~ /\..*/ ? ext : ".#{ext}" }
end

Instance Method Details

#load(ignore_empty = false) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hieradata/yaml_validator.rb', line 15

def load(ignore_empty = false)

  files = Dir.glob(File.join(@directory, '**', '*')).reject { |path|
    File.directory?(path) || !@extensions.include?(File.extname path )
  }

  @data = {}
  files.each { |file|

    # Assume all file names are unique i.e. thing.yaml and thing.yml don't both exist
    file_name = File.basename(file).split('.').first

    begin
      yaml = File.open(file) { |yf|
        YAML::load( yf )
      }
    rescue ArgumentError => e
      raise StandardError, "Yaml Syntax error in file #{file}: #{e.message}"
    end
    raise StandardError, "Yaml file is empty: #{file}" unless yaml || ignore_empty

    @data[file_name.to_sym] = yaml if yaml
  }

end