watcher cleaned up.

This commit is contained in:
Kevin Krauss 2011-10-09 15:54:24 -07:00
parent a7b6ebeef1
commit b2f282f1e8

View File

@ -6,15 +6,16 @@ module Guard
# #
class Watcher class Watcher
attr_accessor :pattern, :action, :any_rtn attr_accessor :pattern, :action, :any_return
# Initialize a file watcher. # Initialize a file watcher.
# #
# @param [String, Regexp] pattern the pattern to be watched by the guard # @param [String, Regexp] pattern the pattern to be watched by the guard
# @param [Block] action the action to execute before passing the result to the Guard # @param [Block] action the action to execute before passing the result to the Guard
# @param [Boolean] any_return allow the user to define return when using a block
# #
def initialize(pattern, action = nil, any_rtn = false) def initialize(pattern, action = nil, any_return = false)
@pattern, @action, @any_rtn = pattern, action, any_rtn @pattern, @action, @any_return = pattern, action, any_return
@@warning_printed ||= false @@warning_printed ||= false
# deprecation warning # deprecation warning
@ -41,12 +42,11 @@ module Guard
# @return [Array<Object>] the matched watcher response # @return [Array<Object>] the matched watcher response
def self.match_files(guard, files) def self.match_files(guard, files)
guard.watchers.inject([]) do |paths, watcher| guard.watchers.inject([]) do |paths, watcher|
wa = watcher.any_rtn
files.each do |file| files.each do |file|
if matches = watcher.match_file?(file) if matches = watcher.match_file?(file)
if watcher.action if watcher.action
result = watcher.call_action(matches) result = watcher.call_action(matches)
if wa if watcher.any_return
paths << result paths << result
elsif result.respond_to?(:empty?) && ! result.empty? elsif result.respond_to?(:empty?) && ! result.empty?
paths << Array(result) paths << Array(result)
@ -57,7 +57,7 @@ module Guard
end end
end end
wa ? paths : paths.flatten.map{|p| p.to_s} watcher.any_return ? paths : paths.flatten.map{|p| p.to_s}
end end
end end