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



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

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/puppet/settings/ini_file.rb', line 13

def self.parse(config_fh)
  config = new([DefaultSection.new])
  config_fh.each_line do |line|
    case line.chomp
    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



6
7
8
9
10
11
# File 'lib/puppet/settings/ini_file.rb', line 6

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



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

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

#delete(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



38
39
40
41
42
43
44
45
# File 'lib/puppet/settings/ini_file.rb', line 38

def delete(section, name)
  delete_offset = @lines.index(setting(section, name))
  next_offset = delete_offset + 1
  if next_offset < @lines.length
    @lines[next_offset].previous = @lines[delete_offset].previous
  end
  @lines.delete_at(delete_offset)
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



47
48
49
50
51
52
53
54
55
# File 'lib/puppet/settings/ini_file.rb', line 47

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



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/puppet/settings/ini_file.rb', line 71

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_exists_with_default_section_name? ⇒ Boolean

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:

API:

  • private



93
94
95
96
97
# File 'lib/puppet/settings/ini_file.rb', line 93

def section_exists_with_default_section_name?
  section_lines.any? do |section|
    !section.is_a?(DefaultSection) && section.name == DEFAULT_SECTION_NAME
  end
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



61
62
63
# File 'lib/puppet/settings/ini_file.rb', line 61

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

#section_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



57
58
59
# File 'lib/puppet/settings/ini_file.rb', line 57

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

#set_default_section_write_sectionline(value) ⇒ 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



99
100
101
102
103
104
# File 'lib/puppet/settings/ini_file.rb', line 99

def set_default_section_write_sectionline(value)
  index = @lines.find_index { |line| line.is_a?(DefaultSection) }
  if index
    @lines[index].write_sectionline = true
  end
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



65
66
67
68
69
# File 'lib/puppet/settings/ini_file.rb', line 65

def setting(section, name)
  settings_in(lines_in(section)).find do |line|
    line.name == name
  end
end

#settings_exist_in_default_section? ⇒ Boolean

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:

API:

  • private



89
90
91
# File 'lib/puppet/settings/ini_file.rb', line 89

def settings_exist_in_default_section?
  lines_in(DEFAULT_SECTION_NAME).any? { |line| line.is_a?(SettingLine) }
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



85
86
87
# File 'lib/puppet/settings/ini_file.rb', line 85

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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/puppet/settings/ini_file.rb', line 106

def write(fh)
  # If no real section line for the default section exists, configure the
  # DefaultSection object to write its section line. (DefaultSection objects
  # don't write the section line unless explicitly configured to do so)
  if settings_exist_in_default_section? && !section_exists_with_default_section_name?
    set_default_section_write_sectionline(true)
  end

  fh.truncate(0)
  fh.rewind
  @lines.each do |line|
    line.write(fh)
  end
  fh.flush
end