Class: EventStream::Subscriber

Inherits:
Struct
  • Object
show all
Defined in:
lib/event_stream.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



63
64
65
# File 'lib/event_stream.rb', line 63

def action
  @action
end

#filterObject

Returns the value of attribute filter

Returns:

  • (Object)

    the current value of filter



63
64
65
# File 'lib/event_stream.rb', line 63

def filter
  @filter
end

Class Method Details

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



64
65
66
67
68
69
70
71
72
73
# File 'lib/event_stream.rb', line 64

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



75
76
77
# File 'lib/event_stream.rb', line 75

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