Class: Cakewalk::Handler
- Inherits:
-
Object
- Object
- Cakewalk::Handler
- Defined in:
- lib/cakewalk/handler.rb
Overview
Instance Attribute Summary collapse
-
#args ⇒ Array
readonly
-
#block ⇒ Proc
readonly
-
#bot ⇒ Bot
readonly
-
#event ⇒ Symbol
readonly
-
#group ⇒ Symbol
readonly
-
#pattern ⇒ Pattern
readonly
-
#strip_colors ⇒ Boolean
readonly
-
#thread_group ⇒ ThreadGroup
readonly
private
Instance Method Summary collapse
-
#call(message, captures, arguments) ⇒ Thread
Executes the handler.
-
#initialize(bot, event, pattern, options = {}, &block) ⇒ Handler
constructor
A new instance of Handler.
-
#stop
Stops execution of the handler.
-
#to_s ⇒ String
-
#unregister
Unregisters the handler.
Constructor Details
#initialize(bot, event, pattern, options = {}, &block) ⇒ Handler
Returns a new instance of Handler.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/cakewalk/handler.rb', line 41 def initialize(bot, event, pattern, = {}, &block) = { :group => nil, :execute_in_callback => false, :strip_colors => false, :args => [] }.merge() @bot = bot @event = event @pattern = pattern @group = [:group] @execute_in_callback = [:execute_in_callback] @strip_colors = [:strip_colors] @args = [:args] @block = block @thread_group = ThreadGroup.new end |
Instance Attribute Details
#args ⇒ Array (readonly)
14 15 16 |
# File 'lib/cakewalk/handler.rb', line 14 def args @args end |
#block ⇒ Proc (readonly)
17 18 19 |
# File 'lib/cakewalk/handler.rb', line 17 def block @block end |
#event ⇒ Symbol (readonly)
8 9 10 |
# File 'lib/cakewalk/handler.rb', line 8 def event @event end |
#group ⇒ Symbol (readonly)
20 21 22 |
# File 'lib/cakewalk/handler.rb', line 20 def group @group end |
#pattern ⇒ Pattern (readonly)
11 12 13 |
# File 'lib/cakewalk/handler.rb', line 11 def pattern @pattern end |
#strip_colors ⇒ Boolean (readonly)
23 24 25 |
# File 'lib/cakewalk/handler.rb', line 23 def strip_colors @strip_colors end |
#thread_group ⇒ ThreadGroup (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.
27 28 29 |
# File 'lib/cakewalk/handler.rb', line 27 def thread_group @thread_group end |
Instance Method Details
#call(message, captures, arguments) ⇒ Thread
Executes the handler.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/cakewalk/handler.rb', line 89 def call(, captures, arguments) = 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(, *@args, *, &@block) else @block.call(, *@args, *) 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 |
#stop
This method returns an undefined value.
Stops execution of the handler. This means stopping and killing all associated threads.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/cakewalk/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 |
#to_s ⇒ String
113 114 115 116 |
# File 'lib/cakewalk/handler.rb', line 113 def to_s # TODO maybe add the number of running threads to the output? "#<Cakewalk::Handler @event=#{@event.inspect} pattern=#{@pattern.inspect}>" end |
#unregister
This method returns an undefined value.
Unregisters the handler.
63 64 65 |
# File 'lib/cakewalk/handler.rb', line 63 def unregister @bot.handlers.unregister(self) end |