Class: XMLRegistry

Inherits:
Object
  • Object
show all
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.



26
27
28
29
30
31
32
# File 'lib/xml-registry.rb', line 26

def initialize(template = '<root></root>', debug: false)

  @debug = debug
  super()
  @template, _ = RXFHelper.read(template)
  @doc = Rexle.new(@template)
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



24
25
26
# File 'lib/xml-registry.rb', line 24

def doc
  @doc
end

Instance Method Details

#[](path) ⇒ Object



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

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

#[]=(path, val) ⇒ Object



62
63
64
65
# File 'lib/xml-registry.rb', line 62

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’



127
128
129
# File 'lib/xml-registry.rb', line 127

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.



231
232
233
234
235
236
# File 'lib/xml-registry.rb', line 231

def export(s=nil)
  reg = print_scan(@doc.root).join("\n")
  # jr 250118 File.open(s){|f| f.write reg} if s
  RXFHelper.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



72
73
74
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
# File 'lib/xml-registry.rb', line 72

def get_key(path)

  key = @doc.root.element path
  raise ("xml-registry: key %s not found" % path) 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’) #=> [<funday>tuesday</funday>,<funday>friday</funday>]

returns an array of 0 or more Rexle elements



114
115
116
# File 'lib/xml-registry.rb', line 114

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



174
175
176
177
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
# File 'lib/xml-registry.rb', line 174

def import(s)

  r = RXFHelper.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(‘<root><app/></root>’)



37
38
39
# File 'lib/xml-registry.rb', line 37

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



146
147
148
149
# File 'lib/xml-registry.rb', line 146

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

#save(s) ⇒ Object

save the registry to the specified file path



153
154
155
# File 'lib/xml-registry.rb', line 153

def save(s)
  RXFHelper.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’



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/xml-registry.rb', line 47

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



134
135
136
# File 'lib/xml-registry.rb', line 134

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

#xml(options = {}) ⇒ Object



140
141
142
# File 'lib/xml-registry.rb', line 140

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

#xpath(s) ⇒ Object



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

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