class Cinch::Handler
@since 2.0.0
Attributes
@return [Array]
@return [Proc]
@return [Bot]
@return [Symbol]
@return [Symbol]
@return [Pattern]
@return [Boolean]
@return [ThreadGroup] @api private
Public Class Methods
@param [Bot] bot @param [Symbol] event @param [Pattern] pattern @param [Hash] options @option options [Symbol] :group (nil) Match group the h belongs
to.
@option options [Boolean] :execute_in_callback (false) Whether
to execute the handler in an instance of {Callback}
@option options [Boolean] :strip_colors (false) Strip colors
from message before attemping match
@option options [Array] :args ([]) Additional arguments to pass
to the block
# File lib/cinch/handler.rb, line 41 def initialize(bot, event, pattern, options = {}, &block) options = { :group => nil, :execute_in_callback => false, :strip_colors => false, :args => [] }.merge(options) @bot = bot @event = event @pattern = pattern @group = options[:group] @execute_in_callback = options[:execute_in_callback] @strip_colors = options[:strip_colors] @args = options[:args] @block = block @thread_group = ThreadGroup.new end
Public Instance Methods
Executes the handler.
@param [Message] message Message that caused the invocation @param [Array] captures Capture groups of the pattern that are
being passed as arguments
@return [Thread]
# File lib/cinch/handler.rb, line 89 def call(message, captures, arguments) bargs = captures + arguments thread = 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(message, *@args, *bargs, &@block) else @block.call(message, *@args, *bargs) end rescue => e @bot.loggers.exception(e) ensure @bot.loggers.debug "[Thread done] For #{self}: #{Thread.current} -- #{@thread_group.list.size - 1} remaining." end } @thread_group.add(thread) thread end
Stops execution of the handler. This means stopping and killing all associated threads.
@return [void]
# File lib/cinch/handler.rb, line 71 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
@return [String]
# File lib/cinch/handler.rb, line 113 def to_s # TODO maybe add the number of running threads to the output? "#<Cinch::Handler @event=#{@event.inspect} pattern=#{@pattern.inspect}>" end
Unregisters the handler.
@return [void]
# File lib/cinch/handler.rb, line 63 def unregister @bot.handlers.unregister(self) end