summaryrefslogtreecommitdiff
path: root/lib/puppet/util/ini_file.rb
diff options
context:
space:
mode:
authorChris Price <chris@puppetlabs.com>2012-10-17 13:27:28 -0700
committerChris Price <chris@puppetlabs.com>2012-10-17 13:27:28 -0700
commitcda30a6ab9b6fa673ead091ab0b130cbc6e75dbc (patch)
tree17a5bb35c8861a745786105eeaee2e7d8479e711 /lib/puppet/util/ini_file.rb
parent1106d70e881028ee2dfa476307444780c9c4cbaa (diff)
downloadpuppet-inifile-cda30a6ab9b6fa673ead091ab0b130cbc6e75dbc.tar.gz
puppet-inifile-cda30a6ab9b6fa673ead091ab0b130cbc6e75dbc.tar.bz2
Minor tweaks to handling of removing settings
This commit makes some minor changes to how we handle removing settings. In particular, it updates all of the line numbers of the various 'section' objects to correspond to the new state of the world based on the removal of a line that appeared before them. Also adds one more test related to setting removal.
Diffstat (limited to 'lib/puppet/util/ini_file.rb')
-rw-r--r--lib/puppet/util/ini_file.rb26
1 files changed, 23 insertions, 3 deletions
diff --git a/lib/puppet/util/ini_file.rb b/lib/puppet/util/ini_file.rb
index 107924d..52ad32c 100644
--- a/lib/puppet/util/ini_file.rb
+++ b/lib/puppet/util/ini_file.rb
@@ -45,8 +45,19 @@ module Util
def remove_setting(section_name, setting)
section = @sections_hash[section_name]
if (section.has_existing_setting?(setting))
+ # If the setting is found, we have some work to do.
+ # First, we remove the line from our array of lines:
remove_line(section, setting)
+
+ # Then, we need to tell the setting object to remove
+ # the setting from its state:
section.remove_existing_setting(setting)
+
+ # Finally, we need to update all of the start/end line
+ # numbers for all of the sections *after* the one that
+ # was modified.
+ section_index = @section_names.index(section_name)
+ decrement_section_line_numbers(section_index + 1)
end
end
@@ -61,9 +72,7 @@ module Util
fh.puts("\n[#{section.name}]")
elsif ! section.end_line.nil?
(section.start_line..section.end_line).each do |line_num|
- if lines[line_num]
- fh.puts(lines[line_num])
- end
+ fh.puts(lines[line_num])
end
end
@@ -153,6 +162,17 @@ module Util
File.readlines(path)
end
+
+ # Utility method; given a section index (index into the @section_names
+ # array), decrement the start/end line numbers for that section and all
+ # all of the other sections that appear *after* the specified section.
+ def decrement_section_line_numbers(section_index)
+ @section_names[section_index..(@section_names.length - 1)].each do |name|
+ section = @sections_hash[name]
+ section.decrement_line_nums
+ end
+ end
+
end
end
end