Class: Chamber::Filters::EnvironmentFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/chamber/filters/environment_filter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, secure_key_prefix:, **_args) ⇒ EnvironmentFilter

Returns a new instance of EnvironmentFilter.



97
98
99
100
# File 'lib/chamber/filters/environment_filter.rb', line 97

def initialize(data:, secure_key_prefix:, **_args)
  self.data             = data
  self.secure_key_token = /\A#{Regexp.escape(secure_key_prefix)}/
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



10
11
12
# File 'lib/chamber/filters/environment_filter.rb', line 10

def data
  @data
end

#secure_key_tokenObject

Returns the value of attribute secure_key_token.



10
11
12
# File 'lib/chamber/filters/environment_filter.rb', line 10

def secure_key_token
  @secure_key_token
end

Class Method Details

.execute(**args) ⇒ 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.

This filter will also do basic value conversions from the environment variable string to the data type defined in the YAML. For example if the YAML value is ‘true`, then the conversion knows it’s a Boolean. If there’s an environment varible which should override that value, it will look to see if it is a ‘String` of ’true’, ‘false’, ‘t’, ‘f’, ‘yes’, or ‘no’ and perform the appropriate conversion of that value into a Boolean.

This will work for:

* Booleans
* Integers
* Floats
* Arrays

For the Arrays, it will convert the environment value by parsing the string as YAML. Whatever the parsed value ends up being, must be an Array.

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'

EnvironmentFilter.execute(
  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 do basic value conversions based on the raw data
#
ENV['LEVEL_ONE_1_LEVEL_TWO_1']               = '1'
ENV['LEVEL_ONE_1_LEVEL_TWO_2_LEVEL_THREE_1'] = '[1, 2, 3]'

EnvironmentFilter.execute(
  level_one_1: {
    level_two_1: 4,
    level_two_2: {
      level_three_1: [4, 5, 6] } } )

# => {
  'level_one_1' => {
    'level_two_1' => 1,
    'level_two_2' => {
      'level_three_1' => [1, 2, 3],
}

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

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

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


93
94
95
# File 'lib/chamber/filters/environment_filter.rb', line 93

def self.execute(**args)
  new(**args).__send__(:execute)
end