Module: TCFG::Helper
Overview
TCFG::Helper does all the “heavy lifting”. Essentially all the logic within TCFG is defined by this module. The intended ways to use this module are:
-
use the TCFG module methods whenever a single instance of configuration will do
TCFG['some_key'] => 'some_value' TCFG.tcfg => { 'some_key' => 'some_value', ... }
-
mix it into any class that needs configuration
Class MyClass include TCFG::Helper end Myclass.new.tcfg => { 'some_key' => 'some_value', ... }
-
create a configuration instance object with this module pre-mixed in
cfg = TCFG::Base.new cfg['some_key'] => 'some_value' cfg.tcfg => { 'some_key' => 'some_value', ... }
Constant Summary collapse
- DEFAULT_CONFIG_FILE =
the public config file that is looked for unless tcfg_config_file is called
'tcfg.yml'.freeze
- DEFAULT_ENV_VAR_PREFIX =
the default prefix that goes before environment variables and the environments section in config files
'T_'.freeze
Instance Method Summary collapse
-
#tcfg ⇒ ActiveSupport::HashWithIndifferentAccess
return a copy of the resolved configuration.
-
#tcfg_config_file(filename) ⇒ nil
change the name of the public configuration file.
-
#tcfg_fetch(key, alt_value = nil) ⇒ Object
like tcfg_get but doesnt raise an exception if key is not defined.
-
#tcfg_get(key) ⇒ String, ...
return a single piece of configuration by key.
-
#tcfg_reset ⇒ nil
force tcfg to re-resolve the configuration.
-
#tcfg_secret_config_file(filename) ⇒ nil
change the name of the secret configuration file.
-
#tcfg_set(key, value) ⇒ Object
to correct way to default configuration is to use tcfg_set.
-
#tcfg_set_env_var_prefix(prefix) ⇒ nil
change the prefix used for configuration finding.
Instance Method Details
#tcfg ⇒ ActiveSupport::HashWithIndifferentAccess
return a copy of the resolved configuration
This is the preferred way to access a complete copy of the fully resolved configuration.
62 63 64 65 66 |
# File 'lib/tcfg/tcfg_helper.rb', line 62 def tcfg @tcfg_resolved_config ||= resolve_config # return a frozen deep copy of the configuration object to prevent mutations @tcfg_resolved_config.deep_dup.freeze end |
#tcfg_config_file(filename) ⇒ nil
change the name of the public configuration file
Changing the name of the public configuration file also changes the default secret configuration file. For example calling #tcfg_config_file(‘my_cfg.ym’) will cause TCFG to look for ‘my_cfg.secret.yml’ for the secret file unless #tcfg_secret_config_file is also called.
81 82 83 84 85 86 |
# File 'lib/tcfg/tcfg_helper.rb', line 81 def tcfg_config_file(filename) confirm_config_file_existence filename tcfg_reset @tcfg_config_filename = filename nil end |
#tcfg_fetch(key, alt_value = nil) ⇒ Object
like tcfg_get but doesnt raise an exception if key is not defined
130 131 132 |
# File 'lib/tcfg/tcfg_helper.rb', line 130 def tcfg_fetch(key, alt_value = nil) tcfg.fetch key, alt_value end |
#tcfg_get(key) ⇒ String, ...
return a single piece of configuration by key
121 122 123 124 125 126 127 |
# File 'lib/tcfg/tcfg_helper.rb', line 121 def tcfg_get(key) t_tcfg = tcfg unless t_tcfg.key? key raise NoSuchConfigurationKeyError, "No configuration defined for '#{key}'" end t_tcfg[key] end |
#tcfg_reset ⇒ nil
force tcfg to re-resolve the configuration
This method can be called to force tcfg to re-resolve the configuration. This generally should not be needed directly, but situations where it could be used include:
-
The underlying config file(s) have changed and you want to re-read them
-
The underlying ENV environment variables have changed and you want to re-read them
144 145 146 |
# File 'lib/tcfg/tcfg_helper.rb', line 144 def tcfg_reset @tcfg_resolved_config = nil end |
#tcfg_secret_config_file(filename) ⇒ nil
change the name of the secret configuration file
Calling this method if neccesary only if:
-
you dont have a public configuration file, or
-
your secret file is not named like <public name>.secret.yml
97 98 99 100 101 102 |
# File 'lib/tcfg/tcfg_helper.rb', line 97 def tcfg_secret_config_file(filename) confirm_config_file_existence filename tcfg_reset @tcfg_secret_config_filename = filename nil end |
#tcfg_set(key, value) ⇒ Object
to correct way to default configuration is to use tcfg_set
110 111 112 113 114 |
# File 'lib/tcfg/tcfg_helper.rb', line 110 def tcfg_set(key, value) tier_code_defaults[key] = value tcfg_reset value end |
#tcfg_set_env_var_prefix(prefix) ⇒ nil
change the prefix used for configuration finding
By default TCFG looks for
-
environment variables prefixed with T_
-
sections in config files called t_environments
This method lets you change that to any prefic you want. For example calling it like this:
TCFG.tcfg_set_env_var_prefix 'MY_'
Will cause tcfg to look for:
-
environment variables prefixed with MY_
-
sections in config files called my_environments
166 167 168 169 |
# File 'lib/tcfg/tcfg_helper.rb', line 166 def tcfg_set_env_var_prefix(prefix) @tcfg_env_var_prefix = prefix tcfg_reset end |