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, opts = {}) ⇒ YAML

Returns a new instance of YAML.

Parameters:

  • file (String)

    the path to the file

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :namespace (String)

    optionally set this as the root

  • :required (Boolean)

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

Raises:

  • (ArgumentError)

    if the namespace isn’t found in the file

  • (Errno:ENOENT)

    if the file isn’t found



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

def initialize file, opts = {}
  @settings = {}
  required = opts.delete(:required) { true }

  @settings = ::YAML.load_file(file)

  namespace = opts.delete(:namespace)
  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
  $stderr.puts "WARNING: Configuration file #{file} not found, ensure it's present"
  raise e if required
end

Instance Method Details

#lookup_path(setting_path, *_) ⇒ Object



33
34
35
# File 'lib/configurate/provider/yaml.rb', line 33

def lookup_path setting_path, *_
  Provider.lookup_in_hash(setting_path, @settings)
end