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?, #options
Constructor Details
#initialize(key, opts = {}) ⇒ Section
Parameters
- key<String>
-
The section name.
- opts<Hash>
-
Extra options for the line.
89 90 91 92 93 |
# File 'lib/iniparse/lines.rb', line 89 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.
80 81 82 |
# File 'lib/iniparse/lines.rb', line 80 def key @key end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
81 82 83 |
# File 'lib/iniparse/lines.rb', line 81 def lines @lines end |
Class Method Details
.parse(line, opts) ⇒ Object
95 96 97 98 99 |
# File 'lib/iniparse/lines.rb', line 95 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.
162 163 164 165 166 167 168 169 170 171 172 |
# 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) ⇒ 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.
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/iniparse/lines.rb', line 145 def []=(key, value) line = @lines[key.to_s] opts = {} if line.kind_of?(Array) opts = line.first. elsif line.respond_to? :options opts = line. end @lines[key.to_s] = IniParse::Lines::Option.new(key.to_s, value, opts) end |
#delete(*args) ⇒ Object
Deletes the option identified by key
.
Returns the section.
178 179 180 181 |
# File 'lib/iniparse/lines.rb', line 178 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?
127 128 129 |
# File 'lib/iniparse/lines.rb', line 127 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.
193 194 195 |
# File 'lib/iniparse/lines.rb', line 193 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.
204 205 206 207 208 209 210 211 212 |
# 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) ⇒ 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.
188 189 190 |
# File 'lib/iniparse/lines.rb', line 188 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.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# 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 |