Class: Confset::Sources::YAMLSource

Inherits:
Object
  • Object
show all
Defined in:
lib/confset/sources/yaml_source.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, evaluate_erb: Confset.evaluate_erb_in_yaml) ⇒ YAMLSource

Returns a new instance of YAMLSource.



12
13
14
15
# File 'lib/confset/sources/yaml_source.rb', line 12

def initialize(path, evaluate_erb: Confset.evaluate_erb_in_yaml)
  @path = path.to_s
  @evaluate_erb = !!evaluate_erb
end

Instance Attribute Details

#evaluate_erbObject (readonly)

Returns the value of attribute evaluate_erb.



10
11
12
# File 'lib/confset/sources/yaml_source.rb', line 10

def evaluate_erb
  @evaluate_erb
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/confset/sources/yaml_source.rb', line 9

def path
  @path
end

Instance Method Details

#loadObject

returns a config hash from the YML file



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/confset/sources/yaml_source.rb', line 18

def load
  if @path && File.exist?(@path)
    file_contents = IO.read(@path)
    file_contents = ERB.new(file_contents).result if evaluate_erb
    result = YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(file_contents) : YAML.load(file_contents)
  end

  result || {}

  rescue Psych::SyntaxError => e
    raise "YAML syntax error occurred while parsing #{@path}. " \
          "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
          "Error: #{e.message}"
end