Class: Brainguy::Subscription
- Inherits:
-
Object
- Object
- Brainguy::Subscription
- Includes:
- Comparable
- Defined in:
- lib/brainguy/subscription.rb
Overview
A value object that represents a single subscription to an event source.
It ties together an event source
to a listener
. A listener is simply
some call-able object.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#listener ⇒ Object
readonly
Returns the value of attribute listener.
-
#owner ⇒ Emitter
readonly
The owning Emitter.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compare this to another subscription.
-
#cancel ⇒ Object
Cancel the subscription (remove it from the
owner
). -
#handle(event) ⇒ Object
Dispatch
event
to the listener. -
#hash ⇒ Object
The hash value is based on the identity (but not the state) of
owner
andlistener
. -
#initialize(owner, listener) ⇒ Subscription
constructor
A new instance of Subscription.
Constructor Details
#initialize(owner, listener) ⇒ Subscription
Returns a new instance of Subscription.
17 18 19 20 21 |
# File 'lib/brainguy/subscription.rb', line 17 def initialize(owner, listener) @owner = owner @listener = listener freeze end |
Instance Attribute Details
#listener ⇒ Object (readonly)
Returns the value of attribute listener.
13 |
# File 'lib/brainguy/subscription.rb', line 13 attr_reader :owner, :listener |
Instance Method Details
#<=>(other) ⇒ Object
Compare this to another subscription
36 37 38 39 |
# File 'lib/brainguy/subscription.rb', line 36 def <=>(other) return nil unless other.is_a?(Subscription) equality_components <=> other.equality_components end |
#cancel ⇒ Object
Cancel the subscription (remove it from the owner
)
31 32 33 |
# File 'lib/brainguy/subscription.rb', line 31 def cancel @owner.delete(self) end |
#handle(event) ⇒ Object
Dispatch event
to the listener.
26 27 28 |
# File 'lib/brainguy/subscription.rb', line 26 def handle(event) @listener.call(event) end |
#hash ⇒ Object
The hash value is based on the identity (but not the state) of owner
and listener
. Two Subscription
objects with the same owner
and
listener
are considered to be equivalent.
44 45 46 |
# File 'lib/brainguy/subscription.rb', line 44 def hash [self.class, *equality_components].hash end |