class AppConfigLoader::Loader

Public Class Methods

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

Public Instance Methods

load() click to toggle source

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

# 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

Private Instance Methods

expanded_paths() click to toggle source

Expands the list of path entries into absolute paths for each matching files

@return [Array<String>] absolute paths of all entries in the @config.config_paths

# File lib/app_config_loader/loader.rb, line 63
def expanded_paths
  @config.config_paths.reduce(Set.new) do |memo, path|
    if File.directory? path
      Dir.chdir(path) do
        # add all files (recursively) within the directory
        memo += Dir.glob('**/*').map { |f| File.absolute_path f, path }
      end
    elsif File.exists? path
      # add the file to the list if it exists
      memo << File.absolute_path(path)
    else
      # try to glob the entries and add the result to the list
      memo += Dir.glob(path).map { |f| File.absolute_path f, path }
    end

    memo
  end
end
local_override_path() click to toggle source
# File lib/app_config_loader/loader.rb, line 82
def local_override_path
  @config.local_overrides ? File.absolute_path(@config.local_overrides) : nil
end
parse_yml(path) click to toggle source
# File lib/app_config_loader/loader.rb, line 56
def parse_yml(path)
  @parser.parse ::ERB.new(File.read(path)).result
end