Class: Alondra::EventListener
- Inherits:
-
Object
- Object
- Alondra::EventListener
- Includes:
- Pushing
- Defined in:
- lib/alondra/event_listener.rb
Instance Attribute Summary collapse
-
#channel_name ⇒ Object
Returns the value of attribute channel_name.
-
#event ⇒ Object
Returns the value of attribute event.
-
#resource ⇒ Object
Returns the value of attribute resource.
Class Method Summary collapse
- .callbacks ⇒ Object
- .default_listened_pattern ⇒ Object
- .inherited(subclass) ⇒ Object
- .listen_to(channel_name) ⇒ Object
- .listen_to?(channel_name) ⇒ Boolean
- .listened_patterns ⇒ Object
- .matching_callbacks_for(event) ⇒ Object
- .on(event_type, options = {}, &block) ⇒ Object
- .process(event) ⇒ Object
Instance Method Summary collapse
-
#initialize(event) ⇒ EventListener
constructor
A new instance of EventListener.
- #session ⇒ Object
Methods included from Pushing
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_name ⇒ Object
Returns the value of attribute channel_name.
7 8 9 |
# File 'lib/alondra/event_listener.rb', line 7 def channel_name @channel_name end |
#event ⇒ Object
Returns the value of attribute event.
5 6 7 |
# File 'lib/alondra/event_listener.rb', line 5 def event @event end |
#resource ⇒ Object
Returns the value of attribute resource.
6 7 8 |
# File 'lib/alondra/event_listener.rb', line 6 def resource @resource end |
Class Method Details
.callbacks ⇒ Object
36 37 38 |
# File 'lib/alondra/event_listener.rb', line 36 def callbacks @callbacks ||= [] end |
.default_listened_pattern ⇒ Object
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
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_patterns ⇒ Object
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, = {}, &block) callbacks << ListenerCallback.new(event_type, , 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. Log.error ex.backtrace.join("\n") end end end |
Instance Method Details
#session ⇒ Object
75 76 77 |
# File 'lib/alondra/event_listener.rb', line 75 def session @connection.session end |