Module: Appfuel::Config::Populate

Included in:
DefinitionDsl
Defined in:
lib/appfuel/config/populate.rb

Instance Method Summary collapse

Instance Method Details

#load_env(env_data, definition) ⇒ Hash

Parameters:

Returns:

  • (Hash)


45
46
47
48
49
50
51
52
53
# File 'lib/appfuel/config/populate.rb', line 45

def load_env(env_data, definition)
  config = {}
  definition.env.each do |env_key, config_key|
    env_key = env_key.to_s
    next unless env_data.key?(env_key)
    config[config_key] = env_data[env_key]
  end
  config
end

#populate(data = {}) ⇒ Object

This converts a definition into a hash of configuation values. It does this using the following steps

  1. load config data from a yaml or json file if a file is defined

  2. populate all children

  3. merge defaults into config data that has been given or resolved from the config file

  4. merge override data into the results from step 3

  5. run validation and assign clean data to config with the key

Parameters:

  • data (defaults to: {})

    Hash holds overrides and config source data

Returns:

  • Hash



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/appfuel/config/populate.rb', line 17

def populate(data = {})
  overrides = data[:overrides] || {}
  config    = data[:config]    || {}
  env_data  = data[:env]       || ENV

  if overrides.key?(:config_file) && !overrides[:config_file].nil?
    file overrides[:config_file]
  end

  if file?
    config = load_file(self)
  else
    config = config[key]
  end

  config ||= {}
  config = defaults.deep_merge(config)
  config = config.deep_merge(load_env(env_data, self))
  config = config.deep_merge(overrides || {})

  populate_children(children, config, env_data) unless children.empty?

  handle_validation(config)
end