Skip to content
Browse files

Hook - 1) Send args to hooks from Guard.supervised_task

2) Pass args from hooks to callbacks
3) Suppress UI message from hooks unless in 'development'
  • Loading branch information...
1 parent 5b8ae60 commit c82e1582f84cb95f6c31662deb62a4659261c685 @monocle committed Apr 17, 2011
Showing with 64 additions and 33 deletions.
  1. +2 −2 lib/guard.rb
  2. +8 −5 lib/guard/hook.rb
  3. +46 −22 spec/guard/hook_spec.rb
  4. +8 −4 spec/guard_spec.rb
View
4 lib/guard.rb
@@ -60,9 +60,9 @@ def run_on_change_for_all_guards(files)
# Let a guard execute its task but
# fire it if his work leads to a system failure
def supervised_task(guard, task_to_supervise, *args)
- guard.hook "#{task_to_supervise}_begin"
+ guard.hook("#{task_to_supervise}_begin", *args)
result = guard.send(task_to_supervise, *args)
- guard.hook "#{task_to_supervise}_end"
+ guard.hook("#{task_to_supervise}_end", result)
result
rescue Exception
UI.error("#{guard.class.name} guard failed to achieve its <#{task_to_supervise}> command: #{$!}")
View
13 lib/guard/hook.rb
@@ -9,16 +9,19 @@ module InstanceMethods
# from the symbol and calling method name. When passed
# a string, #hook will turn the string into a symbol
# directly.
- def hook(event)
+ def hook(event, *args)
hook_name = if event.is_a? Symbol
calling_method = caller[0][/`([^']*)'/, 1]
"#{calling_method}_#{event}".to_sym
else
event.to_sym
end
- UI.info "\nHook :#{hook_name} executed for #{self.class}"
- Hook.notify(self.class, hook_name)
+ if ENV["GUARD_ENV"] == "development"
+ UI.info "\nHook :#{hook_name} executed for #{self.class}"
+ end
+
+ Hook.notify(self.class, hook_name, *args)
end
end
@@ -38,9 +41,9 @@ def has_callback?(listener, guard_class, event)
callbacks[[guard_class, event]].include?(listener)
end
- def notify(guard_class, event)
+ def notify(guard_class, event, *args)
callbacks[[guard_class, event]].each do |listener|
- listener.call(guard_class, event)
+ listener.call(guard_class, event, *args)
end
end
View
68 spec/guard/hook_spec.rb
@@ -3,23 +3,16 @@
describe Guard::Hook do
subject { Guard::Hook }
- class Guard::Dummy < Guard::Guard
- include Guard::Hook
-
- def run_all
- hook :begin
- hook :end
- end
- end
+ class Guard::Dummy < Guard::Guard; end
let(:guard_class) { ::Guard::Dummy }
let(:listener) { double('listener').as_null_object }
+ after { subject.reset_callbacks! }
+
context "--module methods--" do
before { subject.add_callback(listener, guard_class, :start_begin) }
- after { subject.reset_callbacks! }
-
describe ".add_callback" do
it "can add a single callback" do
subject.has_callback?(listener, guard_class, :start_begin).should be_true
@@ -34,8 +27,8 @@ def run_all
describe ".notify" do
it "sends :call to the given Guard class's callbacks" do
- listener.should_receive(:call).with(guard_class, :start_begin)
- subject.notify(guard_class, :start_begin)
+ listener.should_receive(:call).with(guard_class, :start_begin, "args")
+ subject.notify(guard_class, :start_begin, "args")
end
it "runs only the given callbacks" do
@@ -55,24 +48,55 @@ def run_all
end
describe "#hook" do
+ before(:all) do
+ guard_class.class_eval do
+ def start
+ hook "my_hook"
+ end
+
+ def run_all
+ hook :begin
+ hook :end
+ end
+
+ def stop
+ hook :begin, 'args'
+ hook 'special_sauce', 'first_arg', 'second_arg'
+ end
+ end
+
+ @guard = guard_class.new
+ end
+
it "calls Guard::Hook.notify" do
- guard = guard_class.new
Guard::Hook.should_receive(:notify).with(guard_class, :run_all_begin)
Guard::Hook.should_receive(:notify).with(guard_class, :run_all_end)
- guard.run_all
+ @guard.run_all
end
it "if passed a string parameter, will use that for the hook name" do
- guard_class.class_eval do
- def start
- hook "my_hook"
- end
+ Guard::Hook.should_receive(:notify).with(guard_class, :my_hook)
+ @guard.start
+ end
+
+ it "accepts extra args" do
+ Guard::Hook.should_receive(:notify).with(guard_class, :stop_begin, 'args')
+ Guard::Hook.should_receive(:notify).with(guard_class, :special_sauce, 'first_arg', 'second_arg')
+ @guard.stop
+ end
+
+ context "--UI message--" do
+ it "is sent when in development mode" do
+ ENV["GUARD_ENV"] = 'development'
+ Guard::UI.should_receive(:info)
+ @guard.start
+ ENV["GUARD_ENV"] = 'test'
end
- guard = guard_class.new
- Guard::Hook.should_receive(:notify).with(guard_class, :my_hook)
- guard.start
+ it "is not sent when not in development mode" do
+ Guard::UI.should_not_receive(:info)
+ @guard.start
+ end
end
end
-
end
View
12 spec/guard_spec.rb
@@ -77,10 +77,14 @@
::Guard.supervised_task(@g, :regular_with_arg, "given_path").should == "i'm a success"
end
- it "calls the default hooks" do
- @g.should_receive(:hook).with("regular_begin")
- @g.should_receive(:hook).with("regular_end")
- ::Guard.supervised_task(@g, :regular)
+ it "passes the args to the :begin hook" do
+ @g.should_receive(:hook).with("regular_with_arg_begin", "given_path")
+ ::Guard.supervised_task(@g, :regular_with_arg, "given_path")
+ end
+
+ it "passes the result of the supervised method to the :end hook" do
+ @g.should_receive(:hook).with("regular_with_arg_end", "i'm a success")
+ ::Guard.supervised_task(@g, :regular_with_arg, "given_path")
end
end

0 comments on commit c82e158

Please sign in to comment.
Something went wrong with that request. Please try again.