Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions lib/inifile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ def read( opts = {} )
# Returns this IniFile converted to a String.
def to_s
s = []
@ini.each do |section,hash|
hash = @ini.dup
default = hash.delete(@default)
default.each {|param,val| s << "#{param} #{@param} #{escape_value val}"}
hash.each do |section,hash|
s << "[#{section}]"
hash.each {|param,val| s << "#{param} #{@param} #{escape_value val}"}
s << ""
Expand Down Expand Up @@ -188,14 +191,16 @@ def merge!( other )
end

(other_keys - my_keys).each do |key|
@ini[key] = case other[key]
when Hash
other[key].dup
when nil
{}
else
raise Error, "cannot merge section #{key.inspect} - unsupported type: #{other[key].class.name}"
end
case other[key]
when Hash
@ini[key] = other[key].dup
when nil
@ini[key] = {}
when String
@ini[@default].merge!({key => other[key]})
else
raise Error, "cannot merge section #{key.inspect} - unsupported type: #{other[key].class.name}"
end
end

self
Expand Down
20 changes: 17 additions & 3 deletions test/test_inifile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ def test_initialize_from_string

def test_initialize_from_hash
hash = {
'value' => 'bat',
'section one' => {
'foo' => 'bar',
'baz' => 'buz'
Expand All @@ -296,9 +297,25 @@ def test_initialize_from_hash
assert_equal '7e6ff3', ini_file['colors']['perrywinkle']
assert_equal '4682b4', ini_file['colors']['steelblue']

assert_equal 'bat', ini_file['global']['value']

assert_empty ini_file['empty']
end

def test_to_s
hash = {
'one' => 'bar',
'two' => 'bat',
'section one' => {
'foo' => 'baz',
}
}

ini_file = IniFile.new(:content => hash)

assert_equal "one = bar\ntwo = bat\n[section one]\nfoo = baz\n", ini_file.to_s
end

def test_sections
expected = [
'section_one', 'section_two', 'section three',
Expand Down Expand Up @@ -460,9 +477,6 @@ def test_merge_invalid_hash
bad_hash = { 'section_one' => [1, 2, 3, 4] }
assert_raise(IniFile::Error) { @ini_file.merge(bad_hash) }

bad_hash = { 'foo' => 'bar' }
assert_raise(IniFile::Error) { @ini_file.merge(bad_hash) }

not_a_hash = [['section_one', ['foo','bar'], ['baz', 'buz']]]
assert_raise(IniFile::Error) { @ini_file.merge(not_a_hash) }

Expand Down