class Puppet::Settings::IniFile

@api private

Constants

DEFAULT_SECTION_NAME
Line
SectionLine
SettingLine

Public Class Methods

new(lines = []) click to toggle source
   # File lib/puppet/settings/ini_file.rb
28 def initialize(lines = [])
29   @lines = lines
30 end
parse(config_fh) click to toggle source
   # File lib/puppet/settings/ini_file.rb
12 def self.parse(config_fh)
13   config = new([DefaultSection.new])
14   config_fh.each_line do |line|
15     case line.chomp
16     when /^(\s*)\[([[:word:]]+)\](\s*)$/
17       config.append(SectionLine.new($1, $2, $3))
18     when /^(\s*)([[:word:]]+)(\s*=\s*)(.*?)(\s*)$/
19       config.append(SettingLine.new($1, $2, $3, $4, $5))
20     else
21       config.append(Line.new(line))
22     end
23   end
24 
25   config
26 end
update(config_fh) { |manipulator| ... } click to toggle source
   # File lib/puppet/settings/ini_file.rb
 5 def self.update(config_fh, &block)
 6   config = parse(config_fh)
 7   manipulator = Manipulator.new(config)
 8   yield manipulator
 9   config.write(config_fh)
10 end

Public Instance Methods

append(line) click to toggle source
   # File lib/puppet/settings/ini_file.rb
32 def append(line)
33   line.previous = @lines.last
34   @lines << line
35 end
delete(section, name) click to toggle source
   # File lib/puppet/settings/ini_file.rb
37 def delete(section, name)
38   delete_offset = @lines.index(setting(section, name))
39   next_offset = delete_offset + 1
40   if next_offset < @lines.length
41     @lines[next_offset].previous = @lines[delete_offset].previous
42   end
43   @lines.delete_at(delete_offset)
44 end
insert_after(line, new_line) click to toggle source
   # File lib/puppet/settings/ini_file.rb
46 def insert_after(line, new_line)
47   new_line.previous = line
48 
49   insertion_point = @lines.index(line)
50   @lines.insert(insertion_point + 1, new_line)
51   if @lines.length > insertion_point + 2
52     @lines[insertion_point + 2].previous = new_line
53   end
54 end
lines_in(section_name) click to toggle source
   # File lib/puppet/settings/ini_file.rb
70 def lines_in(section_name)
71   section_lines = []
72   current_section_name = DEFAULT_SECTION_NAME
73   @lines.each do |line|
74     if line.is_a?(SectionLine)
75       current_section_name = line.name
76     elsif current_section_name == section_name
77       section_lines << line
78     end
79   end
80 
81   section_lines
82 end
section_exists_with_default_section_name?() click to toggle source
   # File lib/puppet/settings/ini_file.rb
92 def section_exists_with_default_section_name?
93   section_lines.any? do |section|
94     !section.is_a?(DefaultSection) && section.name == DEFAULT_SECTION_NAME
95   end
96 end
section_line(name) click to toggle source
   # File lib/puppet/settings/ini_file.rb
60 def section_line(name)
61   section_lines.find { |section| section.name == name }
62 end
section_lines() click to toggle source
   # File lib/puppet/settings/ini_file.rb
56 def section_lines
57   @lines.select { |line| line.is_a?(SectionLine) }
58 end
set_default_section_write_sectionline(value) click to toggle source
    # File lib/puppet/settings/ini_file.rb
 98 def set_default_section_write_sectionline(value)
 99   index = @lines.find_index { |line| line.is_a?(DefaultSection) }
100   if index
101     @lines[index].write_sectionline = true
102   end
103 end
setting(section, name) click to toggle source
   # File lib/puppet/settings/ini_file.rb
64 def setting(section, name)
65   settings_in(lines_in(section)).find do |line|
66     line.name == name
67   end
68 end
settings_exist_in_default_section?() click to toggle source
   # File lib/puppet/settings/ini_file.rb
88 def settings_exist_in_default_section?
89   lines_in(DEFAULT_SECTION_NAME).any? { |line| line.is_a?(SettingLine) }
90 end
settings_in(lines) click to toggle source
   # File lib/puppet/settings/ini_file.rb
84 def settings_in(lines)
85   lines.select { |line| line.is_a?(SettingLine) }
86 end
write(fh) click to toggle source
    # File lib/puppet/settings/ini_file.rb
105 def write(fh)
106   # If no real section line for the default section exists, configure the
107   # DefaultSection object to write its section line. (DefaultSection objects
108   # don't write the section line unless explicitly configured to do so)
109   if settings_exist_in_default_section? && !section_exists_with_default_section_name?
110     set_default_section_write_sectionline(true)
111   end
112 
113   fh.truncate(0)
114   fh.rewind
115   @lines.each do |line|
116     line.write(fh)
117   end
118   fh.flush
119 end