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.



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

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.



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

def destroy=(value)
  @destroy = value
end

#entriesObject (readonly)

Returns the value of attribute entries.



17
18
19
# File 'lib/puppet/util/inifile.rb', line 17

def entries
  @entries
end

#fileObject (readonly)

Returns the value of attribute file.



17
18
19
# File 'lib/puppet/util/inifile.rb', line 17

def file
  @file
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/puppet/util/inifile.rb', line 17

def name
  @name
end

Instance Method Details

#[](key) ⇒ Object

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



72
73
74
75
# File 'lib/puppet/util/inifile.rb', line 72

def [](key)
  entry = find_entry(key)
  return(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



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

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



54
55
56
# File 'lib/puppet/util/inifile.rb', line 54

def add_line(line)
  @entries << line
end

#destroy?Boolean

Should the file be destroyed?

Returns:

  • (Boolean)


47
48
49
# File 'lib/puppet/util/inifile.rb', line 47

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)


33
34
35
# File 'lib/puppet/util/inifile.rb', line 33

def dirty?
  @dirty or @destroy
end

#formatObject

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



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

def format
  if @destroy
    text = ""
  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



42
43
44
# File 'lib/puppet/util/inifile.rb', line 42

def mark_clean
  @dirty = false
end

#mark_dirtyObject



37
38
39
# File 'lib/puppet/util/inifile.rb', line 37

def mark_dirty
  @dirty = true
end