Class: PureMVC::Notifier

Inherits:
Object
  • Object
show all
Defined in:
src/patterns/observer/notifier.rb

Overview

A base INotifier implementation.

MacroCommand, SimpleCommand, Mediator, and Proxy all need to send Notifications.

The INotifier interface provides a common method called send_notification that relieves implementation code of the necessity to actually construct Notifications.

The Notifier class, which all the above-mentioned classes extend, provides an initialized reference to the Facade Multiton, which is required for the convenience method for sending Notifications. It also eases implementation, as these classes have frequent Facade interactions and usually require access to it anyway.

NOTE: In the MultiCore version of the framework, there is one caveat: notifiers cannot send notifications or reach the facade until they have a valid multiton_key.

The multiton_key is set:

  • on a SimpleCommand when it is executed by the <code>Controller<c/ode>

  • on a Mediator when registered with the View

  • on a Proxy when registered with the Model

Direct Known Subclasses

MacroCommand, Mediator, Proxy, SimpleCommand

Constant Summary collapse

MULTITON_MSG =

Message Constants

'multitonKey for this Notifier not yet initialized!'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#multiton_keyObject (readonly)



46
47
48
# File 'src/patterns/observer/notifier.rb', line 46

def multiton_key
  @multiton_key
end

Instance Method Details

#facadeIFacade

Return the Multiton Facade instance

Returns:

  • (IFacade)

    the facade instance for the notifier’s key

Raises:

  • (RuntimeError)

    if the multiton_key is not set



81
82
83
84
85
# File 'src/patterns/observer/notifier.rb', line 81

def facade
  raise MULTITON_MSG if @multiton_key.nil?

  Facade.get_instance(@multiton_key) { |key| Facade.new(key) }
end

#initialize_notifier(key) ⇒ Object

Initialize this INotifier instance.

This is how a Notifier receives its multiton_key. Any calls to send_notification or attempts to access the facade will fail until this method has been called.

Subclasses such as Mediator, Command, or Proxy may override this method if they need to send notifications or access the Facade instance as early as possible. However, note that the Facade cannot be accessed within the constructor of these classes, because initialize_notifier will not yet have been called at that point.

Parameters:

  • key (String)

    the multiton_key this INotifier will use



61
62
63
# File 'src/patterns/observer/notifier.rb', line 61

def initialize_notifier(key)
  @multiton_key = key
end

#send_notification(name, body = nil, type = nil) ⇒ Object

Create and send an INotification.

This method eliminates the need to manually construct INotification instances in your implementation code.

Parameters:

  • name (String)

    the name of the notification

  • body (Object, nil) (defaults to: nil)

    optional body

  • type (String, nil) (defaults to: nil)

    optional type



73
74
75
# File 'src/patterns/observer/notifier.rb', line 73

def send_notification(name, body = nil, type = nil)
  facade.send_notification(name, body, type)
end