Class: YleTf::Config::Loader
- Inherits:
-
Object
- Object
- YleTf::Config::Loader
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.
18
19
20
21
|
# File 'lib/yle_tf/config/loader.rb', line 18
def initialize(opts)
@tf_env = opts.fetch(:tf_env)
@module_dir = opts.fetch(:module_dir)
end
|
Instance Attribute Details
#module_dir ⇒ Object
Returns the value of attribute module_dir.
16
17
18
|
# File 'lib/yle_tf/config/loader.rb', line 16
def module_dir
@module_dir
end
|
#tf_env ⇒ Object
Returns the value of attribute tf_env.
16
17
18
|
# File 'lib/yle_tf/config/loader.rb', line 16
def tf_env
@tf_env
end
|
Instance Method Details
#config_context ⇒ Object
108
109
110
|
# File 'lib/yle_tf/config/loader.rb', line 108
def config_context
@config_context ||= load_config_context
end
|
#config_files ⇒ Object
76
77
78
79
80
81
|
# File 'lib/yle_tf/config/loader.rb', line 76
def config_files
module_dir.descend.lazy
.map { |dir| dir.join('tf.yaml') }
.select(&:exist?)
.map { |file| Config::File.new(file) }
end
|
#deep_merge(prev_config, config, **opts) ⇒ Object
98
99
100
101
102
103
104
105
106
|
# File 'lib/yle_tf/config/loader.rb', line 98
def deep_merge(prev_config, config, **opts)
task(' -> Merging') do
Helpers::Hash.deep_merge(prev_config, config)
end
rescue StandardError => e
Logger.fatal("Failed to merge configuration from #{opts[:source]}:\n" \
"#{config.inspect}\ninto:\n#{prev_config.inspect}")
raise e
end
|
#eval_config(config) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/yle_tf/config/loader.rb', line 59
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
|
#evaluate_configuration_strings(config) ⇒ Object
55
56
57
|
# File 'lib/yle_tf/config/loader.rb', line 55
def evaluate_configuration_strings(config)
task('Evaluating the configuration strings') { eval_config(config) }
end
|
#load ⇒ Object
23
24
25
26
27
28
29
30
31
|
# File 'lib/yle_tf/config/loader.rb', line 23
def load
load_sequence = i[
load_default_config
load_plugin_configurations
load_config_files
evaluate_configuration_strings
]
load_sequence.inject({}) { |config, method| send(method, config) }
end
|
#load_config_context ⇒ Object
112
113
114
115
116
117
118
119
|
# File 'lib/yle_tf/config/loader.rb', line 112
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
|
#load_config_files(config) ⇒ Object
46
47
48
49
50
51
52
53
|
# File 'lib/yle_tf/config/loader.rb', line 46
def load_config_files(config)
Logger.debug('Loading configuration from files')
config_files.inject(config) do |prev_config, file|
migrate_and_merge_configuration(prev_config, file.read,
type: 'file', name: file.name)
end
end
|
#load_default_config(_config) ⇒ Object
33
34
35
|
# File 'lib/yle_tf/config/loader.rb', line 33
def load_default_config(_config)
task('Loading default config') { default_config }
end
|
#load_plugin_configurations(config) ⇒ Object
37
38
39
40
41
42
43
44
|
# File 'lib/yle_tf/config/loader.rb', line 37
def load_plugin_configurations(config)
Logger.debug('Loading configuration from plugins')
plugins.inject(config) do |prev_config, plugin|
migrate_and_merge_configuration(prev_config, plugin.default_config,
type: 'plugin', name: plugin.to_s)
end
end
|
#merge_plugin_config_contexts(context) ⇒ Object
121
122
123
124
125
|
# File 'lib/yle_tf/config/loader.rb', line 121
def merge_plugin_config_contexts(context)
Plugin.manager.config_contexts.each do |plugin_context|
context.merge!(plugin_context)
end
end
|
#migrate_and_merge_configuration(prev_config, config, **opts) ⇒ Object
83
84
85
86
87
88
89
90
|
# File 'lib/yle_tf/config/loader.rb', line 83
def migrate_and_merge_configuration(prev_config, config, **opts)
task("- #{opts[:name]}") { config }
return prev_config if config.empty?
source = "#{opts[:type]}: '#{opts[:name]}'"
config = migrate_old_config(config, source: source)
deep_merge(prev_config, config, source: source)
end
|
#migrate_old_config(config, **opts) ⇒ Object
92
93
94
95
96
|
# File 'lib/yle_tf/config/loader.rb', line 92
def migrate_old_config(config, **opts)
task(' -> Migrating') do
Config::Migration.migrate_old_config(config, **opts)
end
end
|
#plugins ⇒ Object
72
73
74
|
# File 'lib/yle_tf/config/loader.rb', line 72
def plugins
Plugin.manager.registered
end
|
#task(message = nil) ⇒ Object
Helper to print debug information about the task and configuration after it
129
130
131
132
133
134
135
|
# File 'lib/yle_tf/config/loader.rb', line 129
def task(message = nil)
Logger.debug(message) if message
yield.tap do |config|
Logger.debug(" #{config.inspect}")
end
end
|