Class: Mediate::Mediator
- Inherits:
-
Object
- Object
- Mediate::Mediator
- Includes:
- Singleton
- Defined in:
- lib/mediate/mediator.rb
Overview
Implements the mediator pattern. Call #dispatch to send requests and
{#publish} to publish notifications.
Defined Under Namespace
Classes: NullHandler
Instance Method Summary collapse
-
#dispatch(request) ⇒ Object
Sends a request to the registered handler, passing to any applicable pipeline behaviors.
-
#initialize ⇒ Mediator
constructor
A new instance of Mediator.
-
#publish(notification) ⇒ void
Sends a notification to all registered handlers for the given notification’s type.
- #register_error_handler(handler_class, exception_class, dispatch_class) ⇒ Object
- #register_notification_handler(handler_class, notif_class) ⇒ Object
- #register_postrequest_behavior(behavior_class, request_class) ⇒ Object
- #register_prerequest_behavior(behavior_class, request_class) ⇒ Object
- #register_request_handler(handler_class, request_class) ⇒ Object
-
#reset ⇒ void
Clears all registered handlers and behaviors for this Mediator instance.
Constructor Details
#initialize ⇒ Mediator
Returns a new instance of Mediator.
28 29 30 |
# File 'lib/mediate/mediator.rb', line 28 def initialize reset end |
Instance Method Details
#dispatch(request) ⇒ Object
Sends a request to the registered handler, passing to any applicable pipeline behaviors.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mediate/mediator.rb', line 42 def dispatch(request) raise ArgumentError, "request cannot be nil" if request.nil? request_handler = resolve_handler(request_handlers, request.class, REQUEST_BASE) raise Errors::NoHandlerError, request.class if request_handler == NullHandler prerequest = collect_by_inheritance(prerequest_behaviors, request.class, REQUEST_BASE) postrequest = collect_by_inheritance(postrequest_behaviors, request.class, REQUEST_BASE) run_request_pipeline(request, prerequest, request_handler, postrequest) end |
#publish(notification) ⇒ void
This method returns an undefined value.
Sends a notification to all registered handlers for the given notification’s type.
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mediate/mediator.rb', line 60 def publish(notification) raise ArgumentError, "notification cannot be nil" if notification.nil? handler_classes = collect_by_inheritance(notification_handlers, notification.class, NOTIF_BASE) handler_classes.each do |handler_class| handler_class.new.handle(notification) rescue StandardError => e handle_exception(notification, e, NOTIF_BASE) # Don't break from loop, since we don't want a single notification handler to prevent others from # receiving notification. end end |
#register_error_handler(handler_class, exception_class, dispatch_class) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/mediate/mediator.rb', line 103 def register_error_handler(handler_class, exception_class, dispatch_class) if dispatch_class <= NOTIF_BASE register_error_handler_for_dispatch(handler_class, exception_class, dispatch_class, NOTIF_BASE) else register_error_handler_for_dispatch(handler_class, exception_class, dispatch_class, REQUEST_BASE) end end |
#register_notification_handler(handler_class, notif_class) ⇒ Object
85 86 87 88 89 |
# File 'lib/mediate/mediator.rb', line 85 def register_notification_handler(handler_class, notif_class) validate_base_class(handler_class, Mediate::NotificationHandler) validate_base_class(notif_class, NOTIF_BASE, allow_base: true) append_to_hash_value(notification_handlers, notif_class, handler_class) end |
#register_postrequest_behavior(behavior_class, request_class) ⇒ Object
97 98 99 100 101 |
# File 'lib/mediate/mediator.rb', line 97 def register_postrequest_behavior(behavior_class, request_class) validate_base_class(behavior_class, Mediate::PostrequestBehavior) validate_base_class(request_class, REQUEST_BASE, allow_base: true) append_to_hash_value(postrequest_behaviors, request_class, behavior_class) end |
#register_prerequest_behavior(behavior_class, request_class) ⇒ Object
91 92 93 94 95 |
# File 'lib/mediate/mediator.rb', line 91 def register_prerequest_behavior(behavior_class, request_class) validate_base_class(behavior_class, Mediate::PrerequestBehavior) validate_base_class(request_class, REQUEST_BASE, allow_base: true) append_to_hash_value(prerequest_behaviors, request_class, behavior_class) end |
#register_request_handler(handler_class, request_class) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/mediate/mediator.rb', line 73 def register_request_handler(handler_class, request_class) validate_base_class(handler_class, Mediate::RequestHandler) validate_base_class(request_class, REQUEST_BASE) request_handlers.compute(request_class) do |old_value| unless old_value.nil? || old_value == handler_class raise Errors::RequestHandlerAlreadyExistsError.new(request_class, old_value, handler_class) end handler_class end end |
#reset ⇒ void
This method returns an undefined value.
Clears all registered handlers and behaviors for this Mediator instance. This is useful
for cleaning up after integration tests.
117 118 119 120 121 122 123 |
# File 'lib/mediate/mediator.rb', line 117 def reset @request_handlers = Concurrent::Map.new @notification_handlers = Concurrent::Map.new @prerequest_behaviors = Concurrent::Map.new @postrequest_behaviors = Concurrent::Map.new @exception_handlers = Concurrent::Map.new end |