class IniParse::Lines::Option

Represents probably the most common type of line in an INI document: an option. Consists of a key and value, usually separated with an =.

key = value

Attributes

key[RW]
value[RW]

Public Class Methods

new(key, value, opts = {}) click to toggle source

Parameters

key<String>

The option key.

value<String>

The value for this option.

opts<Hash>

Extra options for the line.

# File lib/iniparse/lines.rb, line 265
def initialize(key, value, opts = {})
  super(opts)
  @key, @value = key.to_s, value
  @option_sep = opts.fetch(:option_sep, ' = ')
end
parse(line, opts) click to toggle source
# File lib/iniparse/lines.rb, line 271
def self.parse(line, opts)
  if m = @regex.match(line)
    opts[:option_sep] = m[2]
    [:option, m[1].strip, typecast(m[3].strip), opts]
  end
end
typecast(value) click to toggle source

Attempts to typecast values.

# File lib/iniparse/lines.rb, line 279
def self.typecast(value)
  case value
    when /^\s*$/                                        then nil
    when /^-?(?:\d|[1-9]\d+)$/                          then Integer(value)
    when /^-?(?:\d|[1-9]\d+)(?:\.\d+)?(?:e[+-]?\d+)?$/ then Float(value)
    when /^true$/                                      then true
    when /^false$/                                     then false
    else                                                     value
  end
end