Module: Hallon::Observable::ClassMethods
- Defined in:
- lib/hallon/observable.rb
Overview
This module is responsible for creating methods for registering callbacks. It expects a certain protocol to already available.
Instance Attribute Summary collapse
-
#callbacks ⇒ Method, Struct
readonly
Callbacks to attach to this object.
Class Method Summary collapse
-
.extended(other) ⇒ Object
When extended it’ll call #initialize_observable to set-up
other
.
Instance Method Summary collapse
- #callback_for(name) ⇒ Method protected
-
#initialize_observable ⇒ Object
protected
Run when ClassMethods are extended.
-
#subscribe(object, pointer) ⇒ Object
Subscribe to callbacks for a given pointer.
-
#trigger(pointer, event, *arguments) ⇒ Object
protected
Scans through the list of subscribers, trying to find any subscriber attached to this pointer.
Instance Attribute Details
#callbacks ⇒ Method, Struct (readonly)
Returns callbacks to attach to this object.
18 19 20 |
# File 'lib/hallon/observable.rb', line 18 def callbacks @callbacks end |
Class Method Details
.extended(other) ⇒ Object
When extended it’ll call #initialize_observable to set-up other
.
13 14 15 |
# File 'lib/hallon/observable.rb', line 13 def self.extended(other) other.send(:initialize_observable) end |
Instance Method Details
#callback_for(name) ⇒ Method (protected)
41 42 43 |
# File 'lib/hallon/observable.rb', line 41 def callback_for(name) method("#{name}_callback") end |
#initialize_observable ⇒ Object (protected)
Run when ClassMethods are extended.
It sets up the callbacks and all book-keeping required to keep track of all subscribers properly.
34 35 36 37 |
# File 'lib/hallon/observable.rb', line 34 def initialize_observable @callbacks = initialize_callbacks @observers = WeakObservable::Hub.new end |
#subscribe(object, pointer) ⇒ Object
Subscribe to callbacks for a given pointer.
24 25 26 |
# File 'lib/hallon/observable.rb', line 24 def subscribe(object, pointer) @observers.add(pointer.address, object, :trigger) end |
#trigger(pointer, event, *arguments) ⇒ Object (protected)
Scans through the list of subscribers, trying to find any subscriber attached to this pointer. For each subscriber, trigger the appropriate event.
53 54 55 56 57 |
# File 'lib/hallon/observable.rb', line 53 def trigger(pointer, event, *arguments) if results = @observers.notify(pointer.address, event, *arguments) results[-1] end end |