Class: Nanoc3::NotificationCenter
Nanoc3::NotificationCenter provides a way to send notifications between objects. It allows blocks associated with a certain notification name to be registered; these blocks will be called when the notification with the given name is posted.
It is a slightly different implementation of the Observer pattern; the table of subscribers is not stored in the observable object itself, but in the notification center.
Public Visibility
Public Class Method Summary
| on(name, id = nil, &block) |
Adds the given block to the list of blocks that should be called when the notification with the given name is received. |
|---|---|
| post(name, *args) |
Posts a notification with the given name. |
| remove(name, id) |
Removes the block with the given identifier from the list of blocks that should be called when the notification with the given name is posted. |
Public Class Method Details
on
Adds the given block to the list of blocks that should be called when the notification with the given name is received.
| name: | The name of the notification that will be posted. |
| id: | An identifier for the block. This is only used to be able to remove the block (using the remove method) later. Defaults to nil. |
25 26 27 28 29 30 |
# File 'lib/nanoc3/base/notification_center.rb', line 25 def on(name, id=nil, &block) initialize_if_necessary(name) # Add observer @notifications[name] << { :id => id, :block => block } end |
post
Posts a notification with the given name. All arguments wil be passed to the blocks handling the notification.
34 35 36 37 38 39 40 41 |
# File 'lib/nanoc3/base/notification_center.rb', line 34 def post(name, *args) initialize_if_necessary(name) # Notify all observers @notifications[name].each do |observer| observer[:block].call(*args) end end |
remove
Removes the block with the given identifier from the list of blocks that should be called when the notification with the given name is posted.
| name: | The name of the notification that will be posted. |
| id: | The identifier of the block that should be removed. |
50 51 52 53 54 55 |
# File 'lib/nanoc3/base/notification_center.rb', line 50 def remove(name, id) initialize_if_necessary(name) # Remove relevant observers @notifications[name].reject! { |i| i[:id] == id } end |