Class: FiniteMachine::Subscribers
- Inherits:
-
Object
- Object
- FiniteMachine::Subscribers
- Includes:
- Enumerable, MonitorMixin
- Defined in:
- lib/finite_machine/subscribers.rb
Overview
A class responsibile for storage of event subscribers
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Iterate over subscribers.
-
#empty? ⇒ Boolean
Check if anyone is subscribed.
-
#index(subscriber) ⇒ Object
Return index of the subscriber.
-
#initialize ⇒ Subscribers
constructor
Initialize a subscribers collection.
-
#reset ⇒ self
Reset subscribers.
-
#size ⇒ Integer
Number of subscribed listeners.
-
#subscribe(*observers) ⇒ undefined
Add listener to subscribers.
-
#visit(hook_event, *data) ⇒ undefined
Visit subscribers and notify.
Constructor Details
#initialize ⇒ Subscribers
Initialize a subscribers collection
14 15 16 17 |
# File 'lib/finite_machine/subscribers.rb', line 14 def initialize super @subscribers = [] end |
Instance Method Details
#each(&block) ⇒ Object
Iterate over subscribers
22 23 24 |
# File 'lib/finite_machine/subscribers.rb', line 22 def each(&block) @subscribers.each(&block) end |
#empty? ⇒ Boolean
Check if anyone is subscribed
38 39 40 |
# File 'lib/finite_machine/subscribers.rb', line 38 def empty? @subscribers.empty? end |
#index(subscriber) ⇒ Object
Return index of the subscriber
29 30 31 |
# File 'lib/finite_machine/subscribers.rb', line 29 def index(subscriber) @subscribers.index(subscriber) end |
#reset ⇒ self
Reset subscribers
83 84 85 86 |
# File 'lib/finite_machine/subscribers.rb', line 83 def reset @subscribers.clear self end |
#size ⇒ Integer
Number of subscribed listeners
74 75 76 |
# File 'lib/finite_machine/subscribers.rb', line 74 def size synchronize { @subscribers.size } end |
#subscribe(*observers) ⇒ undefined
Add listener to subscribers
49 50 51 52 53 |
# File 'lib/finite_machine/subscribers.rb', line 49 def subscribe(*observers) synchronize do observers.each { |observer| @subscribers << observer } end end |
#visit(hook_event, *data) ⇒ undefined
Visit subscribers and notify
63 64 65 66 67 |
# File 'lib/finite_machine/subscribers.rb', line 63 def visit(hook_event, *data) each { |subscriber| synchronize { hook_event.notify(subscriber, *data) } } end |