Class: EventStream::Subscriber

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actionObject

Returns the value of attribute action

Returns:

  • (Object)

    the current value of action



2
3
4
# File 'lib/event_stream/subscriber.rb', line 2

def action
  @action
end

#filterObject

Returns the value of attribute filter

Returns:

  • (Object)

    the current value of filter



2
3
4
# File 'lib/event_stream/subscriber.rb', line 2

def filter
  @filter
end

Class Method Details

.create(filter = nil, &action) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/event_stream/subscriber.rb', line 3

def self.create(filter = nil, &action)
  filter ||= lambda { |e| true }
  filter_predicate = case filter
                     when Symbol, String then lambda { |e| e.name.to_s == filter.to_s }
                     when Regexp then lambda { |e| e.name =~ filter }
                     when Hash then lambda { |e| filter.all? { |k,v| e[k] === v } }
                     else filter
                     end
  new(filter_predicate, action)
end

Instance Method Details

#consume(event) ⇒ Object



14
15
16
# File 'lib/event_stream/subscriber.rb', line 14

def consume(event)
  action.call(event) if filter.call(event)
end