class YleTf::Config::Loader

Attributes

module_dir[R]
tf_env[R]

Public Class Methods

new(opts) click to toggle source
# 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

Public Instance Methods

config_context() click to toggle source
# File lib/yle_tf/config/loader.rb, line 108
def config_context
  @config_context ||= load_config_context
end
config_files() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/yle_tf/config/loader.rb, line 55
def evaluate_configuration_strings(config)
  task('Evaluating the configuration strings') { eval_config(config) }
end
load() click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/yle_tf/config/loader.rb, line 72
def plugins
  Plugin.manager.registered
end
task(message = nil) { || ... } click to toggle source

Helper to print debug information about the task and configuration after it

# 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