Module: Grove

Defined in:
lib/grove.rb

Constant Summary collapse

@@services =
{}

Class Method Summary collapse

Class Method Details

.configObject



49
50
51
52
53
54
55
# File 'lib/grove.rb', line 49

def self.config
  if File.exists?(config_file)
    File.open(config_file, File::RDONLY) { |f| read_config(f) }
  else
    {}
  end
end

.config_fileObject



85
86
87
# File 'lib/grove.rb', line 85

def self.config_file
  "#{ENV['HOME']}/.ruby-grove"
end

.const_missing(const) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/grove.rb', line 11

def self.const_missing const
  if @@services.has_key? const
    @@services[const]
  else
    @@services[const] = get const
  end
end

.get(const) ⇒ Object



19
20
21
22
# File 'lib/grove.rb', line 19

def self.get const
  service_config = config[const]
  ::DRbObject.new_with_uri(service_config)
end

.read_config(f) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/grove.rb', line 68

def self.read_config(f)
  @@config = Hash[(
    f.read.split(/[\r\n]+/).map do |line|
      a = line.split(':',2).map(&:strip)
      if a.size
        [a[0].to_sym, a[1]]
      end
    end.compact
  )]
end

.run_service(front_object) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/grove.rb', line 28

def self.run_service front_object
  trap("INT") { puts; exit }
  uri = nil
  t = Thread.new {
    $SAFE = 1
    server = ::DRb.start_service(nil, front_object)
    uri = server.uri
    ::DRb.thread.join
  }
  while uri.nil?
    sleep 0.01
  end
  set_config(front_object.name, uri)
  puts "Started #{front_object.name} service on #{uri}"
  t.join
end

.set_config(const, service_config) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/grove.rb', line 57

def self.set_config const, service_config
  File.open(config_file, File::CREAT|File::RDWR) { |f|
    f.flock(File::LOCK_EX)
    read_config(f)
    @@config[const.to_sym] = service_config
    f.truncate 0
    f.rewind
    write_config(f)
  }
end

.write_config(f) ⇒ Object



79
80
81
82
83
# File 'lib/grove.rb', line 79

def self.write_config(f)
  @@config.each do |key, value|
    f.puts "#{key}:#{value}"
  end
end