Class: Unobtainium::Config

Inherits:
PathedHash show all
Defined in:
lib/unobtainium/config.rb

Overview

The Config class extends PathedHash by two main pieces of functionality: a) it loads configuration files and turns them into pathed hashes, and b) it treats environment variables as overriding anything contained in

the configuration file.

For configuration file loading, a named configuration file will be laoaded if present. A file with the same name but ‘-local’ appended before the extension will be loaded as well, overriding any values in the original configuration file.

For environment variable support, any environment variable named like a path into the configuration hash, but with separators transformed to underscore and all letters capitalized will override values from the configuration files under that path, i.e. FOO_BAR will override ‘foo.bar’.

Environment variables can contain JSON only; if the value can be parsed as JSON, it becomes a Hash in the configuration tree. If it cannot be parsed as JSON, it remains a string.

Note: if your configuration file’s top-level structure is an array, it will be returned as a hash with a ‘config’ key that maps to your file’s contents.

Defined Under Namespace

Classes: JSONParser, YAMLParser

Constant Summary collapse

FILE_TO_PARSER =

Mapping of file name extensions to parser types.

{
  '.yml'  => YAMLParser,
  '.yaml' => YAMLParser,
  '.json' => JSONParser,
}.freeze
ARRAY_KEY =

If the config file contains an Array, this is what they key of the returned Hash will be.

'config'.freeze

Constants inherited from PathedHash

PathedHash::READ_METHODS, PathedHash::WRITE_METHODS

Instance Attribute Summary

Attributes inherited from PathedHash

#separator

Class Method Summary collapse

Methods inherited from PathedHash

#initialize, #method_missing, #respond_to?, #split_pattern, #to_s

Constructor Details

This class inherits a constructor from Unobtainium::PathedHash

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Unobtainium::PathedHash

Class Method Details

.load_config(path) ⇒ Object

Loads a configuration file with the given file name. The format is detected based on one of the extensions in FILE_TO_PARSER.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/unobtainium/config.rb', line 96

def load_config(path)
  # Load base and local configuration files
  base, config = load_base_config(path)
  _, local_config = load_local_config(base)

  # We can't sensibly merge arrays and hashes, so bail if the two classes
  # don't match.
  if config.class != local_config.class
    raise ArgumentError, "Config file and local override file do not have "\
          "the same top-level structure (hash or array), and therefore "\
          "cannot be merged!"
  end

  # Merge
  if config.is_a? Array
    config = { ARRAY_KEY => config.push(*local_config) }
  elsif config.is_a? Hash
    # rubocop:disable Style/CaseEquality
    merger = proc do |_, v1, v2|
      Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2
    end
    # rubocop:enable Style/CaseEquality
    config.merge!(local_config, &merger)
  else
    raise "Unexpected top-level structure in configuration file: "\
          "#{config.class}"
  end

  return Config.new(config)
end