Class: Nanoc3::NotificationCenter
- Inherits:
-
Object
- Object
- Nanoc3::NotificationCenter
- Defined in:
- lib/nanoc3/base/notification_center.rb
Overview
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.
Class Method Summary collapse
-
.on(name, id = nil) {|*args| ... } ⇒ void
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) ⇒ void
Posts a notification with the given name and the given arguments.
-
.remove(name, id) ⇒ void
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.
Class Method Details
.on(name, id = nil) {|*args| ... } ⇒ void
This method returns an undefined value.
Adds the given block to the list of blocks that should be called when the notification with the given name is received.
30 31 32 33 34 35 |
# File 'lib/nanoc3/base/notification_center.rb', line 30 def on(name, id=nil, &block) initialize_if_necessary(name) # Add observer @notifications[name] << { :id => id, :block => block } end |
.post(name, *args) ⇒ void
This method returns an undefined value.
Posts a notification with the given name and the given arguments.
46 47 48 49 50 51 52 53 |
# File 'lib/nanoc3/base/notification_center.rb', line 46 def post(name, *args) initialize_if_necessary(name) # Notify all observers @notifications[name].each do |observer| observer[:block].call(*args) end end |
.remove(name, id) ⇒ void
This method returns an undefined value.
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.
66 67 68 69 70 71 |
# File 'lib/nanoc3/base/notification_center.rb', line 66 def remove(name, id) initialize_if_necessary(name) # Remove relevant observers @notifications[name].reject! { |i| i[:id] == id } end |