Class: Cinch::Handler
- Inherits:
-
Object
- Object
- Cinch::Handler
- Defined in:
- lib/cinch/handler.rb
Overview
Instance Attribute Summary (collapse)
-
- (Array) args
readonly
-
- (Proc) block
readonly
-
- (Bot) bot
readonly
-
- (Symbol) event
readonly
-
- (Symbol) group
readonly
-
- (Pattern) pattern
readonly
-
- (ThreadGroup) thread_group
readonly
private
Instance Method Summary (collapse)
-
- call(message, captures, arguments)
Executes the handler.
-
- (Handler) initialize(bot, event, pattern, options = {}, &block)
constructor
A new instance of Handler.
-
- stop
Stops execution of the handler.
-
- (String) to_s
-
- unregister
Unregisters the handler.
Constructor Details
- (Handler) initialize(bot, event, pattern, options = {}, &block)
A new instance of Handler
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/cinch/handler.rb', line 36 def initialize(bot, event, pattern, = {}, &block) = {:group => nil, :execute_in_callback => false, :args => []}.merge() @bot = bot @event = event @pattern = pattern @group = [:group] @execute_in_callback = [:execute_in_callback] @args = [:args] @block = block @thread_group = ThreadGroup.new end |
Instance Attribute Details
- (Array) args (readonly)
14 15 16 |
# File 'lib/cinch/handler.rb', line 14 def args @args end |
- (Proc) block (readonly)
17 18 19 |
# File 'lib/cinch/handler.rb', line 17 def block @block end |
- (Symbol) event (readonly)
8 9 10 |
# File 'lib/cinch/handler.rb', line 8 def event @event end |
- (Symbol) group (readonly)
20 21 22 |
# File 'lib/cinch/handler.rb', line 20 def group @group end |
- (Pattern) pattern (readonly)
11 12 13 |
# File 'lib/cinch/handler.rb', line 11 def pattern @pattern end |
- (ThreadGroup) thread_group (readonly)
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.
24 25 26 |
# File 'lib/cinch/handler.rb', line 24 def thread_group @thread_group end |
Instance Method Details
- call(message, captures, arguments)
This method returns an undefined value.
Executes the handler.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/cinch/handler.rb', line 78 def call(, captures, arguments) = captures + arguments @thread_group.add Thread.new { @bot.loggers.debug "[New thread] For #{self}: #{Thread.current} -- #{@thread_group.list.size} in total." begin if @execute_in_callback @bot.callback.instance_exec(, *@args, *, &@block) else @block.call(, *@args, *) end rescue => e @bot.loggers.exception(e) ensure @bot.loggers.debug "[Thread done] For #{self}: #{Thread.current} -- #{@thread_group.list.size - 1} remaining." end } end |
- stop
This method returns an undefined value.
Stops execution of the handler. This means stopping and killing all associated threads.
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/cinch/handler.rb', line 60 def stop @bot.loggers.debug "[Stopping handler] Stopping all threads of handler #{self}: #{@thread_group.list.size} threads..." @thread_group.list.each do |thread| Thread.new do @bot.loggers.debug "[Ending thread] Waiting 10 seconds for #{thread} to finish..." thread.join(10) @bot.loggers.debug "[Killing thread] Killing #{thread}" thread.kill end end end |
- (String) to_s
99 100 101 102 |
# File 'lib/cinch/handler.rb', line 99 def to_s # TODO maybe add the number of running threads to the output? "#<Cinch::Handler @event=#{@event.inspect} pattern=#{@pattern.inspect}>" end |
- unregister
This method returns an undefined value.
Unregisters the handler.
52 53 54 |
# File 'lib/cinch/handler.rb', line 52 def unregister @bot.handlers.unregister(self) end |