Class: Puppet::Util::IniConfig::Section

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/util/inifile.rb

Overview

A section in a .ini file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, file) ⇒ Section

Returns a new instance of Section.



22
23
24
25
26
27
28
# File 'lib/puppet/util/inifile.rb', line 22

def initialize(name, file)
  @name = name
  @file = file
  @dirty = false
  @entries = []
  @destroy = false
end

Instance Attribute Details

#destroy=(value) ⇒ Object (writeonly)

Sets the attribute destroy

Parameters:

  • value

    the value to set the attribute destroy to.



20
21
22
# File 'lib/puppet/util/inifile.rb', line 20

def destroy=(value)
  @destroy = value
end

#entriesObject (readonly)

Returns the value of attribute entries.



19
20
21
# File 'lib/puppet/util/inifile.rb', line 19

def entries
  @entries
end

#fileObject (readonly)

Returns the value of attribute file.



19
20
21
# File 'lib/puppet/util/inifile.rb', line 19

def file
  @file
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/puppet/util/inifile.rb', line 19

def name
  @name
end

Instance Method Details

#[](key) ⇒ Object

Return the value associated with KEY. If no such entry exists, return nil



74
75
76
77
# File 'lib/puppet/util/inifile.rb', line 74

def [](key)
  entry = find_entry(key)
  (entry.nil? ? nil : entry[1])
end

#[]=(key, value) ⇒ Object

Set the entry ‘key=value’. If no entry with the given key exists, one is appended to the end of the section



62
63
64
65
66
67
68
69
70
# File 'lib/puppet/util/inifile.rb', line 62

def []=(key, value)
  entry = find_entry(key)
  @dirty = true
  if entry.nil?
    @entries << [key, value]
  else
    entry[1] = value
  end
end

#add_line(line) ⇒ Object

Add a line of text (e.g., a comment) Such lines will be written back out in exactly the same place they were read in



56
57
58
# File 'lib/puppet/util/inifile.rb', line 56

def add_line(line)
  @entries << line
end

#destroy?Boolean

Should the file be destroyed?

Returns:

  • (Boolean)


49
50
51
# File 'lib/puppet/util/inifile.rb', line 49

def destroy?
  @destroy
end

#dirty?Boolean

Note:

This section is dirty if a key has been modified or if the section has been modified so the associated file can be rewritten without this section.

Does this section need to be updated in/removed from the associated file?

Returns:

  • (Boolean)


35
36
37
# File 'lib/puppet/util/inifile.rb', line 35

def dirty?
  @dirty or @destroy
end

#formatObject

Format the section as text in the way it should be written to file



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/puppet/util/inifile.rb', line 81

def format
  if @destroy
    text = ''.dup
  else
    text = "[#{name}]\n"
    @entries.each do |entry|
      if entry.is_a?(Array)
        key, value = entry
        text << "#{key}=#{value}\n" unless value.nil?
      else
        text << entry
      end
    end
  end
  text
end

#mark_cleanObject

Should only be used internally



44
45
46
# File 'lib/puppet/util/inifile.rb', line 44

def mark_clean
  @dirty = false
end

#mark_dirtyObject



39
40
41
# File 'lib/puppet/util/inifile.rb', line 39

def mark_dirty
  @dirty = true
end