Class: BlueprintConfig::Backend::ENV

Inherits:
Base
  • Object
show all
Defined in:
lib/blueprint_config/backend/env.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#fresh?, #nest_hash, #source

Constructor Details

#initialize(options = {}) ⇒ ENV

Returns a new instance of ENV.



10
11
12
# File 'lib/blueprint_config/backend/env.rb', line 10

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/blueprint_config/backend/env.rb', line 8

def options
  @options
end

Instance Method Details

#env_downcasedObject



21
22
23
# File 'lib/blueprint_config/backend/env.rb', line 21

def env_downcased
  @env_downcased ||= ::ENV.to_h.transform_keys(&:downcase)
end

#env_downcased_keysObject



25
26
27
# File 'lib/blueprint_config/backend/env.rb', line 25

def env_downcased_keys
  @env_downcased_keys ||= env_downcased.keys
end

#filtered_envObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/blueprint_config/backend/env.rb', line 29

def filtered_env
  return env_downcased if @options[:allow_all]

  allowed_keys = @options[:whitelist_keys]&.map(&:to_s) || []
  @options[:whitelist_prefixes]&.each do |prefix|
    allowed_keys += env_downcased_keys.select { |key| key.to_s.start_with?(prefix.to_s) }
  end

  env_downcased.slice(* allowed_keys)
end

#load_keysObject



14
15
16
17
18
19
# File 'lib/blueprint_config/backend/env.rb', line 14

def load_keys
  {
    # env: ::ENV.to_h.transform_keys(&:to_sym),
    ** transformed_env
  }
end

#transformed_envObject



40
41
42
43
44
# File 'lib/blueprint_config/backend/env.rb', line 40

def transformed_env
  return filtered_env.transform_keys(&:to_sym) unless @options[:nest]

  nest_hash(filtered_env, @options[:nest_separator] || '_')
end