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

#comment, #has_comment?, #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.



210
211
212
213
# File 'lib/iniparse/lines.rb', line 210

def initialize(key, value, opts = {})
  super(opts)
  @key, @value = key.to_s, value
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



203
204
205
# File 'lib/iniparse/lines.rb', line 203

def key
  @key
end

#valueObject

Returns the value of attribute value.



203
204
205
# File 'lib/iniparse/lines.rb', line 203

def value
  @value
end

Class Method Details

.parse(line, opts) ⇒ Object



215
216
217
218
219
# File 'lib/iniparse/lines.rb', line 215

def self.parse(line, opts)
  if m = @regex.match(line)
    [:option, m[1].strip, typecast(m[2].strip), opts]
  end
end

.typecast(value) ⇒ Object

Attempts to typecast values.



222
223
224
225
226
227
228
229
230
231
# File 'lib/iniparse/lines.rb', line 222

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