class EventAggregator::Aggregator

Public: TODO: Could potentially turn this into a module.

module OtherSingleton
        @index = -1
        @colors = %w{ red green blue }
        def self.change
                @colors[(@index += 1) % @colors.size]
        end
end

Public Class Methods

message_publish( message ) click to toggle source

Public: Will publish the specified message to all listeners

who has registered for this message type.

message - The message to be distributed to the listeners. async - true => message will be sent async. Default true consisten_data - true => the same object will be sent to all recievers. Default false

# File lib/event_aggregator/aggregator.rb, line 80
def self.message_publish ( message )
        raise "Invalid message" unless message.respond_to?(:message_type) && message.respond_to?(:data)
        @@listeners[message.message_type].each do |listener, callback|       
                perform_message_job(message.data, callback, message.async, message.consisten_data)
        end
        @@listeners_all.each do |listener,callback|
                perform_message_job(message, callback, message.async, message.consisten_data)
        end
        @@message_translation[message.message_type].each do |message_type_new, callback|
                #TODO: I added message.async and consisten_data here. Add tests for that, and make a new version
                EventAggregator::Message.new(message_type_new, callback.call(message.data), message.async, message.consisten_data).publish
        end
end
message_request(message) click to toggle source

Public: Request a piece of information.

message - The message that will be requested based on its message type and data.

Returns The data provided by a producer registered for this specific message type, or nil.

# File lib/event_aggregator/aggregator.rb, line 152
def self.message_request(message)
        @@producers[message.message_type] ? @@producers[message.message_type].call(message.data) : nil
end
register( listener, message_type, callback ) click to toggle source

Public: Register an EventAggregator::Listener to receive

a specified message type

listener - An EventAggregator::Listener which should receive

the messages.

message_type - The message type to receive. Can be anything except nil.

Often it is preferable to use a string eg. "Message Type".

callback - The callback that will be executed when messages of type equal

message_type is published. Is executed with message.data as parameter.
# File lib/event_aggregator/aggregator.rb, line 29
def self.register( listener, message_type, callback )
        raise "Illegal listener" unless listener.class < EventAggregator::Listener
        raise "Illegal message_type" if message_type == nil
        raise "Illegal callback" unless callback.respond_to?(:call)

        @@listeners[message_type][listener] = callback
end
register_all( listener, callback ) click to toggle source

Public: Register an EventAggregator::Listener to receive

every single message that is published.

listener - An EventAggregator::Listener which should receive

the messages.

callback - The callback that will be executed every time a message is published.

will execute with the message as parameter.
# File lib/event_aggregator/aggregator.rb, line 46
def self.register_all( listener, callback )
        raise "Illegal listener" unless listener.class < EventAggregator::Listener
        raise "Illegal callback" unless callback.respond_to?(:call)
        @@listeners_all[listener] = callback
end
register_producer(message_type, callback) click to toggle source

Public: Registering a producer with the Aggregator. A producer will respond to message requests, a

request for a certain piece of data.

message_type - The message type that this callback will respond to. callback - The callback that returns data to the requester. Must have one parameter.

Example:

EventAggregator::Aggregator.register_producer("GetMultipliedByTwo", lambda{|data| data*2})
# File lib/event_aggregator/aggregator.rb, line 129
def self.register_producer(message_type, callback)
        raise "Illegal message_type" if message_type == nil
        raise "Illegal callback" unless callback.respond_to?(:call) && callback.arity == 1
        
        @@producers[message_type] = callback
end
reset() click to toggle source

Public: Resets the Aggregator to the initial state. This removes all registered listeners. Use EventAggregator::Aggregator.reset before each test when doing unit testing.

# File lib/event_aggregator/aggregator.rb, line 98
def self.reset
        @@listeners = Hash.new{|h, k| h[k] = Hash.new}
        @@listeners_all = Hash.new
        @@message_translation = Hash.new{|h, k| h[k] = Hash.new }
        @@producers = Hash.new
end
translate_message_with(message_type, message_type_new, callback=lambda{|data| data}) click to toggle source

Public: Will produce another message when a message type is published.

message_type - Type of the message that will trigger a new message to be published. message_type_new - The type of the new message that will be published callback=lambda{|data| data} - The callback that will transform the data from message_type to message_type_new. Default: copy.

# File lib/event_aggregator/aggregator.rb, line 111
def self.translate_message_with(message_type, message_type_new, callback=lambda{|data| data})
        raise "Illegal parameters" if message_type == nil || message_type_new == nil || !callback.respond_to?(:call) || callback.arity != 1 #TODO: The callback.parameters is not 1.8.7 compatible.
        raise "Illegal parameters, equal message_type and message_type_new" if message_type == message_type_new || message_type.eql?(message_type_new)

        @@message_translation[message_type][message_type_new] = callback unless @@message_translation[message_type][message_type_new] == callback
end
unregister( listener, message_type ) click to toggle source

Public: Unegister an EventAggregator::Listener to a

specified message type. The listener will no
longer get messages of this type.

listener - The EventAggregator::Listener which should no longer receive

the messages.

message_type - The message type to unregister for.

# File lib/event_aggregator/aggregator.rb, line 59
def self.unregister( listener, message_type )
        @@listeners[message_type].delete(listener)
end
unregister_all( listener ) click to toggle source
Public: As Unregister, but will unregister listener from all message types.

!

listener - The listener who should no longer get any messages at all,
                     regardless of type.
# File lib/event_aggregator/aggregator.rb, line 67
def self.unregister_all( listener )
        @@listeners.each do |key,value|
                value.delete(listener)
        end
        @@listeners_all.delete(listener)
end
unregister_producer(message_type) click to toggle source

Public: Will remove a producer.

message_type - The message type which will no longer respond to message requests.

# File lib/event_aggregator/aggregator.rb, line 141
def self.unregister_producer(message_type)
        @@producers.delete(message_type)
end

Private Class Methods

perform_message_job(data, callback, async, consisten_data) click to toggle source
# File lib/event_aggregator/aggregator.rb, line 157
def self.perform_message_job(data, callback, async, consisten_data)
        case [async, consisten_data || data == nil]
        when [true, true]   then @@pool.process{ EventAggregator::MessageJob.new.perform(data,       callback) }
        when [true, false]  then @@pool.process{ EventAggregator::MessageJob.new.perform(data.clone, callback) }
        when [false, true]  then EventAggregator::MessageJob.new.perform(data,       callback)
        when [false, false] then EventAggregator::MessageJob.new.perform(data.clone, callback)
        end
end