Object
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.
Create a new Blackboard object.
# File lib/webgen/blackboard.rb, line 20 20: def initialize 21: @listener = {} 22: @services = {} 23: end
Add the callable_object or the given block as listener for the messages msg_names (one message name or an array of message names).
# File lib/webgen/blackboard.rb, line 27 27: def add_listener(msg_names = nil, callable_object = nil, &block) 28: callable_object = callable_object || block 29: if !callable_object.nil? 30: raise ArgumentError, "The listener needs to respond to 'call'" unless callable_object.respond_to?(:call) 31: [msg_names].flatten.compact.each {|name| (@listener[name] ||= []) << callable_object} 32: else 33: raise ArgumentError, "You have to provide a callback object or a block" 34: end 35: end
Add a service named service_name provided by the callable_object or a block to the blackboard.
# File lib/webgen/blackboard.rb, line 52 52: def add_service(service_name, callable_object = nil, &block) 53: callable_object = callable_object || block 54: if @services.has_key?(service_name) 55: raise "The service name '#{service_name}' is already taken" 56: else 57: raise ArgumentError, "An object providing a service needs to respond to 'call'" unless callable_object.respond_to?(:call) 58: @services[service_name] = callable_object 59: end 60: end
Remove the given object from the dispatcher queues of the message names specified in msg_names.
# File lib/webgen/blackboard.rb, line 39 39: def del_listener(msg_names, callable_object) 40: [msg_names].flatten.each {|name| @listener[name].delete(callable_object) if @listener[name]} 41: end
Delete the service service_name.
# File lib/webgen/blackboard.rb, line 63 63: def del_service(service_name) 64: @services.delete(service_name) 65: end
Dispatch the message msg_name to all listeners for this message, passing the given arguments.
# File lib/webgen/blackboard.rb, line 45 45: def dispatch_msg(msg_name, *args) 46: return unless @listener[msg_name] 47: @listener[msg_name].each {|obj| obj.call(*args)} 48: end
Invoke the service called service_name with the given arguments.
# File lib/webgen/blackboard.rb, line 68 68: def invoke(service_name, *args, &block) 69: if @services.has_key?(service_name) 70: @services[service_name].call(*args, &block) 71: else 72: raise ArgumentError, "No such service named '#{service_name}' available" 73: end 74: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.