Class: BabaScript::Baba

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

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Baba

Returns a new instance of Baba.



8
9
10
11
# File 'lib/babascript/baba.rb', line 8

def initialize(opts)
  @base  = opts[:base]  || "http://linda.masuilab.org"
  @space = opts[:space] || "takumibaba"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/babascript/baba.rb', line 39

def method_missing(name, *args, &block)
  cid = __create_callback_id
  tuple = [:babascript, :eval, name, args, {:callback => cid}]
  ts = linda.tuplespace[@space]
  ts.write tuple
  if block_given?
    ts.take [:babascript, :return, cid] do |result, info|
      next if result.size < 4
      block.call result[3]
    end
  else
    result = ts.take [:babascript, :return, cid]
    return result[3]
  end
end

Instance Method Details

#lindaObject



13
14
15
# File 'lib/babascript/baba.rb', line 13

def linda
  @linda ||= EM::RocketIO::Linda::Client.new @base
end

#run(code = nil, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/babascript/baba.rb', line 17

def run(code=nil, &block)
  raise ArgumentError "block or code require" unless block_given? or code.kind_of? String
  already_eventmachine_running = EM::reactor_running?
  this = self
  EM::run do
    linda.io.once :connect do
      EM::defer do
        if block_given?
          this.instance_eval &block
        else
          this.instance_eval code
        end
        unless already_eventmachine_running
          EM::add_timer 1 do
            EM::stop
          end
        end
      end
    end
  end
end