class Falcore::Config::Parser

Constants

CONFIG_OPTION
CONFIG_SECTION

Public Class Methods

new(contents) click to toggle source
# File lib/falcore/config.rb, line 39
def initialize(contents)
  @contents = contents
  @result   = Config.new
end

Public Instance Methods

parse() click to toggle source
# 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

Private Instance Methods

coerce(value) click to toggle source

Coerce the value into it’s appropiate Ruby type.

@param [Object] value @return [Object]

the coerced value
# File lib/falcore/config.rb, line 76
def coerce(value)
  case value
  when /^[[:digit:]]+$/
    value.to_i
  when /^[[:digit:]]+\.[[:digit:]]+$/
    value.to_f
  else
    value
  end
end