Class: ConfigurationService::Factory::EnvironmentContext
- Inherits:
-
Object
- Object
- ConfigurationService::Factory::EnvironmentContext
- Defined in:
- lib/configuration_service/factory/environment_context.rb,
lib/configuration_service/factory/environment_context/env_dict.rb
Overview
A factory for creating a configuration service bootstrapped from environmental configuration
Defined Under Namespace
Classes: EnvDict
Constant Summary collapse
- DEFAULT_PREFIX =
The default prefix for matching environment variable names
"CFGSRV"
Instance Attribute Summary collapse
-
#prefix ⇒ String
readonly
prefix for matching environment variable names.
Class Method Summary collapse
-
.create ⇒ Object
Return a configuration service bootstrapped with environmental configuration.
Instance Method Summary collapse
-
#create ⇒ ConfigurationService::Base
Create a configuration service bootstrapped with environmental configuration.
-
#initialize(sources = default_sources, prefix = DEFAULT_PREFIX) ⇒ EnvironmentContext
constructor
Returns a new factory.
Constructor Details
#initialize(sources = default_sources, prefix = DEFAULT_PREFIX) ⇒ EnvironmentContext
Returns a new factory
Most consumers will not need to call this method; they can use create which instantiates a factory with default env and prefix and uses that internally.
58 59 60 |
# File 'lib/configuration_service/factory/environment_context.rb', line 58 def initialize(sources = default_sources, prefix = DEFAULT_PREFIX) @env = EnvDict.new(sources, prefix) end |
Instance Attribute Details
#prefix ⇒ String (readonly)
prefix for matching environment variable names. Names match if they begin with the prefix and an underscore.
37 38 39 |
# File 'lib/configuration_service/factory/environment_context.rb', line 37 def prefix @prefix end |
Class Method Details
.create ⇒ Object
Return a configuration service bootstrapped with environmental configuration
Instantiates a new factory with the process ENV and the DEFAULT_PREFIX and uses it to create a configuration service.
105 106 107 |
# File 'lib/configuration_service/factory/environment_context.rb', line 105 def self.create new.create end |
Instance Method Details
#create ⇒ ConfigurationService::Base
Create a configuration service bootstrapped with environmental configuration
The environment is scanned for #prefix matches, within which the following variables are used:
- IDENTIFIER
-
the unique identity of the configuration data (see Base#initialize)
- TOKEN
-
authorization token for the identified configuration data (see Base#initialize)
- PROVIDER
-
the unique identity of the service provider (see ProviderRegistry)
- PROVIDER_*
-
configuration options for the service provider (see service provider documentation)
On JRuby, system properties are also scanned. Where a system property and environment variable of the same name exist, the environment variable is preferred.
The service provider class is fetched from the ProviderRegistry using PROVIDER. A service provider instance is then constructed with a dictionary of the PROVIDER_* variables, in which the keys are the name of the variable without PROVIDER_, downcased and intern’d.
Then a service Base is constructed with the IDENTIFIER, TOKEN and service provider instance.
And finally, the environment is scrubbed of the variables used, to protect them from accidental exposure (e.g. in an exception handler that prints the environment). On JRuby, system properties are scrubbed of variables used as well, regardless of whether they were overridden by environment variables.
92 93 94 95 96 |
# File 'lib/configuration_service/factory/environment_context.rb', line 92 def create ConfigurationService.new(@env[:identifier], @env[:token], provider).tap do @env.scrub! end end |