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'- DEFAULT_ENV_VAR_PREFIX =
the default prefix that goes before environment variables and the environments section in config files
'T_'
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_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.
52 53 54 55 56 |
# File 'lib/tcfg/tcfg_helper.rb', line 52 def tcfg @tcfg_resolved_config ||= resolve_config #return a deep copy of the configuration object to prevent mutations @tcfg_resolved_config.deep_dup 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.
71 72 73 74 75 76 |
# File 'lib/tcfg/tcfg_helper.rb', line 71 def tcfg_config_file filename confirm_config_file_existence filename tcfg_reset @tcfg_config_filename = filename nil end |
#tcfg_get(key) ⇒ String, ...
return a single piece of configuration by key
111 112 113 114 115 116 117 |
# File 'lib/tcfg/tcfg_helper.rb', line 111 def tcfg_get key t_tcfg = tcfg unless t_tcfg.has_key? key raise NoSuchConfigurationKeyError.new "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
129 130 131 |
# File 'lib/tcfg/tcfg_helper.rb', line 129 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
87 88 89 90 91 92 |
# File 'lib/tcfg/tcfg_helper.rb', line 87 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
100 101 102 103 104 |
# File 'lib/tcfg/tcfg_helper.rb', line 100 def tcfg_set key, value tier_code_defaults[key] = value tcfg_reset return 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
151 152 153 154 |
# File 'lib/tcfg/tcfg_helper.rb', line 151 def tcfg_set_env_var_prefix prefix @tcfg_env_var_prefix = prefix tcfg_reset end |