Module: IniParse::LineCollection

Includes:
Enumerable
Included in:
OptionCollection, SectionCollection
Defined in:
lib/iniparse/line_collection.rb

Overview

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#[]=).

Instance Method Summary collapse

Instance Method Details

#<<(line) ⇒ Object Also known as: push

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.



46
47
48
# File 'lib/iniparse/line_collection.rb', line 46

def <<(line)
  line.blank? ? (@lines << line) : (self[line.key] = line) ; self
end

#[](key) ⇒ Object

Retrive a value identified by key.



21
22
23
# File 'lib/iniparse/line_collection.rb', line 21

def [](key)
  has_key?(key) ? @lines[ @indicies[key] ] : nil
end

#[]=(key, value) ⇒ Object

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.



30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Object

Removes the value identified by key.



70
71
72
73
74
75
76
77
78
# 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) ⇒ Object

Enumerates through the collection.

By default #each does not yield blank and comment lines.

Parameters

include_blank<Boolean>

Include blank/comment lines?



59
60
61
62
63
64
65
66
67
# 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) ⇒ Boolean

Returns whether key is in the collection.

Returns:

  • (Boolean)


81
82
83
# File 'lib/iniparse/line_collection.rb', line 81

def has_key?(*args)
  @indicies.has_key?(*args)
end

#initializeObject



15
16
17
18
# File 'lib/iniparse/line_collection.rb', line 15

def initialize
  @lines    = []
  @indicies = {}
end

#keysObject

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



87
88
89
# File 'lib/iniparse/line_collection.rb', line 87

def keys
  map { |line| line.key }
end

#to_aObject

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



92
93
94
# File 'lib/iniparse/line_collection.rb', line 92

def to_a
  @lines.dup
end

#to_hashObject Also known as: to_h

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



98
99
100
# File 'lib/iniparse/line_collection.rb', line 98

def to_hash
  Hash[ *(map { |line| [line.key, line] }).flatten ]
end