Class: Nanoc::Int::NotificationCenter Private
- Inherits:
-
Object
- Object
- Nanoc::Int::NotificationCenter
- Defined in:
- lib/nanoc/base/services/notification_center.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
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
private
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
private
Posts a notification with the given name and the given arguments.
-
.remove(name, id) ⇒ void
private
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 is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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.
27 28 29 30 31 32 |
# File 'lib/nanoc/base/services/notification_center.rb', line 27 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 is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Posts a notification with the given name and the given arguments.
43 44 45 46 47 48 49 50 |
# File 'lib/nanoc/base/services/notification_center.rb', line 43 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 is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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.
63 64 65 66 67 68 |
# File 'lib/nanoc/base/services/notification_center.rb', line 63 def remove(name, id) initialize_if_necessary(name) # Remove relevant observers @notifications[name].reject! { |i| i[:id] == id } end |