5 # Some implementation details:
7 # * `name` will be set to the empty string for the 'global' section.
8 # * there will always be a 'global' section, with a `start_line` of 0,
9 # but if the file actually begins with a real section header on
10 # the first line, then the 'global' section will have an
11 # `end_line` of `nil`.
12 # * `start_line` and `end_line` will be set to `nil` for a new non-global
14 def initialize(name, start_line, end_line, settings, indentation)
16 @start_line = start_line
18 @existing_settings = settings.nil? ? {} : settings
19 @additional_settings = {}
20 @indentation = indentation
23 attr_reader :name, :start_line, :end_line, :additional_settings, :indentation
30 # a new section (global or named) will always have `end_line`
36 @existing_settings.keys | @additional_settings.keys
39 def get_value(setting_name)
40 @existing_settings[setting_name] || @additional_settings[setting_name]
43 def has_existing_setting?(setting_name)
44 @existing_settings.has_key?(setting_name)
47 def update_existing_setting(setting_name, value)
48 @existing_settings[setting_name] = value
51 def remove_existing_setting(setting_name)
52 if (@existing_settings.delete(setting_name))
54 @end_line = @end_line - 1
59 # This is a hacky method; it's basically called when we need to insert
60 # a new setting but we don't want it to appear at the very end of the
61 # section. Instead we hack it into the existing settings list and
62 # increment our end_line number--this assumes that the caller (`ini_file`)
63 # is doing some babysitting w/rt the other sections and the actual data
65 def insert_inline_setting(setting_name, value)
66 @existing_settings[setting_name] = value
68 @end_line = @end_line + 1
72 def set_additional_setting(setting_name, value)
73 @additional_settings[setting_name] = value
76 # Decrement the start and end line numbers for the section (if they are
77 # defined); this is intended to be called when a setting is removed
78 # from a section that comes before this section in the ini file.
79 def decrement_line_nums()
81 @start_line = @start_line - 1
84 @end_line = @end_line - 1
88 # Increment the start and end line numbers for the section (if they are
89 # defined); this is intended to be called when an inline setting is added
90 # to a section that comes before this section in the ini file.
91 def increment_line_nums()
93 @start_line = @start_line + 1
96 @end_line = @end_line + 1