Class: RSpecPuppetUtils::HieraData::YamlValidator
- Defined in:
- lib/hieradata/yaml_validator.rb
Instance Attribute Summary
Attributes inherited from Validator
Instance Method Summary collapse
-
#initialize(directory, extensions = ['yaml', 'yml']) ⇒ YamlValidator
constructor
A new instance of YamlValidator.
- #load(ignore_empty = false) ⇒ Object
Methods inherited from Validator
Constructor Details
#initialize(directory, extensions = ['yaml', 'yml']) ⇒ YamlValidator
Returns a new instance of YamlValidator.
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 |