class Ant::Configuration::ConfigurationManager

This class provides a module for loading configurations from 4 sources:

Using yaml defaults: TODO: Add docs Using yaml files TODO: Add docs Using env vars: TODO: Add docs Using arg vars: TODO: Add docs

Attributes

accept_default_keys[R]

Use this configuration when you don't want your configs to be validated.

append_arrays[R]

With this enabled all array will be concatenated instead of replaced.

default_files[R]
String(Array)

A path to default yaml configs.

default_placeholder[R]
String

The value provided by default. It should mean this \

value is missing on configurations.

env_prefix[R]

The prefix used to find env strings and args.

Public Class Methods

new(default_files:, default_placeholder: nil, append_arrays: false, env_prefix: nil, accept_default_keys: false) click to toggle source
# File lib/ant/configs/configuration_manager.rb, line 39
def initialize(default_files:,
               default_placeholder: nil,
               append_arrays: false,
               env_prefix: nil,
               accept_default_keys: false)
  @default_files = array_wrap(default_files)
  @default_placeholder = default_placeholder || 'REPLACE_ME'
  @append_arrays = append_arrays
  @env_prefix = env_prefix || 'CONFIG'
  @accept_default_keys = accept_default_keys
  @configs = {}
  @env_vars = env_vars
  @config_files = env_files
end

Private Class Methods

auto_load!() click to toggle source
# File lib/ant/configs/configuration_manager.rb, line 151
def self.auto_load!
  auto_configs = new(default_files: './config/autoconfig.yaml')
  auto_configs.load_configs!
  auto_configs = auto_configs['autoconfig']
  configs = new(
    default_files: (auto_configs['default_files'] || []) + (auto_configs['files'] || []) + ['./config/autoconfig.yaml'],
    default_placeholder: auto_configs['default_placeholder'],
    accept_default_keys: auto_configs['accept_default_keys'],
    env_prefix: auto_configs['env_prefix']
  )
  configs.load_configs!
  configs
end

Public Instance Methods

[](key) click to toggle source

provide a method for accessing configs

# File lib/ant/configs/configuration_manager.rb, line 91
def [](key)
  @configs[key]
end
load_configs!() click to toggle source

Loads the configurations from all the possible sources. It will raise an exception when it is required to validate that no default placeholder is present on the configs.

# File lib/ant/configs/configuration_manager.rb, line 57
def load_configs!
  load_configs
  missing_keys = missing_configs
  return if missing_keys.empty? || @accept_default_keys

  raise MissingConfigs, missing_keys
end
pretty_load_configs!(terminate = true) click to toggle source

Use this when you require the application to do not start when something is missing and the error message should be displayed in stdout. This is helpful when you are launching your app and you need to trace any misconfiguration problem. :nocov: #

# File lib/ant/configs/configuration_manager.rb, line 70
def pretty_load_configs!(terminate = true)
  load_configs!
rescue MissingConfigs => ex
  puts 'You are missing some configs!'
  puts 'Add them to a file and export the config env var:'
  puts "$ export #{@env_prefix}_FILES='#{Dir.pwd}'/config/config.yaml"
  puts 'Maybe you just need to add them to your existing files'
  puts 'Missing configs:'
  ex.keys.each { |k| puts "- \"#{k}\"" }
  exit(1) if terminate
end
to_h() click to toggle source

returns the object as a hash :nocov: #

# File lib/ant/configs/configuration_manager.rb, line 85
def to_h
  @configs
end

Private Instance Methods

arg_vars() click to toggle source

Extract vars from arg

# File lib/ant/configs/configuration_manager.rb, line 121
def arg_vars
  Loaders::Arg.new(@env_prefix, self).load!
end
env_files() click to toggle source

Path to config files

# File lib/ant/configs/configuration_manager.rb, line 111
def env_files
  @env_vars['files'] && array_wrap(@env_vars['files'])
end
env_vars() click to toggle source

Extract vars from env

# File lib/ant/configs/configuration_manager.rb, line 116
def env_vars
  Loaders::Env.new(@env_prefix, self).load!
end
load_config_files() click to toggle source

Helper method for loading config files

# File lib/ant/configs/configuration_manager.rb, line 139
def load_config_files
  @config_files && load_files(@config_files)
end
load_configs() click to toggle source

Helper method that loads configurations

# File lib/ant/configs/configuration_manager.rb, line 126
def load_configs
  load_default_files
  load_config_files
  @configs = recursive_merge(@configs, @env_vars)
  @configs = recursive_merge(@configs, arg_vars)
end
load_default_files() click to toggle source

Helper method for loading default files

# File lib/ant/configs/configuration_manager.rb, line 134
def load_default_files
  load_files(@default_files)
end
load_files(files) click to toggle source

Helper method for loading files into configurations

# File lib/ant/configs/configuration_manager.rb, line 144
def load_files(files)
  files.each do |file|
    config = Loaders::YAML.new(file, self).load!
    @configs = recursive_merge(@configs, config)
  end
end
missing_configs(hash = @configs, path = []) click to toggle source

Looks for keys having the default placeholder, which are meant to be missing configurations

# File lib/ant/configs/configuration_manager.rb, line 99
def missing_configs(hash = @configs, path = [])
  case hash
  when Hash
    hash.map { |k, v| missing_configs(v, path + [k]) }.flatten
  when Array
    hash.map.with_index { |e, i| missing_configs(e, path + [i]) }.flatten
  else
    hash == @default_placeholder ? [path.join('.')] : []
  end
end