Class: Kybus::Configuration::Loaders::Env

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/kybus/configs/loaders/env.rb

Overview

This class allows to load configurations from ENV It requires that all the vars are named with a common prefix It uses ‘__’ as a delimiter to allow nested configurations

Examples

export CONFIG_ENV_VALUE=3 => { 'env_value' => '3' }
export CONFIG_ENV_OBJ__VALUE=3 => { "env_obj" => { 'value' => '3' } }

Instance Method Summary collapse

Methods included from Utils

#array_wrap, #parse_type, #recursive_merge, #recursive_set, #split_env_string, #symbolize

Constructor Details

#initialize(env_prefix, manager, conf = ENV.to_h) ⇒ Env

Returns a new instance of Env.



14
15
16
17
18
# File 'lib/kybus/configs/loaders/env.rb', line 14

def initialize(env_prefix, manager, conf = ENV.to_h)
  @env_prefix = env_prefix
  @manager = manager
  @env = conf
end

Instance Method Details

#load!Object

Parses the configurations from ENV



21
22
23
24
25
26
27
28
29
30
# File 'lib/kybus/configs/loaders/env.rb', line 21

def load!
  @env.select { |str, _| str.start_with?(@env_prefix) }
      .each_with_object({}) do |(k, v), h|
    # Remove ENV prefix and convert to downcase, then split by '__'
    clean_key = k.sub(/^#{@env_prefix}_?/, '').downcase.split('__')
    # recursively create the objects to set the config where it
    # should be
    recursive_set(h, clean_key, split_env_string(v))
  end
end