Class: IniParse::OptionCollection

Inherits:
Object
  • Object
show all
Includes:
LineCollection
Defined in:
lib/iniparse/line_collection.rb

Overview

A implementation of LineCollection used for storing (mostly) Option instances contained within a Section.

Whenever OptionCollection encounters an Option key already held in the collection, it treats it as a duplicate. This means that instead of overwriting the existing value, the value is changed to an array containing the previous and the new Option instances.

Instance Method Summary collapse

Methods included from LineCollection

#[], #[]=, #delete, #has_key?, #initialize, #to_a, #to_hash

Instance Method Details

#<<(line) ⇒ Object

Appends a line to the collection.

If you push an Option with a key already represented in the collection, the previous Option will not be overwritten, but treated as a duplicate.

Parameters

line<IniParse::LineType::Line>

The line to be added to this section.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/iniparse/line_collection.rb', line 151

def <<(line)
  if line.kind_of?(IniParse::Lines::Section)
    raise IniParse::LineNotAllowed,
      "You can't add a Section to an OptionCollection."
  end

  if line.blank? || (! has_key?(line.key))
    super # Adding a new option, comment or blank line.
  else
    self[line.key] = [self[line.key], line].flatten
  end

  self
end

#each(*args) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/iniparse/line_collection.rb', line 166

def each(*args)
  return enum_for(:each, *args) unless block_given?

  super(*args) do |value|
    if value.is_a?(Array)
      value.each { |item| yield(item) }
    else
      yield value
    end
  end
end

#keysObject

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



180
181
182
# File 'lib/iniparse/line_collection.rb', line 180

def keys
  map(&:key).uniq
end