Class: Configurate::Provider::YAML

Inherits:
Base
  • Object
show all
Defined in:
lib/configurate/provider/yaml.rb

Overview

This provider tries to open a YAML file and does nested lookups in it.

Instance Method Summary collapse

Methods inherited from Base

#lookup

Constructor Details

#initialize(file, namespace: nil, required: true, raise_on_missing: false) ⇒ YAML

Returns a new instance of YAML.

Parameters:

  • file (String)

    the path to the file

  • namespace (String) (defaults to: nil)

    optionally set this as the root

  • required (Boolean) (defaults to: true)

    whether or not to raise an error if the file or the namespace, if given, is not found. Defaults to true.

  • raise_on_missing (Boolean) (defaults to: false)

    whether to raise MissingSetting if a setting can’t be provided. Defaults to false.

Raises:

  • (ArgumentError)

    if the namespace isn’t found in the file

  • (Errno:ENOENT)

    if the file isn’t found



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/configurate/provider/yaml.rb', line 16

def initialize file, namespace: nil, required: true, raise_on_missing: false
  @raise_on_missing = raise_on_missing
  @settings = {}

  @settings = ::YAML.load_file(file)

  unless namespace.nil?
    @settings = Provider.lookup_in_hash(SettingPath.new(namespace), @settings) do
      raise ArgumentError, "Namespace #{namespace} not found in #{file}" if required
      $stderr.puts "WARNING: Namespace #{namespace} not found in #{file}"
      nil
    end
  end
rescue Errno::ENOENT => e
  warn "WARNING: Configuration file #{file} not found, ensure it's present"
  raise e if required
end

Instance Method Details

#lookup_path(setting_path, *_) ⇒ Object



34
35
36
37
38
39
# File 'lib/configurate/provider/yaml.rb', line 34

def lookup_path setting_path, *_
  Provider.lookup_in_hash(setting_path, @settings) {
    raise MissingSetting.new "#{setting_path} is not a valid setting." if @raise_on_missing
    nil
  }
end