Class: YleTf::Config::Loader

Inherits:
Object
  • Object
show all
Includes:
Defaults
Defined in:
lib/yle_tf/config/loader.rb

Constant Summary

Constants included from Defaults

Defaults::DEFAULT_CONFIG

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Defaults

#default_config, #default_config_context

Constructor Details

#initialize(opts) ⇒ Loader

Returns a new instance of Loader.



16
17
18
19
# File 'lib/yle_tf/config/loader.rb', line 16

def initialize(opts)
  @tf_env = opts.fetch(:tf_env)
  @module_dir = opts.fetch(:module_dir)
end

Instance Attribute Details

#module_dirObject (readonly)

Returns the value of attribute module_dir.



14
15
16
# File 'lib/yle_tf/config/loader.rb', line 14

def module_dir
  @module_dir
end

#tf_envObject (readonly)

Returns the value of attribute tf_env.



14
15
16
# File 'lib/yle_tf/config/loader.rb', line 14

def tf_env
  @tf_env
end

Instance Method Details

#config_contextObject



38
39
40
# File 'lib/yle_tf/config/loader.rb', line 38

def config_context
  @config_context ||= load_config_context
end

#config_filesObject



87
88
89
90
91
92
# File 'lib/yle_tf/config/loader.rb', line 87

def config_files
  module_dir.descend do |dir|
    file = dir.join('tf.yaml')
    yield(Config::File.new(file)) if file.exist?
  end
end

#deep_merge(config, new_config, opts = {}) ⇒ Object



80
81
82
83
84
85
# File 'lib/yle_tf/config/loader.rb', line 80

def deep_merge(config, new_config, opts = {})
  config.deep_merge!(new_config)
rescue StandardError => e
  Logger.fatal(opts[:error_msg]) if opts[:error_msg]
  raise e
end

#eval_config(config) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/yle_tf/config/loader.rb', line 94

def eval_config(config)
  case config
  when Hash
    config.each_with_object({}) { |(key, value), h| h[key] = eval_config(value) }
  when Array
    config.map { |item| eval_config(item) }
  when String
    Config::ERB.evaluate(config, config_context)
  else
    config
  end
end

#loadObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/yle_tf/config/loader.rb', line 21

def load
  Logger.debug('Loading default config')
  config = default_config
  Logger.debug(config.inspect)

  Logger.debug('Merging default configurations from plugins')
  merge_plugin_configurations(config)
  Logger.debug(config.inspect)

  Logger.debug('Merging configurations from files')
  merge_config_files(config)
  Logger.debug(config.inspect)

  Logger.debug('Evaluating the configuration strings')
  eval_config(config)
end

#load_config_contextObject



42
43
44
45
46
47
48
49
# File 'lib/yle_tf/config/loader.rb', line 42

def load_config_context
  Logger.debug('Loading config context')
  default_config_context.tap do |context|
    Logger.debug('Merging configuration contexts from plugins')
    merge_plugin_config_contexts(context)
    Logger.debug("config_context: #{context.inspect}")
  end
end

#merge_config_files(config) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/yle_tf/config/loader.rb', line 68

def merge_config_files(config)
  config_files do |file|
    Logger.debug("  - #{file}")
    deep_merge(
      config, file.read,
      error_msg:
        "Failed to merge configuration from '#{file}' into:\n" \
        "#{config.inspect}"
    )
  end
end

#merge_plugin_config_contexts(context) ⇒ Object



51
52
53
54
55
# File 'lib/yle_tf/config/loader.rb', line 51

def merge_plugin_config_contexts(context)
  Plugin.manager.config_contexts.each do |plugin_context|
    context.merge!(plugin_context)
  end
end

#merge_plugin_configurations(config) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/yle_tf/config/loader.rb', line 57

def merge_plugin_configurations(config)
  Plugin.manager.default_configs.each do |plugin_config|
    deep_merge(
      config, plugin_config,
      error_msg:
        "Failed to merge a plugin's default configuration:\n" \
        "#{plugin_config.inspect}\ninto:\n#{config.inspect}"
    )
  end
end