Class: IniParse::Lines::Section
- Inherits:
-
Object
- Object
- IniParse::Lines::Section
- Includes:
- Enumerable, Line
- Defined in:
- lib/iniparse/lines.rb
Overview
Represents a section header in an INI document. Section headers consist of a string of characters wrapped in square brackets.
[section]
key=value
etc
...
Direct Known Subclasses
Instance Attribute Summary collapse
-
#key ⇒ Object
Returns the value of attribute key.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns the value of an option identified by
key
. -
#[]=(key, value) ⇒ Object
Adds a new option to this section, or updates an existing one.
-
#delete(*args) ⇒ Object
Deletes the option identified by
key
. -
#each(*args, &blk) ⇒ Object
Enumerates through each Option in this section.
-
#has_option?(key) ⇒ Boolean
Returns true if an option with the given
key
exists in this section. -
#initialize(key, opts = {}) ⇒ Section
constructor
Parameters key<String>:: The section name.
-
#merge!(other) ⇒ Object
Merges section
other
into this one. -
#option(key) ⇒ Object
Like [], except instead of returning just the option value, it returns the matching line instance.
-
#to_ini ⇒ Object
Returns this line as a string as it would be represented in an INI document.
Methods included from Line
#blank?, #comment, #has_comment?
Constructor Details
#initialize(key, opts = {}) ⇒ Section
Parameters
- key<String>
-
The section name.
- opts<Hash>
-
Extra options for the line.
78 79 80 81 82 |
# File 'lib/iniparse/lines.rb', line 78 def initialize(key, opts = {}) super(opts) @key = key.to_s @lines = IniParse::OptionCollection.new end |
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
69 70 71 |
# File 'lib/iniparse/lines.rb', line 69 def key @key end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
70 71 72 |
# File 'lib/iniparse/lines.rb', line 70 def lines @lines end |
Class Method Details
.parse(line, opts) ⇒ Object
84 85 86 87 88 |
# File 'lib/iniparse/lines.rb', line 84 def self.parse(line, opts) if m = @regex.match(line) [:section, m[1], opts] end end |
Instance Method Details
#[](key) ⇒ Object
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.
144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/iniparse/lines.rb', line 144 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) ⇒ Object
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.
134 135 136 |
# File 'lib/iniparse/lines.rb', line 134 def []=(key, value) @lines[key.to_s] = IniParse::Lines::Option.new(key.to_s, value) end |
#delete(*args) ⇒ Object
Deletes the option identified by key
.
Returns the section.
160 161 162 163 |
# File 'lib/iniparse/lines.rb', line 160 def delete(*args) @lines.delete(*args) self end |
#each(*args, &blk) ⇒ Object
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?
116 117 118 |
# File 'lib/iniparse/lines.rb', line 116 def each(*args, &blk) @lines.each(*args, &blk) end |
#has_option?(key) ⇒ Boolean
Returns true if an option with the given key
exists in this section.
175 176 177 |
# File 'lib/iniparse/lines.rb', line 175 def has_option?(key) @lines.has_key?(key.to_s) end |
#merge!(other) ⇒ Object
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.
186 187 188 189 190 191 192 193 194 |
# File 'lib/iniparse/lines.rb', line 186 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) ⇒ Object
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.
170 171 172 |
# File 'lib/iniparse/lines.rb', line 170 def option(key) @lines[key.to_s] end |
#to_ini ⇒ Object
Returns this line as a string as it would be represented in an INI document. Includes options, comments and blanks.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/iniparse/lines.rb', line 92 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 |