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>') ⇒ XMLRegistry

Returns a new instance of XMLRegistry.



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

def initialize(template = '<root></root>')
  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



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

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

#[]=(path, val) ⇒ Object



60
61
62
63
# File 'lib/xml-registry.rb', line 60

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’



125
126
127
# File 'lib/xml-registry.rb', line 125

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.



209
210
211
212
213
214
# File 'lib/xml-registry.rb', line 209

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



70
71
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
# File 'lib/xml-registry.rb', line 70

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



112
113
114
# File 'lib/xml-registry.rb', line 112

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



172
173
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
# File 'lib/xml-registry.rb', line 172

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
    
  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.chomp
      [path, Hash[lines.map{|x| x.split(':',2).map(&:strip)}]]
    end
    
    reg_items.compact!
    
  end

  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>’)



35
36
37
# File 'lib/xml-registry.rb', line 35

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



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

def load_xml(s='')      
  @doc = Rexle.new(RXFHelper.read(s)[0])          
  self
end

#save(s) ⇒ Object

save the registry to the specified file path



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

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’



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

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



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

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

#xml(options = {}) ⇒ Object



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

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

#xpath(s) ⇒ Object



216
217
218
# File 'lib/xml-registry.rb', line 216

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