Skip to content
This repository

Allow hooks for specific matcher groups #144

Merged
merged 2 commits into from 9 months ago

2 participants

Curtis McEnroe Dominik Honnef
Curtis McEnroe

Fixes #143.

Hooks without groups are always executed. Matchers execute the global hooks (the ones lacking groups) along with the hooks in their group.

lib/cinch/plugin.rb
@@ -282,14 +283,20 @@ def __hooks(type = nil, events = nil)
282 283 if hooks.is_a?(Hash)
283 284 hooks = hooks.map { |k, v| v }
284 285 end
285   - return hooks.select { |hook| (events & hook.for).size > 0 }
  286 + hooks.select! { |hook| (events & hook.for).size > 0 }
  287 + end
  288 +
  289 + if group.nil?
  290 + return hooks.select { |hook| hook.group.nil? }
  291 + else
  292 + return hooks.select { |hook| hook.group.nil? || hook.group == group }
1
Dominik Honnef Owner
dominikh added a note

This whole if/else can be unconditionally reduced to the body of the else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Dominik Honnef
Owner

Looks good to me, apart from the style comment. Will test and then merge.

Dominik Honnef dominikh merged commit c937c1a into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Oct 04, 2013
Curtis McEnroe programble Allow hooks for specific matcher groups
Fixes #143.
92cdce7
Curtis McEnroe programble Simplify hook selection logic c937c1a
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 16 additions and 13 deletions. Show diff stats Hide diff stats

  1. +16 13 lib/cinch/plugin.rb
29 lib/cinch/plugin.rb
@@ -107,7 +107,7 @@ def plugin_name=(new_name)
107 107 # @attr [Symbol] type
108 108 # @attr [Array<Symbol>] for
109 109 # @attr [Symbol] method
110   - Hook = Struct.new(:type, :for, :method)
  110 + Hook = Struct.new(:type, :for, :method, :group)
111 111
112 112 # @api private
113 113 def self.extended(by)
@@ -256,11 +256,12 @@ def timer(interval, options = {})
256 256 # @option options [Array<:match, :listen_to, :ctcp>] :for ([:match, :listen_to, :ctcp])
257 257 # Which kinds of events to run the hook for.
258 258 # @option options [Symbol] :method (:hook) The method to execute.
  259 + # @option options [Symbol] :group (nil) The matcher group to execute the hook for.
259 260 # @return [Hook]
260 261 # @since 1.1.0
261 262 def hook(type, options = {})
262   - options = {:for => [:match, :listen_to, :ctcp], :method => :hook}.merge(options)
263   - hook = Hook.new(type, options[:for], options[:method])
  263 + options = {:for => [:match, :listen_to, :ctcp], :method => :hook, :group => nil}.merge(options)
  264 + hook = Hook.new(type, options[:for], options[:method], options[:group])
264 265 __hooks(type) << hook
265 266
266 267 hook
@@ -268,7 +269,7 @@ def hook(type, options = {})
268 269
269 270 # @return [Hash]
270 271 # @api private
271   - def __hooks(type = nil, events = nil)
  272 + def __hooks(type = nil, events = nil, group = nil)
272 273 if type.nil?
273 274 hooks = @hooks
274 275 else
@@ -282,14 +283,16 @@ def __hooks(type = nil, events = nil)
282 283 if hooks.is_a?(Hash)
283 284 hooks = hooks.map { |k, v| v }
284 285 end
285   - return hooks.select { |hook| (events & hook.for).size > 0 }
  286 + hooks.select! { |hook| (events & hook.for).size > 0 }
286 287 end
  288 +
  289 + return hooks.select { |hook| hook.group.nil? || hook.group == group }
287 290 end
288 291
289 292 # @return [Boolean] True if processing should continue
290 293 # @api private
291   - def call_hooks(type, event, instance, args)
292   - stop = __hooks(type, event).find { |hook|
  294 + def call_hooks(type, event, group, instance, args)
  295 + stop = __hooks(type, event, group).find { |hook|
293 296 # stop after the first hook that returns false
294 297 if hook.method.respond_to?(:call)
295 298 instance.instance_exec(*args, &hook.method) == false
@@ -316,9 +319,9 @@ def __register_listeners
316 319 self.class.listeners.each do |listener|
317 320 @bot.loggers.debug "[plugin] #{self.class.plugin_name}: Registering listener for type `#{listener.event}`"
318 321 new_handler = Handler.new(@bot, listener.event, Pattern.new(nil, //, nil)) do |message, *args|
319   - if self.class.call_hooks(:pre, :listen_to, self, [message])
  322 + if self.class.call_hooks(:pre, :listen_to, nil, self, [message])
320 323 __send__(listener.method, message, *args)
321   - self.class.call_hooks(:post, :listen_to, self, [message])
  324 + self.class.call_hooks(:post, :listen_to, nil, self, [message])
322 325 else
323 326 @bot.loggers.debug "[plugin] #{self.class.plugin_name}: Dropping message due to hook"
324 327 end
@@ -334,9 +337,9 @@ def __register_ctcps
334 337 self.class.ctcps.each do |ctcp|
335 338 @bot.loggers.debug "[plugin] #{self.class.plugin_name}: Registering CTCP `#{ctcp}`"
336 339 new_handler = Handler.new(@bot, :ctcp, Pattern.generate(:ctcp, ctcp)) do |message, *args|
337   - if self.class.call_hooks(:pre, :ctcp, self, [message])
  340 + if self.class.call_hooks(:pre, :ctcp, nil, self, [message])
338 341 __send__("ctcp_#{ctcp.downcase}", message, *args)
339   - self.class.call_hooks(:post, :ctcp, self, [message])
  342 + self.class.call_hooks(:post, :ctcp, nil, self, [message])
340 343 else
341 344 @bot.loggers.debug "[plugin] #{self.class.plugin_name}: Dropping message due to hook"
342 345 end
@@ -380,9 +383,9 @@ def __register_matchers
380 383 elsif arity == 0
381 384 args = []
382 385 end
383   - if self.class.call_hooks(:pre, :match, self, [message])
  386 + if self.class.call_hooks(:pre, :match, matcher.group, self, [message])
384 387 method.call(message, *args)
385   - self.class.call_hooks(:post, :match, self, [message])
  388 + self.class.call_hooks(:post, :match, matcher.group, self, [message])
386 389 else
387 390 @bot.loggers.debug "[plugin] #{self.class.plugin_name}: Dropping message due to hook"
388 391 end

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.