Module: Chamber::SystemEnvironment

Defined in:
lib/chamber/system_environment.rb

Class Method Summary collapse

Class Method Details

.extract_from(settings, parent_keys = []) ⇒ Object

Internal: Allows the environment variable-compatible variables to be extracted from a passed in hash.

Examples:

###
# Extracts the environment variables based on the hash keys
#
SystemEnvironment.extract_from(
  level_one_1: {
    level_two_1: 'value 1',
    level_two_2: {
      level_three_1: 'value 2' } } )

# => {
  'LEVEL_ONE_1_LEVEL_TWO_1'               => 'env value 1',
  'LEVEL_ONE_1_LEVEL_TWO_2_LEVEL_THREE_1' => 'env value 2',
}

###
# Can extract environment variables if said variables are prefixed
#
SystemEnvironment.extract_from({
                                level_two_1: 'value 1',
                                level_two_2: 'value 2'
                              },
                              ['prefix'])

# => {
  'PREFIX_LEVEL_TWO_1' => 'value 1',
  'PREFIX_LEVEL_TWO_2' => 'value 2',
}


98
99
100
101
102
103
104
105
106
# File 'lib/chamber/system_environment.rb', line 98

def self.extract_from(settings, parent_keys = [])
  with_environment(settings, parent_keys,
    ->(key, value, environment_keys) do
      extract_from(value, environment_keys)
    end,
    ->(key, value, environment_key) do
      { environment_key => value.to_s }
    end)
end

.inject_into(settings = {}, parent_keys = []) ⇒ Object

Internal: Allows the existing environment to be injected into the passed in hash. The hash that is passed in is not modified, instead a new hash is returned.

Examples:

###
# Injects the current environment variables
#
ENV['LEVEL_ONE_1_LEVEL_TWO_1']               = 'env value 1'
ENV['LEVEL_ONE_1_LEVEL_TWO_2_LEVEL_THREE_1'] = 'env value 2'

SystemEnvironment.inject_into(
  level_one_1: {
    level_two_1: 'value 1',
    level_two_2: {
      level_three_1: 'value 2' } } )

# => {
  'level_one_1' => {
    'level_two_1' => 'env value 1',
    'level_two_2' => {
      'level_three_1' => 'env value 2',
}

###
# Can inject environment variables if said variables are prefixed
#
ENV['PREFIX_LEVEL_TWO_1'] = 'env value 1'
ENV['PREFIX_LEVEL_TWO_2'] = 'env value 2'

SystemEnvironment.inject_into({
                                level_two_1: 'value 1',
                                level_two_2: 'value 2'
                              },
                              ['prefix'])

# => {
  'level_two_1' => 'env value 1',
  'level_two_2' => 'env value 2',
}


54
55
56
57
58
59
60
61
62
# File 'lib/chamber/system_environment.rb', line 54

def self.inject_into(settings = {}, parent_keys = [])
  with_environment(settings, parent_keys,
    ->(key, value, environment_keys) do
      { key => inject_into(value, environment_keys) }
    end,
    ->(key, value, environment_key) do
      { key => convert_value(ENV[environment_key] || value) }
    end)
end