Class: Ant::Configuration::ConfigurationManager
- Inherits:
-
Object
- Object
- Ant::Configuration::ConfigurationManager
- Includes:
- Utils
- Defined in:
- lib/ant/configs/configuration_manager.rb
Overview
This class provides a module for loading configurations from 4 sources:
-
YAML default files
-
YAML files
-
ENV vars
-
ARGV values
Using yaml defaults: TODO: Add docs Using yaml files TODO: Add docs Using env vars: TODO: Add docs Using arg vars: TODO: Add docs
Defined Under Namespace
Classes: MissingConfigs
Instance Attribute Summary collapse
-
#accept_default_keys ⇒ Object
readonly
Use this configuration when you don’t want your configs to be validated.
-
#append_arrays ⇒ Object
readonly
With this enabled all array will be concatenated instead of replaced.
-
#default_files ⇒ Object
readonly
- String(Array)
-
A path to default yaml configs.
-
#default_placeholder ⇒ Object
readonly
- String
-
The value provided by default.
-
#env_prefix ⇒ Object
readonly
The prefix used to find env strings and args.
Instance Method Summary collapse
-
#[](key) ⇒ Object
provide a method for accessing configs.
-
#initialize(default_files:, default_placeholder: nil, append_arrays: false, env_prefix: nil, accept_default_keys: false) ⇒ ConfigurationManager
constructor
A new instance of ConfigurationManager.
-
#load_configs! ⇒ Object
Loads the configurations from all the possible sources.
-
#pretty_load_configs!(terminate = true) ⇒ Object
Use this when you require the application to do not start when something is missing and the error message should be displayed in stdout.
-
#to_h ⇒ Object
returns the object as a hash :nocov: #.
Methods included from Utils
#array_wrap, #parse_type, #recursive_merge, #recursive_set, #split_env_string, #symbolize
Constructor Details
#initialize(default_files:, default_placeholder: nil, append_arrays: false, env_prefix: nil, accept_default_keys: false) ⇒ ConfigurationManager
Returns a new instance of ConfigurationManager.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# 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 |
Instance Attribute Details
#accept_default_keys ⇒ Object (readonly)
Use this configuration when you don’t want your configs to be validated.
35 36 37 |
# File 'lib/ant/configs/configuration_manager.rb', line 35 def accept_default_keys @accept_default_keys end |
#append_arrays ⇒ Object (readonly)
With this enabled all array will be concatenated instead of replaced.
33 34 35 |
# File 'lib/ant/configs/configuration_manager.rb', line 33 def append_arrays @append_arrays end |
#default_files ⇒ Object (readonly)
- String(Array)
-
A path to default yaml configs.
28 29 30 |
# File 'lib/ant/configs/configuration_manager.rb', line 28 def default_files @default_files end |
#default_placeholder ⇒ Object (readonly)
- String
-
The value provided by default. It should mean this \
value is missing on configurations.
31 32 33 |
# File 'lib/ant/configs/configuration_manager.rb', line 31 def default_placeholder @default_placeholder end |
#env_prefix ⇒ Object (readonly)
The prefix used to find env strings and args.
37 38 39 |
# File 'lib/ant/configs/configuration_manager.rb', line 37 def env_prefix @env_prefix end |
Instance Method Details
#[](key) ⇒ Object
provide a method for accessing configs
91 92 93 |
# File 'lib/ant/configs/configuration_manager.rb', line 91 def [](key) @configs[key] end |
#load_configs! ⇒ Object
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.
57 58 59 60 61 62 63 |
# 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) ⇒ Object
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: #
70 71 72 73 74 75 76 77 78 79 80 |
# 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 ⇒ Object
returns the object as a hash :nocov: #
85 86 87 |
# File 'lib/ant/configs/configuration_manager.rb', line 85 def to_h @configs end |