Class: EventStream::Stream
- Inherits:
-
Object
- Object
- EventStream::Stream
- Defined in:
- lib/event_stream/stream.rb
Instance Method Summary collapse
-
#add_subscriber(subscriber) ⇒ Object
Adds a subscriber to this stream.
-
#clear_subscribers ⇒ Object
Clears all subscribers from this event stream.
-
#initialize ⇒ Stream
constructor
A new instance of Stream.
-
#publish(name_or_event, attrs = {}) ⇒ Object
Publishes an event to this event stream.
-
#subscribe(filter = nil) {|Event| ... } ⇒ Object
Registers a subscriber to this event stream.
-
#subscribers ⇒ Array<EventStream::Subscriber]
Returns all subscribers for this stream.
Constructor Details
#initialize ⇒ Stream
Returns a new instance of Stream.
3 4 5 |
# File 'lib/event_stream/stream.rb', line 3 def initialize @subscribers = [] end |
Instance Method Details
#add_subscriber(subscriber) ⇒ Object
Adds a subscriber to this stream
41 42 43 |
# File 'lib/event_stream/stream.rb', line 41 def add_subscriber(subscriber) @subscribers << subscriber end |
#clear_subscribers ⇒ Object
Clears all subscribers from this event stream.
29 30 31 |
# File 'lib/event_stream/stream.rb', line 29 def clear_subscribers @subscribers = [] end |
#publish(name_or_event, attrs = {}) ⇒ Object
Publishes an event to this event stream
10 11 12 13 14 15 16 |
# File 'lib/event_stream/stream.rb', line 10 def publish(name_or_event, attrs = {}) event = case name_or_event when Event then name_or_event else Event.new(attrs.merge(:name => name_or_event)) end @subscribers.each { |l| l.consume(event) } end |
#subscribe(filter = nil) {|Event| ... } ⇒ Object
Registers a subscriber to this event stream. If a string or regexp is provided, these will be matched against the event name. A hash will be matched against the attributes of the event. Or, any arbitrary predicate on events may be provided.
24 25 26 |
# File 'lib/event_stream/stream.rb', line 24 def subscribe(filter = nil, &action) add_subscriber(Subscriber.create(filter, &action)) end |
#subscribers ⇒ Array<EventStream::Subscriber]
Returns all subscribers for this stream
35 36 37 |
# File 'lib/event_stream/stream.rb', line 35 def subscribers @subscribers end |