Class: AtCoderFriends::ConfigLoader
- Inherits:
-
Object
- Object
- AtCoderFriends::ConfigLoader
- 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'
- ACF_HOME =
File.realpath(File.join(__dir__, '..', '..'))
- DEFAULT_FILE =
File.join(ACF_HOME, 'config', 'default.yml')
Class Method Summary collapse
- .config_file_for(target_dir) ⇒ Object
- .default_config ⇒ Object
- .default_file ⇒ Object
- .dotfile ⇒ Object
- .find_file_upwards(filename, start_dir) ⇒ Object
- .find_project_dotfile(target_dir) ⇒ Object
- .load_config(ctx) ⇒ Object
- .load_yaml(path) ⇒ Object
- .merge(base_hash, derived_hash) ⇒ Object
- .merge_with_default(config) ⇒ Object
Class Method Details
.config_file_for(target_dir) ⇒ Object
30 31 32 |
# 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 ⇒ Object
49 50 51 |
# File 'lib/at_coder_friends/config_loader.rb', line 49 def default_config load_yaml(DEFAULT_FILE) end |
.default_file ⇒ Object
18 19 20 |
# File 'lib/at_coder_friends/config_loader.rb', line 18 def default_file DEFAULT_FILE end |
.dotfile ⇒ Object
14 15 16 |
# File 'lib/at_coder_friends/config_loader.rb', line 14 def dotfile DOTFILE end |
.find_file_upwards(filename, start_dir) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/at_coder_friends/config_loader.rb', line 38 def find_file_upwards(filename, start_dir) Pathname.new(start_dir)..ascend do |dir| file = dir + filename return file.to_s if file.exist? end end |
.find_project_dotfile(target_dir) ⇒ Object
34 35 36 |
# 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) ⇒ Object
22 23 24 25 26 27 28 |
# 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) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/at_coder_friends/config_loader.rb', line 63 def load_yaml(path) YAML.load_file(path) || {} rescue Errno::ENOENT raise ConfigNotFoundError, "Configuration file not found: #{path}" end |
.merge(base_hash, derived_hash) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/at_coder_friends/config_loader.rb', line 53 def merge(base_hash, derived_hash) 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 end |
.merge_with_default(config) ⇒ Object
45 46 47 |
# File 'lib/at_coder_friends/config_loader.rb', line 45 def merge_with_default(config) merge(default_config, config) end |