Class: EventAggregator::Aggregator
- Inherits:
-
Object
- Object
- EventAggregator::Aggregator
- Defined in:
- lib/event_aggregator/aggregator.rb
Overview
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
Constant Summary collapse
- @@pool =
Thread.pool(4)
- @@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
Class Method Summary collapse
-
.message_publish(message) ⇒ Object
Public: Will publish the specified message to all listeners who has registered for this message type.
-
.message_request(message) ⇒ Object
Public: Request a piece of information.
-
.register(listener, message_type, callback) ⇒ Object
Public: Register an EventAggregator::Listener to receive a specified message type.
-
.register_all(listener, callback) ⇒ Object
Public: Register an EventAggregator::Listener to receive every single message that is published.
-
.register_producer(message_type, callback) ⇒ Object
Public: Registering a producer with the Aggregator.
-
.reset ⇒ Object
Public: Resets the Aggregator to the initial state.
-
.translate_message_with(message_type, message_type_new, callback = lambda{|data| data}) ⇒ Object
Public: Will produce another message when a message type is published.
-
.unregister(listener, message_type) ⇒ Object
Public: Unegister an EventAggregator::Listener to a specified message type.
-
.unregister_all(listener) ⇒ Object
Public: As Unregister, but will unregister listener from all message types.
-
.unregister_producer(message_type) ⇒ Object
Public: Will remove a producer.
Class Method Details
.message_publish(message) ⇒ Object
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
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/event_aggregator/aggregator.rb', line 80 def self. ( ) raise "Invalid message" unless .respond_to?(:message_type) && .respond_to?(:data) @@listeners[.].each do |listener, callback| (.data, callback, .async, .consisten_data) end @@listeners_all.each do |listener,callback| (, callback, .async, .consisten_data) end @@message_translation[.].each do |, callback| #TODO: I added message.async and consisten_data here. Add tests for that, and make a new version EventAggregator::Message.new(, callback.call(.data), .async, .consisten_data).publish end end |
.message_request(message) ⇒ Object
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.
152 153 154 |
# File 'lib/event_aggregator/aggregator.rb', line 152 def self.() @@producers[.] ? @@producers[.].call(.data) : nil end |
.register(listener, message_type, callback) ⇒ Object
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.
29 30 31 32 33 34 35 |
# File 'lib/event_aggregator/aggregator.rb', line 29 def self.register( listener, , callback ) raise "Illegal listener" unless listener.class < EventAggregator::Listener raise "Illegal message_type" if == nil raise "Illegal callback" unless callback.respond_to?(:call) @@listeners[][listener] = callback end |
.register_all(listener, callback) ⇒ Object
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.
46 47 48 49 50 |
# 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) ⇒ Object
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})
129 130 131 132 133 134 |
# File 'lib/event_aggregator/aggregator.rb', line 129 def self.register_producer(, callback) raise "Illegal message_type" if == nil raise "Illegal callback" unless callback.respond_to?(:call) && callback.arity == 1 @@producers[] = callback end |
.reset ⇒ Object
Public: Resets the Aggregator to the initial state. This removes all registered listeners. Use EventAggregator::Aggregator.reset before each test when doing unit testing.
98 99 100 101 102 103 |
# 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}) ⇒ Object
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.
111 112 113 114 115 116 |
# File 'lib/event_aggregator/aggregator.rb', line 111 def self.(, , callback=lambda{|data| data}) raise "Illegal parameters" if == nil || == 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 == || .eql?() @@message_translation[][] = callback unless @@message_translation[][] == callback end |
.unregister(listener, message_type) ⇒ Object
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.
59 60 61 |
# File 'lib/event_aggregator/aggregator.rb', line 59 def self.unregister( listener, ) @@listeners[].delete(listener) end |
.unregister_all(listener) ⇒ Object
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.
67 68 69 70 71 72 |
# 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) ⇒ Object
Public: Will remove a producer.
message_type - The message type which will no longer respond to message requests.
141 142 143 |
# File 'lib/event_aggregator/aggregator.rb', line 141 def self.unregister_producer() @@producers.delete() end |