Class: RBridge

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

Overview

Erlang サーバの関数を Ruby のメソッドのように扱えるようにするクラス

Constant Summary collapse

VERSION =
"0.1.2"

Instance Method Summary collapse

Constructor Details

#initialize(mod, host = "localhost", port = 9900, async = false) ⇒ RBridge

Erlang host server name (the default is “localhost”)

Port

Erlang server port number (the default is “9900”)

Async

Whether asynchronous communication (defaults to “false”)



36
37
38
39
40
41
42
43
44
# File 'lib/rbridge.rb', line 36

def initialize(mod, host="localhost", port=9900, async=false)
   # Which module should we call by default? If nothing is specified try the
   # erlang module.
  @mod = mod
   @mod = "erlang" if @mod.nil?
   
  @erlang = ErlangAdapter.new(host, port)
  @async = async
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object

Ruby-style method calling.



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rbridge.rb', line 47

def method_missing(symbol, *args)

  # Pop off the block used for asynchronous access.
  block = args.pop if @async == true

  # Try to create a command that Erlang knows what to do with from the ruby
   # style syntax we're given.
  command = @erlang.make_command(@mod, symbol, args)
  
  erl(command, block)
end

Instance Method Details

#erl(command, block = nil) ⇒ Object

Send commands to Erlang to be processed.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rbridge.rb', line 60

def erl(command, block=nil)
begin
  if @async == true then
    # If we're asynchronous generate a thread around the call then pass 
      # the results back to a block to display.
    Thread.new do
      res = @erlang.eval(command)
      raise res if @erlang.is_error(res)
      block.call(eval(res))
    end
  elsif @async == false then
    # Blocking mode should just eval and wait for data to come back.
    res = @erlang.eval(command)
    raise res if @erlang.is_error(res)
    eval res
  end
rescue => res
  raise "[Error]=>#{res}"
end
end