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

Public Instance Methods

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