module IniParse::LineCollection

Represents a collection of lines in an INI document.

LineCollection acts a bit like an Array/Hash hybrid, allowing arbitrary lines to be added to the collection, but also indexes the keys of Section and Option lines to enable O(1) lookup via LineCollection#[].

The lines instances are stored in an array, +@lines+, while the index of each Section/Option is held in a Hash, +@indicies+, keyed with the Section/Option#key value (see LineCollection#[]=).

Public Class Methods

new() click to toggle source
# File lib/iniparse/line_collection.rb, line 15
def initialize
  @lines    = []
  @indicies = {}
end

Public Instance Methods

<<(line) click to toggle source

Appends a line to the collection.

Note that if you pass a line with a key already represented in the collection, the old item will be replaced.

# File lib/iniparse/line_collection.rb, line 46
def <<(line)
  line.blank? ? (@lines << line) : (self[line.key] = line) ; self
end
Also aliased as: push
[](key) click to toggle source

Retrive a value identified by key.

# File lib/iniparse/line_collection.rb, line 21
def [](key)
  has_key?(key) ? @lines[ @indicies[key] ] : nil
end
[]=(key, value) click to toggle source

Set a value identified by key.

If a value with the given key already exists, the value will be replaced with the new one, with the new value taking the position of the old.

# File lib/iniparse/line_collection.rb, line 30
def []=(key, value)
  key = key.to_s

  if has_key?(key)
    @lines[ @indicies[key] ] = value
  else
    @lines << value
    @indicies[key] = @lines.length - 1
  end
end
delete(key) click to toggle source

Removes the value identified by key.

# File lib/iniparse/line_collection.rb, line 70
def delete(key)
  key = key.key if key.respond_to?(:key)

  unless (idx = @indicies[key]).nil?
    @indicies.delete(key)
    @indicies.each { |k,v| @indicies[k] = v -= 1 if v > idx }
    @lines.delete_at(idx)
  end
end
each(include_blank = false) { |line| ... } click to toggle source

Enumerates through the collection.

By default each does not yield blank and comment lines.

Parameters

include_blank<Boolean>

Include blank/comment lines?

# File lib/iniparse/line_collection.rb, line 59
def each(include_blank = false)
  return enum_for(:each, include_blank) unless block_given?

  @lines.each do |line|
    if include_blank || ! (line.is_a?(Array) ? line.empty? : line.blank?)
      yield(line)
    end
  end
end
has_key?(*args) click to toggle source

Returns whether key is in the collection.

# File lib/iniparse/line_collection.rb, line 81
def has_key?(*args)
  @indicies.has_key?(*args)
end
keys() click to toggle source

Return an array containing the keys for the lines added to this collection.

# File lib/iniparse/line_collection.rb, line 87
def keys
  map { |line| line.key }
end
push(line)
Alias for: <<
to_a() click to toggle source

Returns this collection as an array. Includes blank and comment lines.

# File lib/iniparse/line_collection.rb, line 92
def to_a
  @lines.dup
end
to_h()
Alias for: to_hash
to_hash() click to toggle source

Returns this collection as a hash. Does not contain blank and comment lines.

# File lib/iniparse/line_collection.rb, line 98
def to_hash
  Hash[ *(map { |line| [line.key, line] }).flatten ]
end
Also aliased as: to_h