Class: Puppet::Settings::IniFile Private

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/settings/ini_file.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

API:

  • private

Defined Under Namespace

Modules: LineNumber Classes: DefaultSection, Line, Manipulator, SectionLine, SettingLine

Constant Summary collapse

DEFAULT_SECTION_NAME =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

API:

  • private

"main"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines = []) ⇒ IniFile

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of IniFile.

API:

  • private



28
29
30
# File 'lib/puppet/settings/ini_file.rb', line 28

def initialize(lines = [])
  @lines = lines
end

Class Method Details

.parse(config_fh) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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*)\[([[:word:]]+)\](\s*)$/
      config.append(SectionLine.new($1, $2, $3))
    when /^(\s*)([[:word:]]+)(\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| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Yields:

  • (manipulator)

API:

  • private



5
6
7
8
9
10
# 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

Instance Method Details

#append(line) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



32
33
34
35
# File 'lib/puppet/settings/ini_file.rb', line 32

def append(line)
  line.previous = @lines.last
  @lines << line
end

#insert_after(line, new_line) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



37
38
39
40
41
42
43
44
45
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



61
62
63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



51
52
53
# File 'lib/puppet/settings/ini_file.rb', line 51

def section_line(name)
  section_lines.find { |section| section.name == name }
end

#section_linesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



47
48
49
# File 'lib/puppet/settings/ini_file.rb', line 47

def section_lines
  @lines.select { |line| line.is_a?(SectionLine) }
end

#setting(section, name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



55
56
57
58
59
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



75
76
77
# File 'lib/puppet/settings/ini_file.rb', line 75

def settings_in(lines)
  lines.select { |line| line.is_a?(SettingLine) }
end

#write(fh) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



79
80
81
82
83
84
85
86
# 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