Class: Cinch::HandlerList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/cinch/handler_list.rb

Overview

Since:

  • 2.0.0

Instance Method Summary collapse

Constructor Details

#initializeHandlerList

Returns a new instance of HandlerList.

Since:

  • 2.0.0



12
13
14
15
# File 'lib/cinch/handler_list.rb', line 12

def initialize
  @handlers = Hash.new { |h, k| h[k] = [] }
  @mutex = Mutex.new
end

Instance Method Details

#dispatch(event, msg = nil, *arguments) ⇒ Array<Thread>

Parameters:

  • event (Symbol)

    The event type

  • msg (Message, nil) (defaults to: nil)

    The message which is responsible for and attached to the event, or nil.

  • arguments (Array)

    A list of additional arguments to pass to event handlers

Returns:

  • (Array<Thread>)

Since:

  • 2.0.0



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cinch/handler_list.rb', line 57

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| ... } ⇒ void

This method returns an undefined value.

Yields:

  • (handler)

    Yields all registered handlers

Yield Parameters:

Since:

  • 2.0.0



83
84
85
# File 'lib/cinch/handler_list.rb', line 83

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.

Returns:

Since:

  • 2.0.0



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cinch/handler_list.rb', line 37

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, handler.strip_colors)
    }.group_by { |handler| handler.group }

    handlers.values_at(*(handlers.keys - [nil])).map(&:first) + (handlers[nil] || [])
  end
end

#register(handler) ⇒ Object

Since:

  • 2.0.0



17
18
19
20
21
22
# File 'lib/cinch/handler_list.rb', line 17

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_allObject

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.

Since:

  • 2.0.0



88
89
90
# File 'lib/cinch/handler_list.rb', line 88

def stop_all
  each { |h| h.stop }
end

#unregister(*handlers) ⇒ void

This method returns an undefined value.

Parameters:

See Also:

Since:

  • 2.0.0



27
28
29
30
31
32
33
# File 'lib/cinch/handler_list.rb', line 27

def unregister(*handlers)
  @mutex.synchronize do
    handlers.each do |handler|
      @handlers[handler.event].delete(handler)
    end
  end
end