class EnvLoader::Configurator

Attributes

raw_config[R]

Public Class Methods

new(config_file: nil, config_content: nil, config_subtree_key: nil) click to toggle source
# File lib/env_loader/configurator.rb, line 8
def initialize(config_file: nil, config_content: nil, config_subtree_key: nil)
  if config_file
    @raw_config = config_from_file(config_file, config_subtree_key)
  elsif config_content
    @raw_config = config_from_content(config_content, config_subtree_key)
  end
end

Public Instance Methods

config() click to toggle source
# File lib/env_loader/configurator.rb, line 16
def config
  raw_config.each_with_object({}) do |hash, new_hash|
    key, value = hash
    value = value.is_a?(Hash) ? value.to_json : value.to_s unless value.nil?
    new_hash[key.upcase] = value
  end
end
expose_values_as_environment_variables() click to toggle source
# File lib/env_loader/configurator.rb, line 24
def expose_values_as_environment_variables
  config.each { |key, value| ENV[key] = value }
end

Private Instance Methods

config_from_content(config_content, config_subtree_key = nil) click to toggle source
# File lib/env_loader/configurator.rb, line 36
def config_from_content(config_content, config_subtree_key = nil)
  global_config = YAML.load(config_content)
  return global_config unless config_subtree_key
  extract_subhash_from_hash_and_keys(global_config, config_subtree_key.to_s.split('.'))
end
config_from_file(config_file, config_subtree_key = nil) click to toggle source
# File lib/env_loader/configurator.rb, line 30
def config_from_file(config_file, config_subtree_key = nil)
  return {} unless File.exist?(config_file)
  config_content = File.read(config_file)
  config_from_content(config_content, config_subtree_key)
end
extract_subhash_from_hash_and_keys(hash, subhash_keys) click to toggle source
# File lib/env_loader/configurator.rb, line 42
def extract_subhash_from_hash_and_keys(hash, subhash_keys)
  key = subhash_keys.shift
  return unless key
  return hash['default'] || {} unless hash.key?(key)
  return hash[key] if subhash_keys.empty?
  extract_subhash_from_hash_and_keys(hash[key], subhash_keys)
end