Class: Puppet::Util::IniConfig::Section
- Defined in:
- lib/puppet/util/inifile.rb
Overview
A section in a .ini file
Instance Attribute Summary collapse
-
#destroy ⇒ Object
writeonly
Sets the attribute destroy.
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Return the value associated with KEY.
-
#[]=(key, value) ⇒ Object
Set the entry ‘key=value’.
-
#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.
-
#destroy? ⇒ Boolean
Should the file be destroyed?.
-
#dirty? ⇒ Boolean
Does this section need to be updated in/removed from the associated file?.
-
#format ⇒ Object
Format the section as text in the way it should be written to file.
-
#initialize(name, file) ⇒ Section
constructor
A new instance of Section.
-
#mark_clean ⇒ Object
Should only be used internally.
- #mark_dirty ⇒ Object
Constructor Details
#initialize(name, file) ⇒ Section
Returns a new instance of Section.
21 22 23 24 25 26 27 |
# File 'lib/puppet/util/inifile.rb', line 21 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
19 20 21 |
# File 'lib/puppet/util/inifile.rb', line 19 def destroy=(value) @destroy = value end |
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
18 19 20 |
# File 'lib/puppet/util/inifile.rb', line 18 def entries @entries end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
18 19 20 |
# File 'lib/puppet/util/inifile.rb', line 18 def file @file end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
18 19 20 |
# File 'lib/puppet/util/inifile.rb', line 18 def name @name end |
Instance Method Details
#[](key) ⇒ Object
Return the value associated with KEY. If no such entry exists, return nil
73 74 75 76 |
# File 'lib/puppet/util/inifile.rb', line 73 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
61 62 63 64 65 66 67 68 69 |
# File 'lib/puppet/util/inifile.rb', line 61 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
55 56 57 |
# File 'lib/puppet/util/inifile.rb', line 55 def add_line(line) @entries << line end |
#destroy? ⇒ Boolean
Should the file be destroyed?
48 49 50 |
# File 'lib/puppet/util/inifile.rb', line 48 def destroy? @destroy end |
#dirty? ⇒ Boolean
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?
34 35 36 |
# File 'lib/puppet/util/inifile.rb', line 34 def dirty? @dirty or @destroy end |
#format ⇒ Object
Format the section as text in the way it should be written to file
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/puppet/util/inifile.rb', line 80 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_clean ⇒ Object
Should only be used internally
43 44 45 |
# File 'lib/puppet/util/inifile.rb', line 43 def mark_clean @dirty = false end |
#mark_dirty ⇒ Object
38 39 40 |
# File 'lib/puppet/util/inifile.rb', line 38 def mark_dirty @dirty = true end |