Class: EventStream::Stream
- Inherits:
-
Object
- Object
- EventStream::Stream
- Defined in:
- lib/event_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, 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.
23 24 25 |
# File 'lib/event_stream.rb', line 23 def initialize @subscribers = [] end |
Instance Method Details
#add_subscriber(subscriber) ⇒ Object
Adds a subscriber to this stream
58 59 60 |
# File 'lib/event_stream.rb', line 58 def add_subscriber(subscriber) @subscribers << subscriber end |
#clear_subscribers ⇒ Object
Clears all subscribers from this event stream.
46 47 48 |
# File 'lib/event_stream.rb', line 46 def clear_subscribers @subscribers = [] end |
#publish(name, attrs = {}) ⇒ Object
Publishes an event to this event stream
30 31 32 33 |
# File 'lib/event_stream.rb', line 30 def publish(name, attrs = {}) e = Event.new(attrs.merge(:name => name)) @subscribers.each { |l| l.consume(e) } 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.
41 42 43 |
# File 'lib/event_stream.rb', line 41 def subscribe(filter = nil, &action) add_subscriber(Subscriber.create(filter, &action)) end |
#subscribers ⇒ Array<EventStream::Subscriber]
Returns all subscribers for this stream
52 53 54 |
# File 'lib/event_stream.rb', line 52 def subscribers @subscribers end |