Class | Core::Config::XMLParser |
In: |
core/config.rb
|
Parent: | Object |
ParserInterface implementation to read configuration parameters from a XML file.
Read and parse the XML file. Stores the options in an internal Hash to speed up the retrieving process.
# File core/config.rb, line 63 def initialize(file) @file = file fd = File.new(@file) @src = REXML::Document.new(fd) fd.close() @options = {} @modified = false @src.elements.each('dradis/option') do |element| @options[element.attributes['name'].to_sym] = element.attributes['value'] end end
See ParserInterface#get_option
# File core/config.rb, line 76 def get_option(key) if @options.key?(key) @options[key] else raise ArgumentError.new('option not found in config file!') end end
See ParserInterface#put_option
# File core/config.rb, line 85 def put_option(key, value) if @options.key?(key) return if @options[key] == value end @options[key] = value @modified = true end
# File core/config.rb, line 94 def save return unless @modified @options.each do |name, value| elements = @src.get_elements("dradis/option[@name='#{name}']") if (elements.size.zero?) # a new element needs to be created @src.root.add_element( 'option', { 'name' => name, 'value' => value}) else # update the existing element elements.first.attributes['value'] = value end end fd = File.new(@file, 'w') fmt = REXML::Formatters::Pretty.new(2) fmt.write( @src, fd ) fd.close() end