class Puppet::Settings::IniFile

@api private

Constants

DEFAULT_SECTION_NAME
Line
SectionLine
SettingLine

Public Instance Methods

append(line) click to toggle source
# File lib/puppet/settings/ini_file.rb, line 32
def append(line)
  line.previous = @lines.last
  @lines << line
end
insert_after(line, new_line) click to toggle source
# File lib/puppet/settings/ini_file.rb, line 37
def insert_after(line, new_line)
  new_line.previous = line

  insertion_point = @lines.index(line)
  @lines.insert(insertion_point + 1, new_line)
  if @lines.length > insertion_point + 2
    @lines[insertion_point + 2].previous = new_line
  end
end
lines_in(section_name) click to toggle source
# File lib/puppet/settings/ini_file.rb, line 61
def lines_in(section_name)
  section_lines = []
  current_section_name = DEFAULT_SECTION_NAME
  @lines.each do |line|
    if line.is_a?(SectionLine)
      current_section_name = line.name
    elsif current_section_name == section_name
      section_lines << line
    end
  end

  section_lines
end
section_line(name) click to toggle source
# File lib/puppet/settings/ini_file.rb, line 51
def section_line(name)
  section_lines.find { |section| section.name == name }
end
section_lines() click to toggle source
# File lib/puppet/settings/ini_file.rb, line 47
def section_lines
  @lines.select { |line| line.is_a?(SectionLine) }
end
setting(section, name) click to toggle source
# File lib/puppet/settings/ini_file.rb, line 55
def setting(section, name)
  settings_in(lines_in(section)).find do |line|
    line.name == name
  end
end
settings_in(lines) click to toggle source
# File lib/puppet/settings/ini_file.rb, line 75
def settings_in(lines)
  lines.select { |line| line.is_a?(SettingLine) }
end
write(fh) click to toggle source
# File lib/puppet/settings/ini_file.rb, line 79
def write(fh)
  fh.truncate(0)
  fh.rewind
  @lines.each do |line|
    line.write(fh)
  end
  fh.flush
end

Public Class Methods

new(lines = []) click to toggle source
# File lib/puppet/settings/ini_file.rb, line 28
def initialize(lines = [])
  @lines = lines
end
parse(config_fh) click to toggle source
# File lib/puppet/settings/ini_file.rb, line 12
def self.parse(config_fh)
  config = new([DefaultSection.new])
  config_fh.each_line do |line|
    case line
    when /^(\s*)\[(\w+)\](\s*)$/
      config.append(SectionLine.new($1, $2, $3))
    when /^(\s*)(\w+)(\s*=\s*)(.*?)(\s*)$/
      config.append(SettingLine.new($1, $2, $3, $4, $5))
    else
      config.append(Line.new(line))
    end
  end

  config
end
update(config_fh) { |manipulator| ... } click to toggle source
# File lib/puppet/settings/ini_file.rb, line 5
def self.update(config_fh, &block)
  config = parse(config_fh)
  manipulator = Manipulator.new(config)
  yield manipulator
  config.write(config_fh)
end