Parent

Class/Module Index [+]

Quicksearch

Webgen::Blackboard

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

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

Public Class Methods

new() click to toggle source

Create a new Blackboard object.

# File lib/webgen/blackboard.rb, line 19
def initialize
  @listener = {}
  @services = {}
end

Public Instance Methods

add_listener(msg_names = nil, callable_object = nil, &block) click to toggle source

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 26
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) click to toggle source

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

# File lib/webgen/blackboard.rb, line 51
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) click to toggle source

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

# File lib/webgen/blackboard.rb, line 38
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) click to toggle source

Delete the service service_name.

# File lib/webgen/blackboard.rb, line 62
def del_service(service_name)
  @services.delete(service_name)
end
dispatch_msg(msg_name, *args) click to toggle source

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

# File lib/webgen/blackboard.rb, line 44
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) click to toggle source

Invoke the service called service_name with the given arguments.

# File lib/webgen/blackboard.rb, line 67
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

[Validate]

Generated with the Darkfish Rdoc Generator 2.