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



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

def opts
  @opts
end

#pathsObject

Returns the value of attribute paths

Returns:

  • (Object)

    the current value of paths



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

def paths
  @paths
end

Instance Method Details

#configObject



29
30
31
# File 'lib/dpl/helper/config_file.rb', line 29

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

#parse(path) ⇒ Object



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

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

#strip_prefix(str) ⇒ Object



40
41
42
# File 'lib/dpl/helper/config_file.rb', line 40

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