Module: WhyClasses::ConfigLoader
- Defined in:
- lib/why_classes/config_loader.rb
Overview
Loads config/default.yml and deep-merges a project .why-classes.yml over it.
Constant Summary collapse
- DEFAULT_PATH =
File.("../../config/default.yml", __dir__)
- PROJECT_FILE =
".why-classes.yml"
Class Method Summary collapse
- .deep_merge(base, override) ⇒ Object
-
.discover(start_dir) ⇒ Object
Walk up from start_dir looking for .why-classes.yml.
- .load(explicit_path: nil, start_dir: Dir.pwd) ⇒ Object
- .read(path) ⇒ Object
Class Method Details
.deep_merge(base, override) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/why_classes/config_loader.rb', line 41 def deep_merge(base, override) base.merge(override) do |_key, base_val, over_val| if base_val.is_a?(Hash) && over_val.is_a?(Hash) deep_merge(base_val, over_val) else over_val end end end |
.discover(start_dir) ⇒ Object
Walk up from start_dir looking for .why-classes.yml.
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/why_classes/config_loader.rb', line 21 def discover(start_dir) dir = File.(start_dir) loop do candidate = File.join(dir, PROJECT_FILE) return candidate if File.file?(candidate) parent = File.dirname(dir) break if parent == dir dir = parent end nil end |
.load(explicit_path: nil, start_dir: Dir.pwd) ⇒ Object
14 15 16 17 18 |
# File 'lib/why_classes/config_loader.rb', line 14 def load(explicit_path: nil, start_dir: Dir.pwd) base = read(DEFAULT_PATH) project = explicit_path ? read(explicit_path) : read(discover(start_dir)) Configuration.new(deep_merge(base, project)) end |
.read(path) ⇒ Object
35 36 37 38 39 |
# File 'lib/why_classes/config_loader.rb', line 35 def read(path) return {} if path.nil? || !File.file?(path) YAML.safe_load_file(path) || {} end |