Class: Dpl::ConfigFile::ConfigFiles

Inherits:
Struct
  • Object
show all
Defined in:
lib/dpl/helper/config_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optsObject

Returns the value of attribute opts

Returns:

  • (Object)

    the current value of opts



26
27
28
# File 'lib/dpl/helper/config_file.rb', line 26

def opts
  @opts
end

#pathsObject

Returns the value of attribute paths

Returns:

  • (Object)

    the current value of paths



26
27
28
# File 'lib/dpl/helper/config_file.rb', line 26

def paths
  @paths
end

Instance Method Details

#configObject



27
28
29
# File 'lib/dpl/helper/config_file.rb', line 27

def config
  paths.map { |path| parse(path) }.inject(&:merge) || {}
end

#parse(path) ⇒ Object



31
32
33
34
35
36
# File 'lib/dpl/helper/config_file.rb', line 31

def parse(path)
  str = File.exists?(path) ? File.read(path) : ''
  opts = str.lines.select { |line| line.include?('=') }.map(&:strip)
  opts = opts.map { |pair| pair.split('=', 2) }.to_h
  opts.map { |key, value| [strip_prefix(key).to_sym, value] }.to_h
end

#strip_prefix(str) ⇒ Object



38
39
40
# File 'lib/dpl/helper/config_file.rb', line 38

def strip_prefix(str)
  opts[:prefix] ? str.sub(/^#{opts[:prefix]}[\-_]?/, '') : str
end