Class: Cinch::HandlerList
- Inherits:
-
Object
- Object
- Cinch::HandlerList
- Includes:
- Enumerable
- Defined in:
- lib/cinch/handler_list.rb
Overview
Instance Method Summary collapse
-
#dispatch(event, msg = nil, *arguments) ⇒ Array<Thread>
-
#each {|handler| ... }
-
#find(type, msg = nil) ⇒ Array<Handler>
private
-
#initialize ⇒ HandlerList
constructor
A new instance of HandlerList.
-
#register(handler) ⇒ Object
-
#stop_all ⇒ Object
private
-
#unregister(*handlers)
Constructor Details
#initialize ⇒ HandlerList
Returns a new instance of HandlerList.
11 12 13 14 |
# File 'lib/cinch/handler_list.rb', line 11 def initialize @handlers = Hash.new { |h, k| h[k] = [] } @mutex = Mutex.new end |
Instance Method Details
#dispatch(event, msg = nil, *arguments) ⇒ Array<Thread>
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/cinch/handler_list.rb', line 54 def dispatch(event, msg = nil, *arguments) threads = [] if handlers = find(event, msg) already_run = Set.new handlers.each do |handler| next if already_run.include?(handler.block) already_run << handler.block # calling Message#match multiple times is not a problem # because we cache the result captures = if msg msg.match(handler.pattern.to_r(msg), event, handler.strip_colors).captures else [] end threads << handler.call(msg, captures, arguments) end end threads end |
#each {|handler| ... }
This method returns an undefined value.
81 82 83 |
# File 'lib/cinch/handler_list.rb', line 81 def each(&block) @handlers.values.flatten.each(&block) end |
#find(type, msg = nil) ⇒ Array<Handler>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cinch/handler_list.rb', line 36 def find(type, msg = nil) if handlers = @handlers[type] return handlers if msg.nil? handlers = handlers.select do |handler| msg.match(handler.pattern.to_r(msg), type, handler.strip_colors) end.group_by(&:group) handlers.values_at(*(handlers.keys - [nil])).map(&:first) + (handlers[nil] || []) end end |
#register(handler) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/cinch/handler_list.rb', line 16 def register(handler) @mutex.synchronize do handler.bot.loggers.debug "[on handler] Registering handler with pattern `#{handler.pattern.inspect}`, reacting on `#{handler.event}`" @handlers[handler.event] << handler end end |
#stop_all ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
86 87 88 |
# File 'lib/cinch/handler_list.rb', line 86 def stop_all each(&:stop) end |
#unregister(*handlers)
This method returns an undefined value.
26 27 28 29 30 31 32 |
# File 'lib/cinch/handler_list.rb', line 26 def unregister(*handlers) @mutex.synchronize do handlers.each do |handler| @handlers[handler.event].delete(handler) end end end |