Class: IniParse::Lines::Option

Inherits:
Object
  • Object
show all
Includes:
Line
Defined in:
lib/iniparse/lines.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Line

#blank?, #comment, #has_comment?, #options, #to_ini

Constructor Details

#initialize(key, value, opts = {}) ⇒ Option

Parameters

key<String>

The option key.

value<String>

The value for this option.

opts<Hash>

Extra options for the line.



265
266
267
268
269
# 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

Instance Attribute Details

#keyObject

Returns the value of attribute key.



258
259
260
# File 'lib/iniparse/lines.rb', line 258

def key
  @key
end

#valueObject

Returns the value of attribute value.



258
259
260
# File 'lib/iniparse/lines.rb', line 258

def value
  @value
end

Class Method Details

.parse(line, opts) ⇒ Object



271
272
273
274
275
276
# 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) ⇒ Object

Attempts to typecast values.



279
280
281
282
283
284
285
286
287
288
# 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