Class: DRbRegClient

Inherits:
Object
  • Object
show all
Defined in:
lib/drb_reg_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(host: nil, port: '59500', debug: false) ⇒ DRbRegClient

Returns a new instance of DRbRegClient.



13
14
15
16
17
18
19
20
# File 'lib/drb_reg_client.rb', line 13

def initialize(host: nil, port: '59500', debug: false)
  
  @port, @debug  = port, debug
  DRb.start_service

  # attach to the DRb server via a URI given on the command line
  @reg = DRbObject.new nil, "druby://#{host}:#{port}" if host
end

Instance Method Details

#delete_key(path) ⇒ Object



22
23
24
# File 'lib/drb_reg_client.rb', line 22

def delete_key(path)
  @reg.delete_key path
end

#get(s) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/drb_reg_client.rb', line 26

def get(s)
  
  raw_uri, path = s[/(?<=reg:\/\/).*/].split('/',2)
  host, port = raw_uri.split(':')        
  port ||= @port
  
  if @debug
    puts 'host:' + host.inspect
    puts 'port:' + port.inspect 
  end
  
  @reg = DRbObject.new nil, "druby://#{host}:#{port}"
  get_key(path)
  
end

#get_key(key = '', auto_detect_type: false) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/drb_reg_client.rb', line 42

def get_key(key='', auto_detect_type: false)

  r = @reg.get_key(key, auto_detect_type: auto_detect_type)     

  return unless r

  doc = Rexle.new(r)
  e = doc.root
  
  return e unless auto_detect_type
  
  c = e.attributes[:type]
  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

#get_keys(key) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/drb_reg_client.rb', line 80

def get_keys(key)
  
  recordset = @reg.get_keys(key)
  return unless recordset

  doc = Rexle.new(recordset)    
  doc.root.elements ? doc.root.elements.to_a : []
 
end

#import(s) ⇒ Object



90
91
92
# File 'lib/drb_reg_client.rb', line 90

def import(s)
  @reg.import s
end

#set(s, value) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/drb_reg_client.rb', line 94

def set(s, value)
  
  raw_uri, path = s[/(?<=reg:\/\/).*/].split('/',2)
  host, port = raw_uri.split(':')        
  port ||= @port
     
  @reg = DRbObject.new nil, "druby://#{host}:#{port}"
  set_key(path, value)
  
end

#set_key(key, value) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/drb_reg_client.rb', line 105

def set_key(key, value)

  r = @reg.set_key(key, value)
  return unless r

  doc = Rexle.new(r)
  doc.root
end

#xpath(path) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/drb_reg_client.rb', line 114

def xpath(path)
  
  r = @reg.xpath path

  return [] if r.empty?

  doc = Rexle.new(r)    
  doc.root.elements ? doc.root.elements.to_a : []
end