Class: PureMVC::Observer
- Inherits:
-
Object
- Object
- PureMVC::Observer
- Defined in:
- src/patterns/observer/observer.rb
Overview
A base IObserver
implementation.
An Observer
is an object that encapsulates information about an interested object with a method that should be called when a particular INotification
is broadcast.
In PureMVC, the Observer
class assumes these responsibilities:
-
Encapsulate the notification (callback) method of the interested object.
-
Encapsulate the notification context (
self
) of the interested object. -
Provide methods for setting the notification method and context.
-
Provide a method for notifying the interested object.
Instance Attribute Summary collapse
-
#context ⇒ Object | nil
Context The object context for the callback.
-
#notify ⇒ Method | nil
Notify The callback method to be called on notification.
Instance Method Summary collapse
-
#compare_notify_context?(object) ⇒ Boolean
Compares the given object with the Observer’s context.
-
#initialize(notify = nil, context = nil) ⇒ Observer
constructor
Initialize an Observer with a notify method and context.
-
#notify_observer(notification) ⇒ Object
Calls the notify method with the given notification.
Constructor Details
#initialize(notify = nil, context = nil) ⇒ Observer
Initialize an Observer with a notify method and context.
35 36 37 38 |
# File 'src/patterns/observer/observer.rb', line 35 def initialize(notify = nil, context = nil) @notify = notify @context = context end |
Instance Attribute Details
#context ⇒ Object | nil
Returns context The object context for the callback.
29 30 31 |
# File 'src/patterns/observer/observer.rb', line 29 def context @context end |
#notify ⇒ Method | nil
Returns notify The callback method to be called on notification.
26 27 28 |
# File 'src/patterns/observer/observer.rb', line 26 def notify @notify end |
Instance Method Details
#compare_notify_context?(object) ⇒ Boolean
Compares the given object with the Observer’s context.
51 52 53 |
# File 'src/patterns/observer/observer.rb', line 51 def compare_notify_context?(object) object.equal?(@context) end |
#notify_observer(notification) ⇒ Object
Calls the notify method with the given notification.
43 44 45 |
# File 'src/patterns/observer/observer.rb', line 43 def notify_observer(notification) @notify&.call(notification) end |