Class: Cinch::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/cinch/handler.rb

Overview

Since:

  • 2.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot, event, pattern, options = {}, &block) ⇒ Handler

Returns a new instance of Handler.

Parameters:

  • bot (Bot)
  • event (Symbol)
  • pattern (Pattern)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :group (Symbol) — default: nil

    Match group the h belongs to.

  • :execute_in_callback (Boolean) — default: false

    Whether to execute the handler in an instance of Callback

  • :strip_colors (Boolean) — default: false

    Strip colors from message before attemping match

  • :args (Array) — default: []

    Additional arguments to pass to the block

Since:

  • 2.0.0



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cinch/handler.rb', line 43

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

Instance Attribute Details

#argsArray (readonly)

Returns:

  • (Array)

Since:

  • 2.0.0



16
17
18
# File 'lib/cinch/handler.rb', line 16

def args
  @args
end

#blockProc (readonly)

Returns:

  • (Proc)

Since:

  • 2.0.0



19
20
21
# File 'lib/cinch/handler.rb', line 19

def block
  @block
end

#botBot (readonly)

Returns:

Since:

  • 2.0.0



7
8
9
# File 'lib/cinch/handler.rb', line 7

def bot
  @bot
end

#eventSymbol (readonly)

Returns:

  • (Symbol)

Since:

  • 2.0.0



10
11
12
# File 'lib/cinch/handler.rb', line 10

def event
  @event
end

#groupSymbol (readonly)

Returns:

  • (Symbol)

Since:

  • 2.0.0



22
23
24
# File 'lib/cinch/handler.rb', line 22

def group
  @group
end

#patternPattern (readonly)

Returns:

Since:

  • 2.0.0



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

def pattern
  @pattern
end

#strip_colorsBoolean (readonly)

Returns:

  • (Boolean)

Since:

  • 2.0.0



25
26
27
# File 'lib/cinch/handler.rb', line 25

def strip_colors
  @strip_colors
end

#thread_groupThreadGroup (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.

Returns:

  • (ThreadGroup)

Since:

  • 2.0.0



29
30
31
# File 'lib/cinch/handler.rb', line 29

def thread_group
  @thread_group
end

Instance Method Details

#call(message, captures, arguments) ⇒ Thread

Executes the handler.

Parameters:

  • message (Message)

    Message that caused the invocation

  • captures (Array)

    Capture groups of the pattern that are being passed as arguments

Returns:

  • (Thread)

Since:

  • 2.0.0



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cinch/handler.rb', line 91

def call(message, captures, arguments)
  bargs = captures + arguments

  thread = Thread.new do
    @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 StandardError => e
      @bot.loggers.exception(e)
    ensure
      @bot.loggers.debug "[Thread done] For #{self}: #{Thread.current} -- #{@thread_group.list.size - 1} remaining."
    end
  end

  @thread_group.add(thread)
  thread
end

#stop

This method returns an undefined value.

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

Since:

  • 2.0.0



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cinch/handler.rb', line 73

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_sString

Returns:

Since:

  • 2.0.0



115
116
117
118
# File 'lib/cinch/handler.rb', line 115

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.

Since:

  • 2.0.0



65
66
67
# File 'lib/cinch/handler.rb', line 65

def unregister
  @bot.handlers.unregister(self)
end