Class: IniParse::Lines::Option
- Inherits:
-
Object
- Object
- IniParse::Lines::Option
- 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
-
#key ⇒ Object
Returns the value of attribute key.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
- .parse(line, opts) ⇒ Object
-
.typecast(value) ⇒ Object
Attempts to typecast values.
Instance Method Summary collapse
-
#initialize(key, value, opts = {}) ⇒ Option
constructor
Parameters key<String>:: The option key.
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
#key ⇒ Object
Returns the value of attribute key.
258 259 260 |
# File 'lib/iniparse/lines.rb', line 258 def key @key end |
#value ⇒ Object
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 |