Class: Celluloid::Call

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

Overview

Calls represent requests to an actor

Direct Known Subclasses

AsyncCall, SyncCall

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, arguments = [], block = nil) ⇒ Call

Returns a new instance of Call.


6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/celluloid/calls.rb', line 6

def initialize(method, arguments = [], block = nil)
  @method, @arguments = method, arguments
  if block
    if Celluloid.exclusive?
      # FIXME: nicer exception
      raise "Cannot execute blocks on sender in exclusive mode"
    end
    @block = BlockProxy.new(self, Celluloid.mailbox, block)
  else
    @block = nil
  end
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments


4
5
6
# File 'lib/celluloid/calls.rb', line 4

def arguments
  @arguments
end

#blockObject (readonly)

Returns the value of attribute block


4
5
6
# File 'lib/celluloid/calls.rb', line 4

def block
  @block
end

#methodObject (readonly)

Returns the value of attribute method


4
5
6
# File 'lib/celluloid/calls.rb', line 4

def method
  @method
end

Instance Method Details

#check(obj) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/celluloid/calls.rb', line 29

def check(obj)
  # NOTE: don't use respond_to? here
  begin
    meth = obj.method(@method)
  rescue NameError
    inspect_dump = begin
                     obj.inspect
                   rescue RuntimeError, NameError
                     simulated_inspect_dump(obj)
                   end
    raise NoMethodError, "undefined method `#{@method}' for #{inspect_dump}"
  end

  arity = meth.arity

  if arity >= 0
    raise ArgumentError, "wrong number of arguments (#{@arguments.size} for #{arity})" if @arguments.size != arity
  elsif arity < -1
    mandatory_args = -arity - 1
    raise ArgumentError, "wrong number of arguments (#{@arguments.size} for #{mandatory_args}+)" if arguments.size < mandatory_args
  end
rescue => ex
  raise AbortError.new(ex)
end

#dispatch(obj) ⇒ Object


23
24
25
26
27
# File 'lib/celluloid/calls.rb', line 23

def dispatch(obj)
  check(obj)
  _block = @block && @block.to_proc
  obj.public_send(@method, *@arguments, &_block)
end

#execute_block_on_receiverObject


19
20
21
# File 'lib/celluloid/calls.rb', line 19

def execute_block_on_receiver
  @block && @block.execution = :receiver
end

#simulated_inspect_dump(obj) ⇒ Object


54
55
56
57
58
59
60
61
62
# File 'lib/celluloid/calls.rb', line 54

def simulated_inspect_dump(obj)
  vars = obj.instance_variables.map do |var|
    begin
      "#{var}=#{obj.instance_variable_get(var).inspect}"
    rescue RuntimeError
    end
  end.compact.join(" ")
  "#<#{obj.class}:0x#{obj.object_id.to_s(16)} #{vars}>"
end