Sha256: 82db34f95eb49adbd8a07c6800f8a1da0ea397f7e8c844926e253266612dca76

Contents?: true

Size: 1.98 KB

Versions: 5

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true
require 'chamber/environmentable'

module  Chamber
module  Filters
class   EnvironmentFilter
  include Environmentable

  def initialize(options = {})
    self.data = options.fetch(:data)
  end

  ###
  # 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'
  #
  #   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 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',
  #   }
  #
  #
  def self.execute(options = {})
    new(options).__send__(:execute)
  end

  protected

  attr_accessor :data

  def execute(settings = data, parent_keys = [])
    with_environment(settings, parent_keys,
                     lambda do |key, value, environment_keys|
                       { key => execute(value, environment_keys) }
                     end,
                     lambda do |key, value, environment_key|
                       { key => (ENV[environment_key] || value) }
                     end)
  end
end
end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
chamber-2.10.2 lib/chamber/filters/environment_filter.rb
chamber-2.10.1 lib/chamber/filters/environment_filter.rb
chamber-2.10.0 lib/chamber/filters/environment_filter.rb
chamber-2.9.1 lib/chamber/filters/environment_filter.rb
chamber-2.9.0 lib/chamber/filters/environment_filter.rb