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

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/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.



18
19
20
21
22
23
# File 'lib/vendor/puppet/util/inifile.rb', line 18

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

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#[](key) ⇒ Object

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



57
58
59
60
# File 'lib/vendor/puppet/util/inifile.rb', line 57

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 teh end of the section



45
46
47
48
49
50
51
52
53
# File 'lib/vendor/puppet/util/inifile.rb', line 45

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



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

def add_line(line)
  @entries << line
end

#dirty?Boolean

Has this section been modified since it’s been read in or written back to disk

Returns:

  • (Boolean)


27
28
29
# File 'lib/vendor/puppet/util/inifile.rb', line 27

def dirty?
  @dirty
end

#formatObject

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



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/vendor/puppet/util/inifile.rb', line 64

def format
  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
  text
end

#mark_cleanObject

Should only be used internally



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

def mark_clean
  @dirty = false
end