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

#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)

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.

Parameters:

  • sources (Hash, Array<Hash>) (defaults to: default_sources)

    one or a list of the string-keyed environments

  • prefix (String) (defaults to: DEFAULT_PREFIX)

    the prefix for matching environment variable names



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

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
  }.each do |envvar, key|
    env[key] = merged_sources[envvar]
    @scrub_keys << envvar
  end
  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:



84
85
86
87
88
89
# File 'lib/configuration_service/factory/environment_context.rb', line 84

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