Class: Konfig::YamlProvider

Inherits:
ConfigProvider show all
Defined in:
lib/konfig/yaml_provider.rb

Instance Attribute Summary collapse

Attributes inherited from ConfigProvider

#mode, #workdir

Instance Method Summary collapse

Constructor Details

#initialize(workdir:, filenames: Konfig.configuration.default_config_files) ⇒ YamlProvider

Returns a new instance of YamlProvider.

Raises:



11
12
13
14
15
# File 'lib/konfig/yaml_provider.rb', line 11

def initialize(workdir:, filenames: Konfig.configuration.default_config_files)
  super(mode: :yaml, workdir: workdir)
  @files = filenames.map { |f| File.join(workdir, f) }
  raise FileNotFound, "none of the configuration files (#{@files}) found" if @files.all? { |f| !File.exist?(f) }
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



8
9
10
# File 'lib/konfig/yaml_provider.rb', line 8

def files
  @files
end

#raw_settingsObject (readonly)

Returns the value of attribute raw_settings.



9
10
11
# File 'lib/konfig/yaml_provider.rb', line 9

def raw_settings
  @raw_settings
end

Instance Method Details

#load(parse = true) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/konfig/yaml_provider.rb', line 17

def load(parse = true)
  content = {}
  @files.each do |f|
    # since we've looked for file existence before, we can skip any that's missing
    next if !File.exist?(f)
    Konfig.configuration.logger.info "Loading #{f}"
    file_content = IO.read(f)
    raise FileError, "file #{f} is empty" if file_content.blank?

    yaml_content = parse ? ERB.new(file_content).result : file_content
    raise FileError, "#{f} seems empty" unless yaml_content
    parsed = YAML.load(yaml_content)
    raise FileError, "#{f} cannot be loaded as YAML" unless parsed
    current_content = parsed.deep_symbolize_keys if f and File.exist?(f)
    current_content = Konfig::EnvReplacer.replace(current_content)

    content = content.deep_merge(current_content)
  end

  os = Konfig::Option.new
  @raw_settings = os.load(content)
  Object.send(:remove_const, Konfig.configuration.namespace) if Object.const_defined?(Konfig.configuration.namespace)
  Object.const_set(Konfig.configuration.namespace, @raw_settings)
rescue Psych::SyntaxError => exc
  raise "YAML syntax error occurred while parsing #{f}. " \
        "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
        "Error: #{exc.message}"
end