Class: Dizby::InvokeMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/dizby/worker/invoke_method.rb

Instance Method Summary collapse

Constructor Details

#initialize(server, obj, msg, argv, block) ⇒ InvokeMethod

Returns a new instance of InvokeMethod.


6
7
8
9
10
11
12
13
# File 'lib/dizby/worker/invoke_method.rb', line 6

def initialize(server, obj, msg, argv, block)
  @obj = obj
  @msg_id = msg.to_sym
  @argv = argv
  @block = block && proc { |*args| call_block(block, *args) }

  @server = server
end

Instance Method Details

#call_block(block, *args) (private)


32
33
34
35
36
37
38
39
40
# File 'lib/dizby/worker/invoke_method.rb', line 32

def call_block(block, *args)
  if args.size == 1 && args[0].is_a?(Array)
    args[0] = DistributedArray.new(args[0], @server)
  end

  block.call(*args)
rescue LocalJumpError
  handle_jump_error($!)
end

#handle_jump_error(err) (private)


42
43
44
45
46
47
48
49
# File 'lib/dizby/worker/invoke_method.rb', line 42

def handle_jump_error(err)
  case err.reason
  when :break
    err.exit_value
  else
    fail err
  end
end

#perform


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dizby/worker/invoke_method.rb', line 15

def perform
  result = send_to_object

  # TODO: do we care what the @msg_id is?
  # Should we convert to a DistributedArray regardless?
  if @msg_id == :to_ary && result.class == Array
    result = DistributedArray.new(result, @server)
  end

  [true, result]
rescue StandardError, ScriptError, Interrupt
  @server.log.backtrace($!)
  [false, $!]
end

#send_to_object (private)


51
52
53
# File 'lib/dizby/worker/invoke_method.rb', line 51

def send_to_object
  @obj.__send__(@msg_id, *@argv, &@block)
end