Class: ConfigurationService::Factory::EnvironmentContext

Inherits:
Context
  • Object
show all
Includes:
EnvironmentContextBackwardCompatibility
Defined in:
lib/configuration_service/factory/environment_context.rb

Overview

A factory context for the process ENV and (on JRuby) system properties

Most consumers will not need to interact with this class directly. It is used by ConfigurationService::Factory to create the default factory context if none is specified.

Prior to version 2.2.0, this class provided factory functionality. Use of this class as a factory is deprecated, supported through the EnvironmentContextBackwardCompatibility module. Consumers should use ConfigurationService::Factory instead.

Constant Summary collapse

DEFAULT_PREFIX =

The default prefix for matching environment variable names

"CFGSRV"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EnvironmentContextBackwardCompatibility

#create

Methods inherited from Context

#decorators, #identifier, #identifier?, #provider_config, #provider_id, #token

Constructor Details

#initialize(sources = default_sources, prefix = DEFAULT_PREFIX) ⇒ EnvironmentContext

Returns a new factory context

The sources are scanned for #prefix matches, within which the following variables are used:

IDENTIFIER

the unique identity of the configuration data (see Client#initialize)

TOKEN

authorization token for the identified configuration data (see Client#initialize)

PROVIDER

the unique identity of the service provider (see ProviderRegistry)

PROVIDER_*

configuration options for the service provider (see service provider documentation)

DECORATORS

list of decorators to compose into the service (see Decorator and #DecoratorRegistry)

If sources are not specified, the process ENV and (on JRuby) system properties are used. Where a system property and environment variable of the same name exist, the environment variable is preferred.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/configuration_service/factory/environment_context.rb', line 56

def initialize(sources = default_sources, prefix = DEFAULT_PREFIX)
  @sources = [sources].flatten
  @prefix = prefix
  @scrub_keys = []
  env = Hash.new { |_, k| raise KeyError, "missing environment variable #{k}" }

  merged_sources = @sources.inject { |m, e| m.merge(e) }
  {
    to_envvar("IDENTIFIER") => :identifier,
    to_envvar("TOKEN") => :token,
    to_envvar("PROVIDER") => :provider_id,
    to_envvar("DECORATORS") => :decorators
  }.select { |envvar, key|
    merged_sources.include?(envvar)
  }.each { |envvar, key|
    env[key] = merged_sources[envvar]
    @scrub_keys << envvar
  }
  env[:decorators] = env[:decorators].split(/[,\s]+/) if env.include?(:decorators)
  config_prefix = to_envvar("PROVIDER_")
  env[:provider_config] = merged_sources
    .select { |envvar| envvar.start_with?(config_prefix) && @scrub_keys << envvar }
    .map { |envvar, value| [envvar.sub(config_prefix, "").downcase.intern, value] }
    .to_h
  @env = Context::SymbolicAccessWrapper.new(env)
end

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



24
25
26
# File 'lib/configuration_service/factory/environment_context.rb', line 24

def prefix
  @prefix
end

Instance Method Details

#scrub!Object

Scrub sources

Every property that matches the prefix is removed from all sources.

See Also:



90
91
92
93
94
95
# File 'lib/configuration_service/factory/environment_context.rb', line 90

def scrub!
  @scrub_keys.each do |k|
    @sources.each { |s| s.delete(k) }
  end
  super
end