Represents a section header in an INI document. Section headers consist of a string of characters wrapped in square brackets.
[section] key=value etc ...
Returns the value of an option identified by key.
Returns nil if there is no corresponding option. If the key provided matches a set of duplicate options, an array will be returned containing the value of each option.
# File lib/iniparse/lines.rb, line 162 def [](key) key = key.to_s if @lines.has_key?(key) if (match = @lines[key]).kind_of?(Array) match.map { |line| line.value } else match.value end end end
Adds a new option to this section, or updates an existing one.
Note that +[]=+ has no knowledge of duplicate options and will happily overwrite duplicate options with your new value.
section['an_option'] # => ['duplicate one', 'duplicate two', ...] section['an_option'] = 'new value' section['an_option] # => 'new value'
If you do not wish to overwrite duplicates, but wish instead for your new
option to be considered a duplicate, use add_option instead.
# File lib/iniparse/lines.rb, line 145 def []=(key, value) line = @lines[key.to_s] opts = {} if line.kind_of?(Array) opts = line.first.options elsif line.respond_to? :options opts = line.options end @lines[key.to_s] = IniParse::Lines::Option.new(key.to_s, value, opts) end
Deletes the option identified by key.
Returns the section.
# File lib/iniparse/lines.rb, line 178 def delete(*args) @lines.delete(*args) self end
Enumerates through each Option in this section.
Does not yield blank and comment lines by default; if you want all lines to be yielded, pass true.
Include blank/comment lines?
# File lib/iniparse/lines.rb, line 127 def each(*args, &blk) @lines.each(*args, &blk) end
Returns true if an option with the given key exists in this
section.
# File lib/iniparse/lines.rb, line 193 def has_option?(key) @lines.has_key?(key.to_s) end
Merges section other into this one. If the section being
merged into this one contains options with the same key, they will be
handled as duplicates.
The section to merge into this one.
# File lib/iniparse/lines.rb, line 204 def merge!(other) other.lines.each(true) do |line| if line.kind_of?(Array) line.each { |duplicate| @lines << duplicate } else @lines << line end end end
Like [], except instead of returning just the option value, it returns the matching line instance.
Will return an array of lines if the key matches a set of duplicates.
# File lib/iniparse/lines.rb, line 188 def option(key) @lines[key.to_s] end
Returns this line as a string as it would be represented in an INI document. Includes options, comments and blanks.
# File lib/iniparse/lines.rb, line 103 def to_ini coll = lines.to_a if coll.any? [*super,coll.to_a.map do |line| if line.kind_of?(Array) line.map { |dup_line| dup_line.to_ini }.join($/) else line.to_ini end end].join($/) else super end end
The section name.
Extra options for the line.
# File lib/iniparse/lines.rb, line 89 def initialize(key, opts = {}) super(opts) @key = key.to_s @lines = IniParse::OptionCollection.new end
# File lib/iniparse/lines.rb, line 95 def self.parse(line, opts) if m = @regex.match(line) [:section, m[1], opts] end end