Class: Kybus::Configuration::ConfigurationManager

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

Overview

This class provides a module for loading configurations from 4 sources:

  • YAML default files

  • YAML files

  • ENV vars

  • ARGV values

Using yaml defaults: TODO: Add docs Using yaml files TODO: Add docs Using env vars: TODO: Add docs Using arg vars: TODO: Add docs

Defined Under Namespace

Classes: MissingConfigs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

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

Constructor Details

#initialize(default_files:, default_placeholder: nil, append_arrays: false, env_prefix: nil, accept_default_keys: false) ⇒ ConfigurationManager

Returns a new instance of ConfigurationManager.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kybus/configs/configuration_manager.rb', line 39

def initialize(default_files:,
               default_placeholder: nil,
               append_arrays: false,
               env_prefix: nil,
               accept_default_keys: false)
  @default_files = array_wrap(default_files)
  @default_placeholder = default_placeholder || 'REPLACE_ME'
  @append_arrays = append_arrays
  @env_prefix = env_prefix || 'CONFIG'
  @accept_default_keys = accept_default_keys
  @configs = {}
  @env_vars = env_vars
  @config_files = env_files
end

Instance Attribute Details

#accept_default_keysObject (readonly)

Use this configuration when you don’t want your configs to be validated.



35
36
37
# File 'lib/kybus/configs/configuration_manager.rb', line 35

def accept_default_keys
  @accept_default_keys
end

#append_arraysObject (readonly)

With this enabled all array will be concatenated instead of replaced.



33
34
35
# File 'lib/kybus/configs/configuration_manager.rb', line 33

def append_arrays
  @append_arrays
end

#default_filesObject (readonly)

String(Array)

A path to default yaml configs.



28
29
30
# File 'lib/kybus/configs/configuration_manager.rb', line 28

def default_files
  @default_files
end

#default_placeholderObject (readonly)

String

The value provided by default. It should mean this \

value is missing on configurations.



31
32
33
# File 'lib/kybus/configs/configuration_manager.rb', line 31

def default_placeholder
  @default_placeholder
end

#env_prefixObject (readonly)

The prefix used to find env strings and args.



37
38
39
# File 'lib/kybus/configs/configuration_manager.rb', line 37

def env_prefix
  @env_prefix
end

Class Method Details

.auto_load!Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/kybus/configs/configuration_manager.rb', line 95

def self.auto_load!
  auto_configs = new(default_files: './config/autoconfig.yaml')
  auto_configs.load_configs!
  auto_configs = auto_configs['autoconfig']
  configs = new(
    default_files: (auto_configs['default_files'] || []) +
                   (auto_configs['files'] || []) +
                   ['./config/autoconfig.yaml'],
    default_placeholder: auto_configs['default_placeholder'],
    accept_default_keys: auto_configs['accept_default_keys'],
    env_prefix: auto_configs['env_prefix']
  )
  configs.load_configs!
  configs
end

Instance Method Details

#[](key) ⇒ Object

provide a method for accessing configs



91
92
93
# File 'lib/kybus/configs/configuration_manager.rb', line 91

def [](key)
  @configs[key]
end

#load_configs!Object

Loads the configurations from all the possible sources. It will raise an exception when it is required to validate that no default placeholder is present on the configs.

Raises:



57
58
59
60
61
62
63
# File 'lib/kybus/configs/configuration_manager.rb', line 57

def load_configs!
  load_configs
  missing_keys = missing_configs
  return if missing_keys.empty? || @accept_default_keys

  raise MissingConfigs, missing_keys
end

#pretty_load_configs!(terminate = true) ⇒ Object

Use this when you require the application to do not start when something is missing and the error message should be displayed in stdout. This is helpful when you are launching your app and you need to trace any misconfiguration problem. :nocov: #



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/kybus/configs/configuration_manager.rb', line 70

def pretty_load_configs!(terminate = true)
  load_configs!
rescue MissingConfigs => e
  puts 'You are missing some configs!'
  puts 'Add them to a file and export the config env var:'
  puts "$ export #{@env_prefix}_FILES='#{Dir.pwd}'/config/config.yaml"
  puts 'Maybe you just need to add them to your existing files'
  puts 'Missing configs:'
  e.keys.each { |k| puts "- \"#{k}\"" }
  exit(1) if terminate
end

#to_hObject

returns the object as a hash :nocov: #



85
86
87
# File 'lib/kybus/configs/configuration_manager.rb', line 85

def to_h
  @configs
end