class AtCoderFriends::ConfigLoader

loads configuration file from the specified directory.

Constants

ACF_HOME
DEFAULT_FILE
DOTFILE

Public Class Methods

config_file_for(target_dir) click to toggle source
# File lib/at_coder_friends/config_loader.rb, line 30
def config_file_for(target_dir)
  find_project_dotfile(target_dir) || default_file
end
default_config() click to toggle source
# File lib/at_coder_friends/config_loader.rb, line 49
def default_config
  load_yaml(DEFAULT_FILE)
end
default_file() click to toggle source
# File lib/at_coder_friends/config_loader.rb, line 18
def default_file
  DEFAULT_FILE
end
dotfile() click to toggle source
# File lib/at_coder_friends/config_loader.rb, line 14
def dotfile
  DOTFILE
end
find_file_upwards(filename, start_dir) click to toggle source
# File lib/at_coder_friends/config_loader.rb, line 38
def find_file_upwards(filename, start_dir)
  Pathname.new(start_dir).expand_path.ascend do |dir|
    file = dir + filename
    return file.to_s if file.exist?
  end
end
find_project_dotfile(target_dir) click to toggle source
# File lib/at_coder_friends/config_loader.rb, line 34
def find_project_dotfile(target_dir)
  find_file_upwards(dotfile, target_dir)
end
load_config(ctx) click to toggle source
# File lib/at_coder_friends/config_loader.rb, line 22
def load_config(ctx)
  path = config_file_for(ctx.path)
  config = load_yaml(path)
  return config if path == default_file

  merge_with_default(config)
end
load_yaml(path) click to toggle source
# File lib/at_coder_friends/config_loader.rb, line 64
def load_yaml(path)
  yaml = IO.read(path, encoding: Encoding::UTF_8)
  YAML.safe_load(yaml, [], [], false, path) || {}
rescue Errno::ENOENT
  raise ConfigNotFoundError,
        "Configuration file not found: #{path}"
end
merge(base_hash, derived_hash) click to toggle source
# File lib/at_coder_friends/config_loader.rb, line 53
def merge(base_hash, derived_hash)
  res = base_hash.merge(derived_hash) do |_, base_val, derived_val|
    if base_val.is_a?(Hash) && derived_val.is_a?(Hash)
      merge(base_val, derived_val)
    else
      derived_val
    end
  end
  res
end
merge_with_default(config) click to toggle source
# File lib/at_coder_friends/config_loader.rb, line 45
def merge_with_default(config)
  merge(default_config, config)
end