Parent

Included Modules

Class/Module Index [+]

Quicksearch

Cinch::HandlerList

@since 2.0.0

Public Class Methods

new() click to toggle source
# File lib/cinch/handler_list.rb, line 10
def initialize
  @handlers = Hash.new {|h,k| h[k] = []}
  @mutex = Mutex.new
end

Public Instance Methods

dispatch(event, msg = nil, *arguments) click to toggle source

@param [Symbol] event The event type @param [Message, nil] msg The message which is responsible for

and attached to the event, or nil.

@param [Array] *arguments A list of additional arguments to pass

to event handlers

@return [void]

# File lib/cinch/handler_list.rb, line 55
def dispatch(event, msg = nil, *arguments)
  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
      if msg
        captures = msg.match(handler.pattern.to_r(msg), event).captures
      else
        captures = []
      end

      handler.call(msg, captures, arguments)
    end
  end
end
each(&block) click to toggle source

@yield [handler] Yields all registered handlers @yieldparam [Handler] handler @return [void]

# File lib/cinch/handler_list.rb, line 77
def each(&block)
  @handlers.values.flatten.each(&block)
end
find(type, msg = nil) click to toggle source

@api private @return [Array<Handler>]

# File lib/cinch/handler_list.rb, line 35
def find(type, msg = nil)
  if handlers = @handlers[type]
    if msg.nil?
      return handlers
    end

    handlers = handlers.select { |handler|
      msg.match(handler.pattern.to_r(msg), type)
    }.group_by {|handler| handler.group}

    handlers.values_at(*(handlers.keys - [nil])).map(&:first) + (handlers[nil] || [])
  end
end
register(handler) click to toggle source
# File lib/cinch/handler_list.rb, line 15
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() click to toggle source

@api private

# File lib/cinch/handler_list.rb, line 82
def stop_all
  each { |h| h.stop }
end
unregister(*handlers) click to toggle source

@param [Handler, Array<Handler>] *handlers The handlers to unregister @return [void] @see Handler#unregister

# File lib/cinch/handler_list.rb, line 25
def unregister(*handlers)
  @mutex.synchronize do
    handlers.each do |handler|
      @handlers[handler.event].delete(handler)
    end
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.