Module: Config::Parser

Defined in:
lib/config/parser.rb

Class Method Summary collapse

Class Method Details

.convert(value) ⇒ Object



31
32
33
34
35
36
# File 'lib/config/parser.rb', line 31

def convert(value)
  return value.split(",") unless value.match(/(,[^\s])/).nil?
  return true unless value.match(/^yes$|^true$|^1{1}[^\w]/).nil?
  return false unless value.match(/^no$|^false$|^0{1}[^\w]/).nil?
  return value
end

.settings(file_path, overrides) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/config/parser.rb', line 4

def settings(file_path, overrides)
  config_hash = { }
  overrides = overrides.map { |o| o.to_sym }
  begin
    File.open(file_path).each do |line|
      group = line.match(/^\[(\S+)\]/)
      kv = line.match(/(\w+)\s*=\s*["|']*([^"'\r\n]*)["|']*/)
      override = line.match(/(\w+)<(\w+)>\s*=\s*["|']*([^"'\r\n]*)["|']*/)

      # TODO: Should do a check for nils on matches and throw exception = malformed
      if !group.nil?
        config_hash[group[1]] = {}
      elsif !kv.nil?
        config_hash[config_hash.keys.last][kv[1]] = convert(kv[2])
      elsif !override.nil?
        if overrides.include? override[2].to_sym
          config_hash[config_hash.keys.last][override[1].to_sym] = convert(override[3])
        end
      end
        config_hash[config_hash.keys.last].symbolize_keys!
    end
    config_hash.symbolize_keys!
  rescue Exception => e
    puts "Caught exception: #{e}, unable to load configuration file"
  end
end