Class: XMLRegistry
- Inherits:
-
Object
- Object
- XMLRegistry
- Defined in:
- lib/xml-registry.rb
Instance Method Summary collapse
- #delete_key(path) ⇒ Object
- #export(s = nil) ⇒ Object
- #get_key(path) ⇒ Object
- #get_keys(path) ⇒ Object
- #import(s) ⇒ Object
- #initialise_registry ⇒ Object (also: #reset_registry)
-
#initialize ⇒ XMLRegistry
constructor
A new instance of XMLRegistry.
- #load_xml(s = '') ⇒ Object
- #save(s) ⇒ Object
- #set_key(path, value) ⇒ Object
- #to_xml(options = {}) ⇒ Object (also: #display_xml)
Constructor Details
#initialize ⇒ XMLRegistry
Returns a new instance of XMLRegistry.
9 10 11 12 13 |
# File 'lib/xml-registry.rb', line 9 def initialize() super() @template = '<root><system/><app/><user/><ip_address/></root>' @doc = Rexle.new(@template) end |
Instance Method Details
#delete_key(path) ⇒ Object
41 42 43 |
# File 'lib/xml-registry.rb', line 41 def delete_key(path) @doc.root.delete path end |
#export(s = nil) ⇒ Object
71 72 73 74 75 |
# File 'lib/xml-registry.rb', line 71 def export(s=nil) reg = print_scan(@doc.root).join("\n") File.open(s){|f| f.write reg} if s reg end |
#get_key(path) ⇒ Object
33 34 35 |
# File 'lib/xml-registry.rb', line 33 def get_key(path) @doc.root.element path end |
#get_keys(path) ⇒ Object
37 38 39 |
# File 'lib/xml-registry.rb', line 37 def get_keys(path) @doc.root.xpath path end |
#import(s) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/xml-registry.rb', line 59 def import(s) reg_buffer = read(s) reg_items = reg_buffer.gsub(/\n/,'').split(/(?=\[.[^\]]+\])/).map do |x| [x[/^\[(.[^\]]+)\]/,1], Hash[*($').scan(/"([^"]+)"="(.[^"]+)?"/).flatten]] end reg_items.each do |path, items| items.each {|k,value| self.set_key("%s/%s" % [path,k], value)} end end |
#initialise_registry ⇒ Object Also known as: reset_registry
15 16 17 |
# File 'lib/xml-registry.rb', line 15 def initialise_registry() @doc = Rexle.new(@template) end |
#load_xml(s = '') ⇒ Object
51 52 53 |
# File 'lib/xml-registry.rb', line 51 def load_xml(s='') @doc = Rexle.new(read(s)) end |
#save(s) ⇒ Object
55 56 57 |
# File 'lib/xml-registry.rb', line 55 def save(s) File.open(s, 'w'){|f| f.write @doc.xml} end |
#set_key(path, value) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/xml-registry.rb', line 21 def set_key(path, value) create_path = find_path(path) if not create_path.empty? then parent_path = (path.split('/') - create_path.reverse).join('/') key_builder(parent_path, create_path) end add_value(path, value) end |
#to_xml(options = {}) ⇒ Object Also known as: display_xml
45 46 47 |
# File 'lib/xml-registry.rb', line 45 def to_xml(={}) @doc.xml end |