Class: AppConfigLoader::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/app_config_loader/loader.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Loader



12
13
14
15
16
17
# File 'lib/app_config_loader/loader.rb', line 12

def initialize(config)
  @config = config
  @parser = Parser.new @config.use_domain

  raise 'Environement is not set in AppConfigLoader::Config' unless @config.env
end

Instance Method Details

#loadObject

Load app config entries from yml paths listed in the AppConfigLoader::Config’s ‘config_paths’ and ‘local_override’ properties



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/app_config_loader/loader.rb', line 21

def load
  cfg_map = ConfigMap.new

  # Reads the raw config entries from the file list configured
  # in the @config object
  expanded_paths.each do |path|
    begin
      raw_entries = parse_yml path
      raw_entries.each { |e| cfg_map << e if e.applicable? @config.env, @config.domain }
    rescue InvalidConfigKey => ex
      raise InvalidConfigFile, "config key error '#{path}': #{ex.message}"
    end
  end

  if (override_path = local_override_path)
    override_map = ConfigMap.new

    begin
      override_entries = parse_yml override_path
      override_entries.each { |e| override_map.add(e, true) if e.applicable? @config.env, @config.domain }

      # merges the override entries into the main config map
      cfg_map.merge override_map
    rescue InvalidConfigKey => ex
      raise InvalidConfigFile, "config key error '#{local_override_path}': #{ex.message}"
    rescue Errno::ENOENT
      # ignore file not exists error as local_override file is optional
    end
  end

  ConfigWithIndifferentAccess.new cfg_map
end