Class: Falcore::Config::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/falcore/config.rb

Constant Summary collapse

CONFIG_SECTION =
/\[(.+)\]/.freeze
CONFIG_OPTION =
/[[:space:]]{2,}(.+) = (.+)/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(contents) ⇒ Parser

Returns a new instance of Parser.



39
40
41
42
# File 'lib/falcore/config.rb', line 39

def initialize(contents)
  @contents = contents
  @result   = Config.new
end

Instance Method Details

#parseObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/falcore/config.rb', line 44

def parse
  @contents.split(/\r?\n/).each do |line|
    next if line.strip.empty?
    next if line.strip.start_with?('#')

    if line =~ CONFIG_SECTION
      @current_key = $1
    elsif line =~ CONFIG_OPTION
      match = line.match(CONFIG_OPTION)
      key   = match[1]
      value = match[2]

      # Make sure we are keyed
      @result[@current_key] ||= Config.new
      @result[@current_key][key] = coerce(value)
    else
      raise "Could not parse line '#{line}'"
    end
  end

  @result
end