Skip to content

Commit

Permalink
Fix Guard::Plugin::Hooker (fixes #521)
Browse files Browse the repository at this point in the history
  • Loading branch information
rymai committed Nov 12, 2013
1 parent 83155e2 commit 444552f
Showing 1 changed file with 67 additions and 53 deletions.
120 changes: 67 additions & 53 deletions lib/guard/plugin/hooker.rb
Expand Up @@ -17,6 +17,73 @@ module Hooker

require 'guard/ui'

# The Hooker module gets included.
#
# @param [Class] base the class that includes the module
#
def self.included(base)
base.send :include, InstanceMethods
end

# Instance methods that gets included in the base class.
#
module InstanceMethods

# When event is a Symbol, {#hook} will generate a hook name
# by concatenating the method name from where {#hook} is called
# with the given Symbol.
#
# @example Add a hook with a Symbol
#
# def run_all
# hook :foo
# end
#
# Here, when {Guard::Plugin::Base#run_all} is called, {#hook} will notify
# callbacks registered for the "run_all_foo" event.
#
# When event is a String, {#hook} will directly turn the String
# into a Symbol.
#
# @example Add a hook with a String
#
# def run_all
# hook "foo_bar"
# end
#
# When {Guard::Plugin::Base#run_all} is called, {#hook} will notify
# callbacks registered for the "foo_bar" event.
#
# @param [Symbol, String] event the name of the Guard event
# @param [Array] args the parameters are passed as is to the callbacks
# registered for the given event.
#
def hook(event, *args)
hook_name = if event.is_a? Symbol
calling_method = caller[0][/`([^']*)'/, 1]
"#{ calling_method }_#{ event }"
else
event
end

::Guard::UI.debug "Hook :#{ hook_name } executed for #{ self.class }"

Hooker.notify(self, hook_name.to_sym, *args)
end

private

# Add all the Guard::Plugin's callbacks to the global @callbacks array
# that's used by Guard to know which callbacks to notify.
#
def _register_callbacks
callbacks.each do |callback|
Hooker.add_callback(callback[:listener], self, callback[:events])
end
end

end

# Get all callbacks registered for all Guard plugins present in the
# Guardfile.
#
Expand Down Expand Up @@ -65,59 +132,6 @@ def self.reset_callbacks!
@callbacks = nil
end

# When event is a Symbol, {#hook} will generate a hook name
# by concatenating the method name from where {#hook} is called
# with the given Symbol.
#
# @example Add a hook with a Symbol
#
# def run_all
# hook :foo
# end
#
# Here, when {Guard::Plugin::Base#run_all} is called, {#hook} will notify
# callbacks registered for the "run_all_foo" event.
#
# When event is a String, {#hook} will directly turn the String
# into a Symbol.
#
# @example Add a hook with a String
#
# def run_all
# hook "foo_bar"
# end
#
# When {Guard::Plugin::Base#run_all} is called, {#hook} will notify
# callbacks registered for the "foo_bar" event.
#
# @param [Symbol, String] event the name of the Guard event
# @param [Array] args the parameters are passed as is to the callbacks
# registered for the given event.
#
def hook(event, *args)
hook_name = if event.is_a? Symbol
calling_method = caller[0][/`([^']*)'/, 1]
"#{ calling_method }_#{ event }"
else
event
end

::Guard::UI.debug "Hook :#{ hook_name } executed for #{ self.class }"

Hooker.notify(self, hook_name.to_sym, *args)
end

private

# Add all the Guard::Plugin's callbacks to the global @callbacks array
# that's used by Guard to know which callbacks to notify.
#
def _register_callbacks
callbacks.each do |callback|
self.class.add_callback(callback[:listener], self, callback[:events])
end
end

end
end
end

1 comment on commit 444552f

@ijcd
Copy link

@ijcd ijcd commented on 444552f Nov 12, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that fixes it. I ran the specs on master while I was trying to figure out what was wrong and they were all passing, I think. This probably needs just a single failing spec for use as a regression. Thanks for the fix!

Please sign in to comment.