Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Allow hooks for specific matcher groups #144

Merged
merged 2 commits into from

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)
if hooks.is_a?(Hash)
hooks = hooks.map { |k, v| v }
end
- return hooks.select { |hook| (events & hook.for).size > 0 }
+ hooks.select! { |hook| (events & hook.for).size > 0 }
+ end
+
+ if group.nil?
+ return hooks.select { |hook| hook.group.nil? }
+ else
+ return hooks.select { |hook| hook.group.nil? || hook.group == group }
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
Commits on Oct 4, 2013
  1. Curtis McEnroe

    Allow hooks for specific matcher groups

    programble authored
    Fixes #143.
  2. Curtis McEnroe
This page is out of date. Refresh to see the latest.
Showing with 16 additions and 13 deletions.
  1. +16 −13 lib/cinch/plugin.rb
29 lib/cinch/plugin.rb
View
@@ -107,7 +107,7 @@ def plugin_name=(new_name)
# @attr [Symbol] type
# @attr [Array<Symbol>] for
# @attr [Symbol] method
- Hook = Struct.new(:type, :for, :method)
+ Hook = Struct.new(:type, :for, :method, :group)
# @api private
def self.extended(by)
@@ -256,11 +256,12 @@ def timer(interval, options = {})
# @option options [Array<:match, :listen_to, :ctcp>] :for ([:match, :listen_to, :ctcp])
# Which kinds of events to run the hook for.
# @option options [Symbol] :method (:hook) The method to execute.
+ # @option options [Symbol] :group (nil) The matcher group to execute the hook for.
# @return [Hook]
# @since 1.1.0
def hook(type, options = {})
- options = {:for => [:match, :listen_to, :ctcp], :method => :hook}.merge(options)
- hook = Hook.new(type, options[:for], options[:method])
+ options = {:for => [:match, :listen_to, :ctcp], :method => :hook, :group => nil}.merge(options)
+ hook = Hook.new(type, options[:for], options[:method], options[:group])
__hooks(type) << hook
hook
@@ -268,7 +269,7 @@ def hook(type, options = {})
# @return [Hash]
# @api private
- def __hooks(type = nil, events = nil)
+ def __hooks(type = nil, events = nil, group = nil)
if type.nil?
hooks = @hooks
else
@@ -282,14 +283,16 @@ def __hooks(type = nil, events = nil)
if hooks.is_a?(Hash)
hooks = hooks.map { |k, v| v }
end
- return hooks.select { |hook| (events & hook.for).size > 0 }
+ hooks.select! { |hook| (events & hook.for).size > 0 }
end
+
+ return hooks.select { |hook| hook.group.nil? || hook.group == group }
end
# @return [Boolean] True if processing should continue
# @api private
- def call_hooks(type, event, instance, args)
- stop = __hooks(type, event).find { |hook|
+ def call_hooks(type, event, group, instance, args)
+ stop = __hooks(type, event, group).find { |hook|
# stop after the first hook that returns false
if hook.method.respond_to?(:call)
instance.instance_exec(*args, &hook.method) == false
@@ -316,9 +319,9 @@ def __register_listeners
self.class.listeners.each do |listener|
@bot.loggers.debug "[plugin] #{self.class.plugin_name}: Registering listener for type `#{listener.event}`"
new_handler = Handler.new(@bot, listener.event, Pattern.new(nil, //, nil)) do |message, *args|
- if self.class.call_hooks(:pre, :listen_to, self, [message])
+ if self.class.call_hooks(:pre, :listen_to, nil, self, [message])
__send__(listener.method, message, *args)
- self.class.call_hooks(:post, :listen_to, self, [message])
+ self.class.call_hooks(:post, :listen_to, nil, self, [message])
else
@bot.loggers.debug "[plugin] #{self.class.plugin_name}: Dropping message due to hook"
end
@@ -334,9 +337,9 @@ def __register_ctcps
self.class.ctcps.each do |ctcp|
@bot.loggers.debug "[plugin] #{self.class.plugin_name}: Registering CTCP `#{ctcp}`"
new_handler = Handler.new(@bot, :ctcp, Pattern.generate(:ctcp, ctcp)) do |message, *args|
- if self.class.call_hooks(:pre, :ctcp, self, [message])
+ if self.class.call_hooks(:pre, :ctcp, nil, self, [message])
__send__("ctcp_#{ctcp.downcase}", message, *args)
- self.class.call_hooks(:post, :ctcp, self, [message])
+ self.class.call_hooks(:post, :ctcp, nil, self, [message])
else
@bot.loggers.debug "[plugin] #{self.class.plugin_name}: Dropping message due to hook"
end
@@ -380,9 +383,9 @@ def __register_matchers
elsif arity == 0
args = []
end
- if self.class.call_hooks(:pre, :match, self, [message])
+ if self.class.call_hooks(:pre, :match, matcher.group, self, [message])
method.call(message, *args)
- self.class.call_hooks(:post, :match, self, [message])
+ self.class.call_hooks(:post, :match, matcher.group, self, [message])
else
@bot.loggers.debug "[plugin] #{self.class.plugin_name}: Dropping message due to hook"
end
Something went wrong with that request. Please try again.