Class: Alondra::EventListener

Inherits:
Object
  • Object
show all
Includes:
Pushing
Defined in:
lib/alondra/event_listener.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Pushing

#push

Constructor Details

#initialize(event) ⇒ EventListener

Returns a new instance of EventListener.



79
80
81
82
83
84
# File 'lib/alondra/event_listener.rb', line 79

def initialize(event)
  @event        = event
  @resource     = event.resource
  @channel_name = event.channel_name
  @connection   = event.connection
end

Instance Attribute Details

#channel_nameObject

Returns the value of attribute channel_name.



7
8
9
# File 'lib/alondra/event_listener.rb', line 7

def channel_name
  @channel_name
end

#eventObject

Returns the value of attribute event.



5
6
7
# File 'lib/alondra/event_listener.rb', line 5

def event
  @event
end

#resourceObject

Returns the value of attribute resource.



6
7
8
# File 'lib/alondra/event_listener.rb', line 6

def resource
  @resource
end

Class Method Details

.callbacksObject



36
37
38
# File 'lib/alondra/event_listener.rb', line 36

def callbacks
  @callbacks ||= []
end

.default_listened_patternObject



40
41
42
43
44
45
46
47
48
# File 'lib/alondra/event_listener.rb', line 40

def default_listened_pattern
  word = self.name.demodulize
  word.gsub!(/Listener$/, '')
  word.gsub!(/::/, '/')
  word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1\/\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1/\2')
  word.downcase!
  Regexp.new("^/#{word}")
end

.inherited(subclass) ⇒ Object



50
51
52
53
54
55
# File 'lib/alondra/event_listener.rb', line 50

def inherited(subclass)
  # In development mode Rails will load the same class many times
  # Delete it first if we already have parsed it
  EventRouter.listeners.delete_if { |l| l.name == subclass.name }
  EventRouter.listeners << subclass
end

.listen_to(channel_name) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/alondra/event_listener.rb', line 18

def listen_to(channel_name)
  unless @custom_pattern_provided
    listened_patterns.clear
    @custom_pattern_provided = true
  end

  if Regexp === channel_name
    listened_patterns << channel_name
  else
    escaped_pattern = Regexp.escape(channel_name)
    listened_patterns << Regexp.new("^#{escaped_pattern}")
  end
end

.listen_to?(channel_name) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/alondra/event_listener.rb', line 14

def listen_to?(channel_name)
  listened_patterns.any? { |p| p =~ channel_name }
end

.listened_patternsObject



10
11
12
# File 'lib/alondra/event_listener.rb', line 10

def listened_patterns
  @listened_patterns ||= [default_listened_pattern]
end

.matching_callbacks_for(event) ⇒ Object



57
58
59
# File 'lib/alondra/event_listener.rb', line 57

def matching_callbacks_for(event)
  callbacks.find_all { |c| c.matches?(event) }
end

.on(event_type, options = {}, &block) ⇒ Object



32
33
34
# File 'lib/alondra/event_listener.rb', line 32

def on(event_type, options = {}, &block)
  callbacks << ListenerCallback.new(event_type, options, block)
end

.process(event) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/alondra/event_listener.rb', line 61

def process(event)
  matching_callbacks_for(event).each do |callback|
    new_instance = new(event)
    begin
      new_instance.instance_exec(event, &callback.proc)
    rescue Exception => ex
      Log.error 'Error while processing event listener callback'
      Log.error ex.message
      Log.error ex.backtrace.join("\n")
    end
  end
end

Instance Method Details

#sessionObject



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

def session
  @connection.session
end