Class: ASS::Actor

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

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}, &block) ⇒ Actor

this class is a thin layer over ASS::Server



3
4
5
6
7
8
# File 'lib/ass/actor.rb', line 3

def initialize(name,opts={},&block)
  @server = ASS.server(name,opts)
  if block
    react(&block)
  end
end

Instance Method Details

#call(*args) ⇒ Object



42
43
44
# File 'lib/ass/actor.rb', line 42

def call(*args)
  @server.call(*args)
end

#cast(*args) ⇒ Object



46
47
48
# File 'lib/ass/actor.rb', line 46

def cast(*args)
  @server.cast(*args)
end

#queue(opts = {}) ⇒ Object



10
11
12
13
# File 'lib/ass/actor.rb', line 10

def queue(opts={})
  @server.queue(opts)
  self
end

#react(callback = nil, opts = nil, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ass/actor.rb', line 15

def react(callback=nil,opts=nil,&block)
  if block
    opts = callback
    callback = block
  end
  opts = {} if opts.nil?
  callback_factory = ASS::CallbackFactory.new(callback)
  server = @server # for closure capturing, needed for callback_factory
  @server.react(opts) {
    define_method(:on_call) do |_|
      raise "can't call an actor with method set to nil" if payload["method"].nil?
      callback_object = callback_factory.callback_for(server,header,payload)
      callback_object.send(payload["method"],payload["data"])
    end

    define_method(:on_error) do |e,_|
      callback_object = callback_factory.callback_for(server,header,payload)
      if callback_object.respond_to?(:on_error)
        callback_object.on_error(e,payload["data"])
      else
        raise e
      end
    end
  }
  self
end