Class: PureMVC::Facade

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

Overview

A base Multiton IFacade implementation.

See Also:

Constant Summary collapse

MULTITON_MSG =

Message Constants

'Facade instance for this Multiton key already constructed!'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Facade

Constructor.

This IFacade implementation is a Multiton, so you should not call the constructor directly. Instead, use the static factory method and pass the unique key for this instance with factory: PureMVC::Facade.get_instance(key) { |key| PureMVC::Facade.new(key) }.

Parameters:

  • key (String)

Raises:

  • (RuntimeError)

    if an instance for this Multiton key has already been constructed.



72
73
74
75
76
77
78
79
# File 'src/patterns/facade/facade.rb', line 72

def initialize(key)
  raise MULTITON_MSG if self.class.instance_map[key]

  self.class.instance_map[key] = self
  @model = @view = @controller = nil
  initialize_notifier(key)
  initialize_facade
end

Class Method Details

.get_instance(key, &factory) ⇒ IFacade

Facade Multiton Factory method.

Parameters:

  • key (String)

    the unique key identifying the Multiton instance

  • factory (^(String) -)

    _IFacade] the unique key passed to the factory block

Returns:

  • (IFacade)

    the Multiton instance of the Facade



34
35
36
37
38
# File 'src/patterns/facade/facade.rb', line 34

def get_instance(key, &factory)
  mutex.synchronize do
    instance_map[key] ||= factory.call(key)
  end
end

.has_core?(key) ⇒ Boolean

Check if a Core is registered or not.

Parameters:

  • key (String)

    the multiton key for the Core in question

Returns:

  • (Boolean)

    whether a Core is registered with the given key.



44
45
46
# File 'src/patterns/facade/facade.rb', line 44

def has_core?(key)
  instance_map.key?(key)
end

.instance_mapHash{String => IFacade}

The Multiton IFacade instanceMap.

Returns:

  • (Hash{String => IFacade})


23
# File 'src/patterns/facade/facade.rb', line 23

def instance_map = (@instance_map ||= {})

.mutexMutex

Mutex used to synchronize access to the instance map for thread safety.

Returns:

  • (Mutex)


27
# File 'src/patterns/facade/facade.rb', line 27

def mutex = (@mutex ||= Mutex.new)

.remove_core(key) ⇒ Object

Remove a Core.

Removes the Model, View, Controller, and Facade instances associated with the given key.

Parameters:

  • key (String)

    the key of the Core to remove



54
55
56
57
58
59
60
61
# File 'src/patterns/facade/facade.rb', line 54

def remove_core(key)
  mutex.synchronize do
    Model.remove_model(key)
    View.remove_view(key)
    Controller.remove_controller(key)
    instance_map.delete(key)
  end
end

Instance Method Details

#has_command?(notification_name) ⇒ Boolean

Check if a Command is registered for a given Notification

Parameters:

  • notification_name (String)

    The name of the Notification to check

Returns:

  • (Boolean)

    whether a Command is currently registered for the given notification_name.



158
159
160
# File 'src/patterns/facade/facade.rb', line 158

def has_command?(notification_name)
  !!@controller&.has_command?(notification_name)
end

#has_mediator?(mediator_name) ⇒ Boolean

Check if a Mediator is registered or not

Parameters:

  • mediator_name (String)

    the name of the Mediator

Returns:

  • (Boolean)

    whether a Mediator is registered with the given mediator_name.



219
220
221
# File 'src/patterns/facade/facade.rb', line 219

def has_mediator?(mediator_name)
  !!@view&.has_mediator?(mediator_name)
end

#has_proxy?(proxy_name) ⇒ Boolean

Check if a Proxy is registered

Parameters:

  • proxy_name (String)

    the name of the Proxy

Returns:

  • (Boolean)

    whether a Proxy is currently registered with the given proxyName.



188
189
190
# File 'src/patterns/facade/facade.rb', line 188

def has_proxy?(proxy_name)
  !!@model&.has_proxy?(proxy_name)
end

#initialize_controllerObject

Initialize the Controller.

Called by the initialize_facade method.

Override this method in your subclass of Facade if one or both of the following are true:

  • You wish to initialize a different IController.

  • You have Commands to register with the Controller at startup.

If you don’t want to initialize a differentIController,callsuper.initialize_controller() at the beginning of your method, then register Commands.



103
104
105
# File 'src/patterns/facade/facade.rb', line 103

def initialize_controller
  @controller = Controller.get_instance(@multiton_key) { |key| Controller.new(key) }
end

#initialize_facadeObject

Note:

Be sure to call super.initialize_facade when overriding.

Initialize the Multiton Facade instance.

This method is called automatically by the constructor. Override it in your subclass to perform any subclass-specific initialization.



87
88
89
90
91
# File 'src/patterns/facade/facade.rb', line 87

def initialize_facade
  initialize_model
  initialize_controller
  initialize_view
end

#initialize_modelObject

Initialize the Model.

Called by the initializeFacade method.

Override this method in your subclass of Facade if one or both of the following are true:

  • You wish to initialize a different IModel.

  • You have Proxys to register with the Model that do not retrieve a reference to the Facade at construction time.

If you don’t want to initialize a different IModel, call super.initialize_model() at the beginning of your method, then register Proxys.

Note: This method is rarely overridden; in practice you are more likely to use a Command to create and register Proxys with the Model, since Proxys with mutable data will likely need to send INotifications and thus will likely want to fetch a reference to the Facade during their construction.



