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.

Calls superclass method IniParse::Lines::Line::new
# 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+)?$/i then Float(value)
    when /^true$/i                                      then true
    when /^false$/i                                     then false
    else                                                     value
  end
end

Private Instance Methods

line_contents() click to toggle source

returns an array to support multiple lines or a single one at once because of options key duplication

# File lib/iniparse/lines.rb, line 296
def line_contents
  if value.kind_of?(Array)
    value.map { |v, i| "#{key}#{@option_sep}#{v}" }
  else
    "#{key}#{@option_sep}#{value}"
  end
end