Class: Brainguy::Subscription

Inherits:
Object
  • Object
show all
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

FullSubscription, SingleEventSubscription

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, listener) ⇒ Subscription

Returns a new instance of Subscription.

Parameters:

  • owner (Emitter)

    the owning Emitter

  • listener (:call)

    a callable listener object



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

#listenerObject (readonly)

Returns the value of attribute listener.



13
# File 'lib/brainguy/subscription.rb', line 13

attr_reader :owner, :listener

#ownerEmitter (readonly)

Returns the owning Emitter.

Returns:



13
14
15
# File 'lib/brainguy/subscription.rb', line 13

def owner
  @owner
end

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

#cancelObject

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.

Parameters:

  • event (Event)

    the event to dispatch

Returns:

  • whatever the listener returns



26
27
28
# File 'lib/brainguy/subscription.rb', line 26

def handle(event)
  @listener.call(event)
end

#hashObject

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