Class: DWSRegistry

Inherits:
XMLRegistry
  • Object
show all
Includes:
RXFHelperModule
Defined in:
lib/dws-registry.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename = 'registry.xml', autosave: true, debug: false) ⇒ DWSRegistry

Returns a new instance of DWSRegistry.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dws-registry.rb', line 15

def initialize(filename='registry.xml', autosave: true, debug: false)
  
  super(debug: debug)
  
  @autosave = autosave    
  
  if filename then

    @filename = filename      
    
    if FileX.exists? filename then
      load_xml(filename)
    else
      save()
    end      

  end
end

Instance Method Details

#delete_key(path) ⇒ Object



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

def delete_key(path)
  super(path)
  save() if @autosave
end

#gem_register(gemfile) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dws-registry.rb', line 34

def gem_register(gemfile)
  
  if gemfile =~ /^\w+\:\/\// then
    
    code = Requestor.read(File.dirname(gemfile)) do |x| 
      x.require File.basename(gemfile)
    end
    
    eval code
    
  else
    
    require gemfile
    
  end
  
  if defined? RegGem::register then
    
    self.import RegGem::register 
    true
    
  else
    nil
  end
  
end

#get_key(path, auto_detect_type: false) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dws-registry.rb', line 61

def get_key(path, auto_detect_type: false)

  e = super path

  return e unless auto_detect_type
  
  raw_c = e.attributes[:type]

  c = raw_c if raw_c
  s = e.text

  return e if e.elements.length > 0 or s.nil?    
  return s unless c
        
  h = {
    string: ->(x) {x},
    boolean: ->(x){ 
      case x
      when 'true' then true
      when 'false' then false
      when 'on' then true
      when 'off' then false
      else x
      end
    },
    number: ->(x){  x[/^[0-9]+$/] ? x.to_i : x.to_f },
    time:   ->(x) {Time.parse x},
    json:   ->(x) {JSON.parse x}
  }
                          
  h[c.to_sym].call s   
  
end

#import(s) ⇒ Object



142
143
144
145
# File 'lib/dws-registry.rb', line 142

def import(s)      
  super(s)
  save() if @autosave
end

#refreshObject



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

def refresh()
  load_xml(@filename)    
end

#save(filename = nil) ⇒ Object



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

def save(filename=nil)
  @filename = filename if filename
  super(@filename)
end

#set_key(path, value) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/dws-registry.rb', line 95

def set_key(path, value)
  
  if @debug then
    puts 'inside set_key path: ' + path.inspect 
    puts '  value: '  + value.inspect
    puts '  value.class : '  + value.class.inspect
  end
  
  value, type = case value.class.to_s.downcase.to_sym
  when :string     then value
  when :time       then ["#%s#" % value.to_s, :time]
  when :fixnum     then [value.to_s, :number]
  when :integer    then [value.to_s, :number]
  when :float      then [value.to_s, :number]      
  when :falseclass then [value.to_s, :boolean]
  when :trueclass  then [value.to_s, :boolean]
  when :array       then ["%s" % value.to_json, :json]
  else value
  end
  
  e = super(path, value)    
  
  type = find_type value unless type
  e.attributes[:type] = type.to_s if type
  e.parent.attributes[:last_modified] = Time.now.to_s
  
  save() if @autosave

  onchange = e.attributes[:onchange]
  
  if onchange then
    RScript.new.run onchange.sub('$value', value).split
  end
  
  e
end