summaryrefslogtreecommitdiff
path: root/lib/puppet/util/ini_file.rb
diff options
context:
space:
mode:
authorChris Price <chris@puppetlabs.com>2012-10-20 23:14:39 -0700
committerChris Price <chris@puppetlabs.com>2012-10-20 23:14:39 -0700
commitf0d443fed02d870965613dd174793cc1816137a2 (patch)
tree610b78ab10ae9cf0f495f71f743433ab9f11ea57 /lib/puppet/util/ini_file.rb
parente517148f4a2ab309aeb8413095791b021574b417 (diff)
downloadpuppet-inifile-f0d443fed02d870965613dd174793cc1816137a2.tar.gz
puppet-inifile-f0d443fed02d870965613dd174793cc1816137a2.tar.bz2
Refactor to clarify implementation of `save`
The `save` method was previously relying on some really specific implementation details of the `section` class (when the start/end_line would be nil, etc.). This made the code a bit hard to follow. This commit introduces a few utility methods in the `section` class (`is_new_section?`, `is_global?`), and refactors the `save` method to use them... this makes the logic a little easier to follow and should hopefully make it easier to maintain.
Diffstat (limited to 'lib/puppet/util/ini_file.rb')
-rw-r--r--lib/puppet/util/ini_file.rb11
1 files changed, 8 insertions, 3 deletions
diff --git a/lib/puppet/util/ini_file.rb b/lib/puppet/util/ini_file.rb
index 6038b6d..0409db2 100644
--- a/lib/puppet/util/ini_file.rb
+++ b/lib/puppet/util/ini_file.rb
@@ -72,9 +72,12 @@ module Util
# We need a buffer to cache lines that are only whitespace
whitespace_buffer = []
- if section.start_line.nil?
+ if (section.is_new_section?) && (! section.is_global?)
fh.puts("\n[#{section.name}]")
- elsif ! section.end_line.nil?
+ end
+
+ if ! section.is_new_section?
+ # write all of the pre-existing settings
(section.start_line..section.end_line).each do |line_num|
line = lines[line_num]
@@ -94,6 +97,7 @@ module Util
end
end
+ # write new settings, if there are any
section.additional_settings.each_pair do |key, value|
fh.puts("#{' ' * (section.indentation || 0)}#{key}#{@key_val_separator}#{value}")
end
@@ -108,7 +112,8 @@ module Util
# and if there are more sections that come after this one,
# we'll write one blank line just so that there is a little
# whitespace between the sections.
- if (section.end_line.nil? &&
+ #if (section.end_line.nil? &&
+ if (section.is_new_section? &&
(section.additional_settings.length > 0) &&
(index < @section_names.length - 1))
fh.puts("")