class IniParse::Lines::Section

Represents a section header in an INI document. Section headers consist of a string of characters wrapped in square brackets.

[section]
key=value
etc
...

Attributes

key[RW]
lines[R]

Public Class Methods

new(key, opts = {}) click to toggle source

Parameters

key<String>

The section name.

opts<Hash>

Extra options for the line.

Calls superclass method IniParse::Lines::Line::new
# File lib/iniparse/lines.rb, line 89
def initialize(key, opts = {})
  super(opts)
  @key   = key.to_s
  @lines = IniParse::OptionCollection.new
end
parse(line, opts) click to toggle source
# File lib/iniparse/lines.rb, line 95
def self.parse(line, opts)
  if m = @regex.match(line)
    [:section, m[1], opts]
  end
end

Public Instance Methods

[](key) click to toggle source

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
[]=(key, value) click to toggle source

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
delete(*args) click to toggle source

Deletes the option identified by key.

Returns the section.

# File lib/iniparse/lines.rb, line 178
def delete(*args)
  @lines.delete(*args)
  self
end
each(*args, &blk) click to toggle source

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.

Parameters

include_blank<Boolean>

Include blank/comment lines?

# File lib/iniparse/lines.rb, line 127
def each(*args, &blk)
  @lines.each(*args, &blk)
end
has_option?(key) click to toggle source

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
merge!(other) click to toggle source

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.

Parameters

other<IniParse::Section>

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
option(key) click to toggle source

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
to_ini() click to toggle source

Returns this line as a string as it would be represented in an INI document. Includes options, comments and blanks.

Calls superclass method IniParse::Lines::Line#to_ini
# 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

Private Instance Methods

line_contents() click to toggle source
# File lib/iniparse/lines.rb, line 218
def line_contents
  '[%s]' % key
end