Class: Wip::ConfigLoader

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

Overview

Finds and parses wip.yml, searching parent directories when unset.

Constant Summary collapse

FILENAME =
'wip.yml'

Instance Method Summary collapse

Constructor Details

#initialize(start_dir: Dir.pwd, path: nil) ⇒ ConfigLoader

Returns a new instance of ConfigLoader.



11
12
13
14
# File 'lib/wip/config_loader.rb', line 11

def initialize(start_dir: Dir.pwd, path: nil)
  @start_dir = Pathname(start_dir).expand_path
  @path = path
end

Instance Method Details

#findObject



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

def find
  return Pathname(@path).expand_path if @path

  @start_dir.ascend do |directory|
    candidate = directory.join(FILENAME)
    return candidate if candidate.file?
  end
  nil
end

#loadObject



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

def load
  path = find
  raise ConfigError, "wip.yml was not found (searched from #{@start_dir} to the filesystem root)" unless path&.file?

  data = YAML.safe_load_file(path, permitted_classes: [], aliases: false) || {}
  Config.new(data, path)
rescue Psych::Exception => e
  raise ConfigError, "Could not parse #{path}: #{e.message}"
end