class Dotini::Section

A single INI file section

Attributes

key_value_pairs[RW]
name[RW]

Public Class Methods

new(name) click to toggle source

Creates a new, empty section

# File lib/dotini/section.rb, line 9
def initialize(name)
  @name = name
  @key_value_pairs = []
end

Public Instance Methods

[](key) click to toggle source

Retrieves a KeyValuePair from the section, or creates one

# 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) click to toggle source

Sets a value for a key in this section

# File lib/dotini/section.rb, line 24
def []=(key, value)
  self[key].value = value
end
to_h() click to toggle source

Represents the section as a hash

# 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() click to toggle source

Represents the section as a string

# 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