class EventStream::Stream

Public Class Methods

new() click to toggle source
# File lib/event_stream/stream.rb, line 3
def initialize
  @subscribers = []
end

Public Instance Methods

add_subscriber(subscriber) click to toggle source

Adds a subscriber to this stream @param [EventStream::Subscriber]

# File lib/event_stream/stream.rb, line 41
def add_subscriber(subscriber)
  @subscribers << subscriber
end
clear_subscribers() click to toggle source

Clears all subscribers from this event stream.

# File lib/event_stream/stream.rb, line 29
def clear_subscribers
  @subscribers = []
end
publish(name_or_event, attrs = {}) click to toggle source

Publishes an event to this event stream @param name [Symbol] name of this event @param attrs [Hash] optional attributes representing this event

# 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, &action) click to toggle source

Registers a subscriber to this event stream. @param filter [Object] Filters which events this subscriber will consume. 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. @yield [Event] action to perform when the event occurs.

# File lib/event_stream/stream.rb, line 24
def subscribe(filter = nil, &action)
  add_subscriber(Subscriber.create(filter, &action))
end
subscribers() click to toggle source

Returns all subscribers for this stream @return [Array<EventStream::Subscriber]

# File lib/event_stream/stream.rb, line 35
def subscribers
  @subscribers
end