Class: AtCoderFriends::ConfigLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/at_coder_friends/config_loader.rb

Overview

loads configuration file from the specified directory.

Constant Summary collapse

DOTFILE =
'.at_coder_friends.yml'

Class Method Summary collapse

Class Method Details

.find_file_upwards(filename, start_dir) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/at_coder_friends/config_loader.rb', line 17

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
  raise ConfigNotFoundError,
        "Configuration file not found: #{start_dir}"
end

.load_config(target_dir) ⇒ Object



12
13
14
15
# File 'lib/at_coder_friends/config_loader.rb', line 12

def load_config(target_dir)
  path = find_file_upwards(DOTFILE, target_dir)
  load_yaml(path)
end

.load_yaml(path) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/at_coder_friends/config_loader.rb', line 26

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