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



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

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



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

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(::Regexp.last_match(1), ::Regexp.last_match(2), ::Regexp.last_match(3)))
    when /^(\s*)([[:word:]]+)(\s*=\s*)(.*?)(\s*)$/
      config.append(SettingLine.new(::Regexp.last_match(1), ::Regexp.last_match(2), ::Regexp.last_match(3), ::Regexp.last_match(4), ::Regexp.last_match(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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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