Parent

Class/Module Index [+]

Quicksearch

Cinch::Handler

@since 2.0.0

Attributes

args[R]

@return [Array]

block[R]

@return [Proc]

bot[R]

@return [Bot]

event[R]

@return [Symbol]

group[R]

@return [Symbol]

pattern[R]

@return [Pattern]

thread_group[R]

@return [ThreadGroup] @api private

Public Class Methods

new(bot, event, pattern, options = {}, &block) click to toggle source

@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 [Array] :args ([]) Additional arguments to pass

to the block
# File lib/cinch/handler.rb, line 36
def initialize(bot, event, pattern, options = {}, &block)
  options              = {:group => nil, :execute_in_callback => false, :args => []}.merge(options)
  @bot                 = bot
  @event               = event
  @pattern             = pattern
  @group               = options[:group]
  @execute_in_callback = options[:execute_in_callback]
  @args                = options[:args]
  @block               = block

  @thread_group = ThreadGroup.new
end

Public Instance Methods

call(message, captures, arguments) click to toggle source

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 [void]

# File lib/cinch/handler.rb, line 78
def call(message, captures, arguments)
  bargs = 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(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
  }
end
stop() click to toggle source

Stops execution of the handler. This means stopping and killing all associated threads.

@return [void]

# 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
to_s() click to toggle source

@return [String]

# 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() click to toggle source

Unregisters the handler.

@return [void]

# File lib/cinch/handler.rb, line 52
def unregister
  @bot.handlers.unregister(self)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.