Module: Compath::ConfigLoader

Defined in:
lib/compath/config_loader.rb

Constant Summary collapse

SCAN_FALSE_REGEXP =
%r{(?<new_path>[^\*]*)(/\*\*)?/\*$}

Class Method Summary collapse

Class Method Details

.load(config) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/compath/config_loader.rb', line 7

def load(config)
  conf = YAML.load(config)

  guides = conf.map do |path, guide|
    if guide.is_a?(Hash)
      # symbolize keys
      options = guide.each_with_object({}) do |(key, value), hash|
        hash[key.to_sym] = value
      end
    else
      # nil or String
      options = {
        scan: true,
        desc: guide,
      }
    end

    # if path is end with `/` character, it represents `scan_children is false`.
    if m = SCAN_FALSE_REGEXP.match(path)
      path = m[:new_path]
      options[:scan] = false
    end

    # TODO: Bad interface...
    Guide.new(path, **options)
  end
end