Class: Puppet::Settings::IniFile Private

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

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.

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.

"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


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.


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.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)

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.


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

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.


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

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.


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

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.


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

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.


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

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.


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

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.


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

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.


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

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.


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

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.


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

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.


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

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.


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

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