Module: TCFG::Helper

Included in:
TCFG, Base
Defined in:
lib/tcfg/tcfg_helper.rb

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

Instance Method Details

#tcfgActiveSupport::HashWithIndifferentAccess

return a copy of the resolved configuration

This is the preferred way to access a complete copy of the fully resolved configuration.

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)

    a copy of the 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.

Parameters:

  • filename (String)

    the path to a yaml file

Returns:

  • (nil)

See Also:



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_fetch(key, alt_value = nil) ⇒ Object

like tcfg_get but doesnt raise an exception if key is not defined



120
121
122
# File 'lib/tcfg/tcfg_helper.rb', line 120

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

Parameters:

  • key (String)

    the configuration to return

Returns:

  • (String, Integer, FixNum, Array, Hash)

    the value of the configuration from the resolved configuration



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_resetnil

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

Returns:

  • (nil)


134
135
136
# File 'lib/tcfg/tcfg_helper.rb', line 134

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

Parameters:

  • filename (String)

    the path to a yaml file

Returns:

  • (nil)


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

Parameters:

  • key (String)

    the configuration key name

  • value (String, Integer, FixNum, Array, Hash)

    the value of the configuration

Returns:

  • value The same value that was passed in



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

Parameters:

  • prefix (String)

    the new prefix. It can be an empty string to specify no prefix should be used.

Returns:

  • (nil)


156
157
158
159
# File 'lib/tcfg/tcfg_helper.rb', line 156

def tcfg_set_env_var_prefix prefix
  @tcfg_env_var_prefix = prefix
  tcfg_reset
end