Class: Webgen::Blackboard

Inherits:
Object
  • Object
show all
Defined in:
lib/webgen/blackboard.rb

Overview

A blackboard object provides two features for inter-object communication:

  • services: An object can add a service to the blackboard which can be called by any other object by just specifing the service name. Therefore it is easy to change the underlying implementation of the service and there are no hard dependencies on specific class or method names.

  • listeners: Objects may register themselves for specific messsage names and get notified when such a message gets dispatched.

For a list of all available services and messages have a look at the main Webgen documentation page.

Instance Method Summary collapse

Constructor Details

#initializeBlackboard

Create a new Blackboard object.



20
21
22
23
# File 'lib/webgen/blackboard.rb', line 20

def initialize
  @listener = {}
  @services = {}
end

Instance Method Details

#add_listener(msg_names = nil, callable_object = nil, &block) ⇒ Object

Add the callable_object or the given block as listener for the messages msg_names (one message name or an array of message names).



27
28
29
30
31
32
33
34
35
# File 'lib/webgen/blackboard.rb', line 27

def add_listener(msg_names = nil, callable_object = nil, &block)
  callable_object = callable_object || block
  if !callable_object.nil?
    raise ArgumentError, "The listener needs to respond to 'call'" unless callable_object.respond_to?(:call)
    [msg_names].flatten.compact.each {|name| (@listener[name] ||= []) << callable_object}
  else
    raise ArgumentError, "You have to provide a callback object or a block"
  end
end

#add_service(service_name, callable_object = nil, &block) ⇒ Object

Add a service named service_name provided by the callable_object or a block to the blackboard.



52
53
54
55
56
57
58
59
60
# File 'lib/webgen/blackboard.rb', line 52

def add_service(service_name, callable_object = nil, &block)
  callable_object = callable_object || block
  if @services.has_key?(service_name)
    raise "The service name '#{service_name}' is already taken"
  else
    raise ArgumentError, "An object providing a service needs to respond to 'call'" unless callable_object.respond_to?(:call)
    @services[service_name] = callable_object
  end
end

#del_listener(msg_names, callable_object) ⇒ Object

Remove the given object from the dispatcher queues of the message names specified in msg_names.



39
40
41
# File 'lib/webgen/blackboard.rb', line 39

def del_listener(msg_names, callable_object)
  [msg_names].flatten.each {|name| @listener[name].delete(callable_object) if @listener[name]}
end

#del_service(service_name) ⇒ Object

Delete the service service_name.



63
64
65
# File 'lib/webgen/blackboard.rb', line 63

def del_service(service_name)
  @services.delete(service_name)
end

#dispatch_msg(msg_name, *args) ⇒ Object

Dispatch the message msg_name to all listeners for this message, passing the given arguments.



45
46
47
48
# File 'lib/webgen/blackboard.rb', line 45

def dispatch_msg(msg_name, *args)
  return unless @listener[msg_name]
  @listener[msg_name].each {|obj| obj.call(*args)}
end

#invoke(service_name, *args, &block) ⇒ Object

Invoke the service called service_name with the given arguments.



68
69
70
71
72
73
74
# File 'lib/webgen/blackboard.rb', line 68

def invoke(service_name, *args, &block)
  if @services.has_key?(service_name)
    @services[service_name].call(*args, &block)
  else
    raise ArgumentError, "No such service named '#{service_name}' available"
  end
end