Class: Ludy::MessageDispatcher

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actor, msg, args, &fun) ⇒ MessageDispatcher

actor is the listener who listen to the message



18
19
20
21
22
23
# File 'lib/ludy/message_dispatcher.rb', line 18

def initialize actor, msg, args, &fun
  @actor = actor
  @msg = msg
  @matcher = PatternMatcher.new args, &fun
  define_method_in_actor
end

Class Method Details

.create(actor, msg, args, &fun) ⇒ Object

create a dispatcher or append arg_list to the existed dispatcher, see Kernel#defun



10
11
12
13
14
15
16
# File 'lib/ludy/message_dispatcher.rb', line 10

def self.create actor, msg, args, &fun
  if (@patchers ||= {})[[actor, msg]].nil?
    @patchers[[actor, msg]] = MessageDispatcher.new actor, msg, args, &fun
  else
    @patchers[[actor, msg]].listen args, &fun
  end
end

Instance Method Details

#listen(arg_list, &fun) ⇒ Object

open your ear and listen, and call me when you hear



25
26
27
# File 'lib/ludy/message_dispatcher.rb', line 25

def listen arg_list, &fun
  @matcher << [arg_list, fun]
end