Class: EventStream::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/event_stream/stream.rb

Instance Method Summary collapse

Constructor Details

#initializeStream

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

Parameters:



41
42
43
# File 'lib/event_stream/stream.rb', line 41

def add_subscriber(subscriber)
  @subscribers << subscriber
end

#clear_subscribersObject

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

Parameters:

  • name (Symbol)

    name of this event

  • attrs (Hash) (defaults to: {})

    optional attributes representing this event



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.

Parameters:

  • filter (Object) (defaults to: nil)

    Filters which events this subscriber will consume.

Yields:

  • (Event)

    action to perform when the event occurs.



24
25
26
# File 'lib/event_stream/stream.rb', line 24

def subscribe(filter = nil, &action)
  add_subscriber(Subscriber.create(filter, &action))
end

#subscribersArray<EventStream::Subscriber]

Returns all subscribers for this stream

Returns:



35
36
37
# File 'lib/event_stream/stream.rb', line 35

def subscribers
  @subscribers
end