Class: Puppet::Util::NimsoftSection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent = nil) ⇒ NimsoftSection

Returns a new instance of NimsoftSection.



5
6
7
8
9
10
11
12
# File 'lib/puppet/util/nimsoft_section.rb', line 5

def initialize(name, parent = nil)
  @parent = parent
  @parent.children << self unless @parent.nil?
  @name = name
  @children = []
  @attributes = {}
  @attribute_order = []
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



3
4
5
# File 'lib/puppet/util/nimsoft_section.rb', line 3

def attributes
  @attributes
end

#childrenObject

Returns the value of attribute children.



3
4
5
# File 'lib/puppet/util/nimsoft_section.rb', line 3

def children
  @children
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/puppet/util/nimsoft_section.rb', line 3

def name
  @name
end

#parentObject

Returns the value of attribute parent.



3
4
5
# File 'lib/puppet/util/nimsoft_section.rb', line 3

def parent
  @parent
end

Instance Method Details

#[](name) ⇒ Object



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

def [](name)
  @attributes[name]
end

#[]=(name, value) ⇒ Object



22
23
24
25
# File 'lib/puppet/util/nimsoft_section.rb', line 22

def []=(name, value)
  @attributes[name] = value
  @attribute_order << name unless @attribute_order.include? name
end

#child(name) ⇒ Object



14
15
16
# File 'lib/puppet/util/nimsoft_section.rb', line 14

def child(name)
  @children.select { |c| c.name == name }.first
end

#clear_attrObject



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

def clear_attr
  @attributes.clear
  @attribute_order.clear
end

#del_attr(name) ⇒ Object



27
28
29
30
# File 'lib/puppet/util/nimsoft_section.rb', line 27

def del_attr(name)
  @attributes.delete(name)
  @attribute_order.delete(name)
end

#keys_in_orderObject



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

def keys_in_order
  @attribute_order
end

#path(name) ⇒ Object



59
60
61
62
63
64
# File 'lib/puppet/util/nimsoft_section.rb', line 59

def path(name)
  return self unless name
  name.split('/').inject(self) do |section, subsectionname|
    section.child(subsectionname) || Puppet::Util::NimsoftSection.new(subsectionname, section)
  end
end

#to_cfg(tabsize = 3, indent = 0) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/puppet/util/nimsoft_section.rb', line 45

def to_cfg(tabsize = 3, indent = 0)
  s = " "*tabsize*indent << "<#{name.gsub('/', '#')}>\n"
  @attribute_order.each do |key|
    if @attributes[key].to_s.empty?
      s <<  " "*tabsize*(indent+1) << "#{key} =\n"
    else
      s <<  " "*tabsize*(indent+1) << "#{key} = #{@attributes[key]}\n"
    end
  end
  @children.each { |c| s << c.to_cfg(tabsize, indent+1) }
  s <<  " "*tabsize*indent << "</#{name.gsub('/','#')}>\n"
  s
end

#values_in_orderObject



41
42
43
# File 'lib/puppet/util/nimsoft_section.rb', line 41

def values_in_order
  @attribute_order.map { |attr| @attributes[attr] }
end