Class: EventStream::Stream

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

Instance Method Summary collapse

Constructor Details

#initializeStream

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

Parameters:



58
59
60
# File 'lib/event_stream.rb', line 58

def add_subscriber(subscriber)
  @subscribers << subscriber
end

#clear_subscribersObject

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

Parameters:

  • name of this event

  • (defaults to: {})

    optional attributes representing this event



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.

Yields:

  • (Event)

    action to perform when the event occurs.

Parameters:

  • (defaults to: nil)

    Filters which events this subscriber will consume.



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

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

#subscribersArray<EventStream::Subscriber]

Returns all subscribers for this stream

Returns:

  • Array<EventStream::Subscriber]



52
53
54
# File 'lib/event_stream.rb', line 52

def subscribers
  @subscribers
end