123
124
125
# File 'src/patterns/facade/facade.rb', line 123

def initialize_model
  @model = Model.get_instance(@multiton_key) { |key| Model.new(key) }
end

#initialize_notifier(key) ⇒ Object

Set the Multiton key for this facade instance.

This is not meant to be called directly. It is invoked internally by the constructor when get_instance is called. However, it must be public to implement INotifier.

Parameters:

  • key (String)

    the multiton key for this instance



263
264
265
# File 'src/patterns/facade/facade.rb', line 263

def initialize_notifier(key)
  @multiton_key = key
end

#initialize_viewObject

Initialize the View.

Called by the initializeFacade method.

Override this method in your subclass of Facade if one or both of the following are true:

  • You wish to initialize a different IView.

  • You have Observers to register with the View.

If you don’t want to initialize a different IView, call super.initialize_view() at the beginning of your method, then register IMediator instances.

Note: This method is rarely overridden; in practice you are more likely to use a Command to create and register Mediators with the View, since IMediator instances will need to send INotifications and thus will likely want to fetch a reference to the Facade during their construction.



142
143
144
# File 'src/patterns/facade/facade.rb', line 142

def initialize_view
  @view = View.get_instance(@multiton_key) { |key| View.new(key) }
end

#notify_observers(notification) ⇒ Object

Notify Observers.

This method is left public mostly for backward compatibility and to allow you to send custom notification classes using the facade.

Usually you should call send_notification and pass the parameters, never having to construct the notification yourself.

Parameters:

  • notification (INotification)

    the notification to have the View notify Observers.



241
242
243
# File 'src/patterns/facade/facade.rb', line 241

def notify_observers(notification)
  @view&.notify_observers(notification)
end

#register_command(notification_name, &factory) ⇒ Object

Register an ICommand with the Controller by Notification name.

Parameters:

  • notification_name (String)

    name of the INotification to associate with ICommand

  • factory (^<() -> ICommand)

    ] a reference to the Class of the ICommand



150
151
152
# File 'src/patterns/facade/facade.rb', line 150

def register_command(notification_name, &factory)
  @controller&.register_command(notification_name, &factory)
end

#register_mediator(mediator) ⇒ Object

Register an IMediator with the View.

Parameters:

  • mediator (IMediator)

    a reference to the IMediator



203
204
205
# File 'src/patterns/facade/facade.rb', line 203

def register_mediator(mediator)
  @view&.register_mediator(mediator)
end

#register_proxy(proxy) ⇒ Object

Register an IProxy with the Model by name.

Parameters:

  • proxy (IProxy)

    the IProxy instance to be registered with the Model.



172
173
174
# File 'src/patterns/facade/facade.rb', line 172

def register_proxy(proxy)
  @model&.register_proxy(proxy)
end

#remove_command(notification_name) ⇒ Object

Remove a previously registered ICommand to INotification mapping from the Controller.

Parameters:

  • notification_name (String)

    the name of the INotification to remove the ICommand



165
166
167
# File 'src/patterns/facade/facade.rb', line 165

def remove_command(notification_name)
  @controller&.remove_command(notification_name)
end

#remove_mediator(mediator_name) ⇒ IMediator?

Remove an IMediator from the View.

Parameters:

  • mediator_name (String)

    name of the IMediator to be removed.

Returns:

  • (IMediator, nil)

    the IMediator that was removed from the View, or nil



227
228
229
# File 'src/patterns/facade/facade.rb', line 227

def remove_mediator(mediator_name)
  @view&.remove_mediator(mediator_name)
end

#remove_proxy(proxy_name) ⇒ IProxy?

Remove an IProxy from the Model by name.

Parameters:

  • proxy_name (String)

    the IProxy to remove from the Model.

Returns:

  • (IProxy, nil)

    the IProxy that was removed from the Model, or nil



196
197
198
# File 'src/patterns/facade/facade.rb', line 196

def remove_proxy(proxy_name)
  @model&.remove_proxy(proxy_name)
end

#retrieve_mediator(mediator_name) ⇒ IMediator?

Retrieve an IMediator from the View.

Parameters:

  • mediator_name (String)

    the name of the IMediator to retrieve

Returns:

  • (IMediator, nil)

    the IMediator previously registered with the mediator_name



211
212
213
# File 'src/patterns/facade/facade.rb', line 211

def retrieve_mediator(mediator_name)
  @view&.retrieve_mediator(mediator_name)
end

#retrieve_proxy(proxy_name) ⇒ IProxy?

Retrieve an IProxy from the Model by name.

Parameters:

  • proxy_name (String)

    the name of the proxy to be retrieved.

Returns:

  • (IProxy, nil)

    the IProxy instance previously registered with the proxy_name



180
181
182
# File 'src/patterns/facade/facade.rb', line 180

def retrieve_proxy(proxy_name)
  @model&.retrieve_proxy(proxy_name)
end

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

Create and send an INotification.

Keeps us from having to construct new notification instances in our implementation code.

Parameters:

  • name (String)

    the name of the notification to send

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

    the body of the notification (optional)

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

    the type of the notification (optional)



252
253
254
# File 'src/patterns/facade/facade.rb', line 252

def send_notification(name, body = nil, type = nil)
  notify_observers(Notification.new(name, body, type))
end