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) ⇒ DWSRegistry

Returns a new instance of DWSRegistry.



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

def initialize(filename='registry.xml', autosave: true)
  
  super()
  
  @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



97
98
99
100
# File 'lib/dws-registry.rb', line 97

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

#get_key(path, auto_detect_type: false) ⇒ Object



33
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
60
61
62
63
64
65
# File 'lib/dws-registry.rb', line 33

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



107
108
109
110
# File 'lib/dws-registry.rb', line 107

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

#refreshObject



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

def refresh()
  load_xml(@filename)    
end

#save(filename = nil) ⇒ Object



102
103
104
105
# File 'lib/dws-registry.rb', line 102

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

#set_key(path, value) ⇒ Object



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
94
95
# File 'lib/dws-registry.rb', line 67

def set_key(path, value)
  
  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]
  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