Class: Puppet::Util::NimsoftConfig

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ NimsoftConfig

Returns a new instance of NimsoftConfig.



27
28
29
30
31
32
# File 'lib/puppet/util/nimsoft_config.rb', line 27

def initialize(filename)
  @name = filename
  @children = []
  @loaded = false
  @tabsize = 3
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



5
6
7
# File 'lib/puppet/util/nimsoft_config.rb', line 5

def children
  @children
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/puppet/util/nimsoft_config.rb', line 5

def name
  @name
end

#tabsizeObject

Returns the value of attribute tabsize.



5
6
7
# File 'lib/puppet/util/nimsoft_config.rb', line 5

def tabsize
  @tabsize
end

Class Method Details

.add(filename) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/puppet/util/nimsoft_config.rb', line 11

def self.add(filename)
  @configfiles ||= {}
  if @configfiles.include? filename
    @configfiles[filename]
  else
    @configfiles[filename] = new(filename)
  end
end

.flush(filename) ⇒ Object



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

def self.flush(filename)
  @configfiles ||= {}
  if @configfiles[filename]
    @configfiles[filename].sync
  end
end

.initvarsObject



7
8
9
# File 'lib/puppet/util/nimsoft_config.rb', line 7

def self.initvars
  @configfiles = {}
end

Instance Method Details

#child(name) ⇒ Object



71
72
73
# File 'lib/puppet/util/nimsoft_config.rb', line 71

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

#loaded?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/puppet/util/nimsoft_config.rb', line 34

def loaded?
  @loaded
end

#parseObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/puppet/util/nimsoft_config.rb', line 38

def parse
  Puppet.debug "parsing nimsoft configuration file #{@name}"
  if File.exists?(@name)
    current_section = self
    File.open(@name, 'r') do |f|
      f.each_line do |line|
        case line.chomp!
        when /^\s*<([^\/]+)>.*$/
          name = $1.gsub('#','/')
          current_section = Puppet::Util::NimsoftSection.new(name, current_section)
        when /^\s*(.*?)\s*=\s*(.*)$/
          key = $1
          value =$2
          current_section[key.intern] = value
        when /^\s*<\/(.*)>\s*$/
          current_section = current_section.parent
        end
      end
      @loaded = true
    end
  end
end

#path(name) ⇒ Object



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

def path(name)
  sectionname, subsections = name.split('/',2)
  section = child(sectionname) || Puppet::Util::NimsoftSection.new(sectionname, self)
  if subsections
    section.path(subsections)
  else
    section
  end
end

#syncObject



75
76
77
78
79
# File 'lib/puppet/util/nimsoft_config.rb', line 75

def sync
  File.open(@name, 'w') do |f|
    f.puts to_cfg
  end
end

#to_cfgObject



81
82
83
# File 'lib/puppet/util/nimsoft_config.rb', line 81

def to_cfg
  @children.inject("") { |content, section| content << section.to_cfg(tabsize) }
end