Class: XMLRegistry

Inherits:
Object
  • Object
show all
Includes:
RXFReadWriteModule
Defined in:
lib/xml-registry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template = '<root></root>', debug: false) ⇒ XMLRegistry

Returns a new instance of XMLRegistry.



29
30
31
32
33
34
35
# File 'lib/xml-registry.rb', line 29

def initialize(template = '<root></root>', debug: false)
  
  @debug = debug
  super()
  @template, _ = RXFReader.read(template)
  @doc = Rexle.new(@template)
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



27
28
29
# File 'lib/xml-registry.rb', line 27

def doc
  @doc
end

Instance Method Details

#[](path) ⇒ Object



122
123
124
125
# File 'lib/xml-registry.rb', line 122

def [](path)
  s = path.split('/').map {|x| x.is_number? ? x.prepend('x') : x}.join '/'
  @doc.root.element s
end

#[]=(path, val) ⇒ Object



65
66
67
68
# File 'lib/xml-registry.rb', line 65

def []=(path, val)
  s = path.split('/').map {|x| x[0].is_number? ? x.prepend('x') : x}.join '/'
  self.set_key(s, val)
end

#delete_key(path) ⇒ Object

delete the key at the specified path example: delete_key 'app/gtd/funday'



131
132
133
# File 'lib/xml-registry.rb', line 131

def delete_key(path)
  @doc.root.delete path    
end

#export(s = nil) ⇒ Object

Export the registry to file if the filepath is specified. Regardless, the registry will be returned as a string in the registry export format.



235
236
237
238
239
240
# File 'lib/xml-registry.rb', line 235

def export(s=nil)
  reg = print_scan(@doc.root).join("\n")
  # jr 250118 File.open(s){|f| f.write reg} if s
  FileX.write(s, reg) if s
  reg
end

#get_key(path) ⇒ Object

get the key value by passing the path example: get_key('app/gtd/funday').value #=> 'friday'

returns the value as a Rexle::Element



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/xml-registry.rb', line 75

def get_key(path)

  key = @doc.root.element path
  #raise ("xml-registry: key %s not found" % path) unless key
  return nil unless key
  
  key.instance_eval { 
    @path = path 

    def to_h(e)

      v = if e.has_elements? then 
        e.elements.inject({}) do |r, x|
          r.merge to_h(x)
        end
      else
        e.text
      end

      {e.name => v}
    end    
  
    def to_config()                    
      SimpleConfig.new(to_h(self), attributes: {key: @path})
    end    
  
    def to_kvx()                    
      Kvx.new(to_h(self), attributes: {key: @path})
    end        
  
    def to_os()
      OpenStruct.new(to_h(self).first.last)
    end
  }
  
  key
end

#get_keys(path) ⇒ Object

get several keys using a Rexle XPath example: get_keys('//funday') #=> [tuesday,friday]

returns an array of 0 or more Rexle elements



118
119
120
# File 'lib/xml-registry.rb', line 118

def get_keys(path)
  @doc.root.xpath(path)
end

#import(s) ⇒ Object

[app/app1] "admin"="jrobertson" "pin-no"="1234"

[app/app2] "admin"="dsmith" "pin-no"="4321" REG

reg = XMLRegistry.new reg.import s



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/xml-registry.rb', line 178

def import(s)      
  
  r = RXFReader.read(s)
  reg_buffer = r.first
  raise "read file error" unless reg_buffer

  if  reg_buffer.strip[/^\[/] then

    reg_items = reg_buffer.gsub(/\n/,'').split(/(?=\[.[^\]]+\])/).map do |x| 
      [x[/^\[(.[^\]]+)\]/,1], Hash[*($').scan(/"([^"]+)"="(.[^"]*)"/).flatten]]
    end
    
  elsif reg_buffer.strip.lines.grep(/^\s+\w/).any? 

    puts 'hierachical import' if @debug
    doc = LineTree.new(reg_buffer).to_doc
    
    reg_items = []
    
    doc.root.each_recursive do |e|
      
      puts 'e: ' + e.inspect if @debug
      if e.is_a?(Rexle::Element) and e.children.length < 2 then
        
        reg_items << [e.backtrack.to_xpath.sub(/^root\//,'')\
                      .sub(/\/[^\/]+$/,''), {e.name => e.text }]
        
      end
        
    end      
    
  else

    reg_items = reg_buffer.split(/(?=^[^:]+$)/).map do |raw_section|

      lines = raw_section.lines.to_a
      next if lines.first.strip.empty?
      path = lines.shift.rstrip
      [path, Hash[lines.map{|x| x.split(':',2).map(&:strip)}]]
    end
    
    reg_items.compact!
    
  end

  puts 'reg_items: ' + reg_items.inspect if @debug
  reg_items.each do |path, items|
    items.each {|k,value| self.set_key("%s/%s" % [path,k], value)}
  end
  
  :import
end

#initialise_registry(new_template = nil) ⇒ Object Also known as: reset_registry, initialize_registry

Creates a new registry document from scratch. Optional param: string XML template example: initialise_registry('')



40
41
42
# File 'lib/xml-registry.rb', line 40

def initialise_registry(new_template=nil)
  @doc = Rexle.new(new_template || @template)
end

#load_xml(s = '') ⇒ Object

load a new registry xml document replacing the existing registry



150
151
152
153
# File 'lib/xml-registry.rb', line 150

def load_xml(s='')      
  @doc = Rexle.new(RXFReader.read(s)[0].force_encoding("UTF-8"))
  self
end

#save(s) ⇒ Object

save the registry to the specified file path



157
158
159
# File 'lib/xml-registry.rb', line 157

def save(s)
  FileX.write s, @doc.xml(pretty: true)
end

#set_key(path, value) ⇒ Object

Set the key by passing in the path and the value example: set_key 'app/gtd/funday', 'friday'



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/xml-registry.rb', line 50

def set_key(path, value)

  # if the 1st element doesn't exist create it
  e = path.split('/',2).first
  @doc.root.add_element Rexle::Element.new(e) unless @doc.root.element e
  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

return the registry as an XML document example: puts reg.to_xml pretty: true



138
139
140
# File 'lib/xml-registry.rb', line 138

def to_xml(options={})
  @doc.xml options
end

#xml(options = {}) ⇒ Object



144
145
146
# File 'lib/xml-registry.rb', line 144

def xml(options={})
  @doc.root.xml options
end

#xpath(s) ⇒ Object



242
243
244
# File 'lib/xml-registry.rb', line 242

def xpath(s)
  @doc.root.xpath s
end