Class: Dotini::Section
- Inherits:
-
Object
- Object
- Dotini::Section
- Defined in:
- lib/dotini/section.rb
Overview
A single INI file section
Instance Attribute Summary collapse
-
#key_value_pairs ⇒ Object
Returns the value of attribute key_value_pairs.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Retrieves a KeyValuePair from the section, or creates one.
-
#[]=(key, value) ⇒ Object
Sets a value for a key in this section.
-
#initialize(name) ⇒ Section
constructor
Creates a new, empty section.
-
#to_h ⇒ Object
Represents the section as a hash.
-
#to_s ⇒ Object
Represents the section as a string.
Constructor Details
#initialize(name) ⇒ Section
Creates a new, empty section
9 10 11 12 |
# File 'lib/dotini/section.rb', line 9 def initialize(name) @name = name @key_value_pairs = [] end |
Instance Attribute Details
#key_value_pairs ⇒ Object
Returns the value of attribute key_value_pairs.
6 7 8 |
# File 'lib/dotini/section.rb', line 6 def key_value_pairs @key_value_pairs end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/dotini/section.rb', line 6 def name @name end |
Instance Method Details
#[](key) ⇒ Object
Retrieves a KeyValuePair from the section, or creates one
15 16 17 18 19 20 21 |
# File 'lib/dotini/section.rb', line 15 def [](key) key_value_pairs.find { |key_pair| key_pair.key == key } || KeyValuePair.new.tap do |pair| pair.key = key key_value_pairs << pair end end |
#[]=(key, value) ⇒ Object
Sets a value for a key in this section
24 25 26 |
# File 'lib/dotini/section.rb', line 24 def []=(key, value) self[key].value = value end |
#to_h ⇒ Object
Represents the section as a hash
29 30 31 32 33 34 35 36 37 |
# File 'lib/dotini/section.rb', line 29 def to_h {}.tap do |hash| key_value_pairs.each do |pair| next if pair.key.nil? hash[pair.key] = pair.value end end end |
#to_s ⇒ Object
Represents the section as a string
40 41 42 43 44 45 46 47 |
# File 'lib/dotini/section.rb', line 40 def to_s buffer = StringIO.new buffer << "[#{name}]\n" unless name.nil? key_value_pairs.each do |pair| buffer << pair.to_s end buffer.string end |