Class: DWSRegistry

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



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

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



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

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

#gem_register(gemfile) ⇒ Object



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

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



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

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



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

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

#refreshObject



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

def refresh()
  load_xml(@filename)
end

#save(filename = nil) ⇒ Object



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

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

#set_key(path, value) ⇒ Object



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
131
# File 'lib/dws-registry.rb', line 96

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