Hooks - Add :begin and :end hook to all guard actions via Guard.supervised_task

This commit is contained in:
monocle 2011-04-14 13:31:34 -07:00 committed by Rémy Coutable
parent 7916139726
commit 386b0be53d
2 changed files with 4 additions and 9 deletions

View File

@ -6,6 +6,7 @@ module Guard
autoload :Listener, 'guard/listener'
autoload :Watcher, 'guard/watcher'
autoload :Notifier, 'guard/notifier'
autoload :Hook, 'guard/hook'
class << self
attr_accessor :options, :guards, :listener
@ -57,7 +58,9 @@ module Guard
# 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.to_s}_begin"
guard.send(task_to_supervise, *args)
guard.hook "#{task_to_supervise.to_s}_end"
rescue Exception
UI.error("#{guard.class.name} guard failed to achieve its <#{task_to_supervise.to_s}> command: #{$!}")
::Guard.guards.delete guard

View File

@ -36,7 +36,7 @@ module Guard
end
def has_callback?(listener, guard_class, event)
listeners(guard_class, event).include? listener
callbacks[[guard_class, event]].include? listener
end
def notify(guard_class, event)
@ -48,14 +48,6 @@ module Guard
def reset!
@callbacks = nil
end
private
def listeners(guard_class, event)
callbacks[[guard_class, event]].inject([]) do |all, arr|
all << arr
end
end
end
end
end