Class: Guard::Watcher
- Inherits:
-
Object
- Object
- Guard::Watcher
- Defined in:
- lib/guard/watcher.rb
Overview
The watcher defines a RegExp that will be matched against file system modifications. When a watcher matches a change, an optional action block is executed to enable processing the file system change result.
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#pattern ⇒ Object
Returns the value of attribute pattern.
Class Method Summary collapse
-
.match_files(guard, files) ⇒ Array<Object>
Finds the files that matches a Guard.
-
.match_files?(guards, files) ⇒ Boolean
Test if a file would be matched by any of the Guards watchers.
-
.match_guardfile?(files) ⇒ Boolean
Test if any of the files is the Guardfile.
Instance Method Summary collapse
-
#call_action(matches) ⇒ String
Executes a watcher action.
-
#initialize(pattern, action = nil) ⇒ Watcher
constructor
Initialize a file watcher.
-
#match_file?(file) ⇒ Boolean
Test the watchers pattern against a file.
Constructor Details
#initialize(pattern, action = nil) ⇒ Watcher
Initialize a file watcher.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/guard/watcher.rb', line 16 def initialize(pattern, action = nil) @pattern, @action = pattern, action @@warning_printed ||= false # deprecation warning if @pattern.is_a?(String) && @pattern =~ /(^(\^))|(>?(\\\.)|(\.\*))|(\(.*\))|(\[.*\])|(\$$)/ unless @@warning_printed UI.info "*"*20 + "\nDEPRECATION WARNING!\n" + "*"*20 UI.info <<-MSG You have a string in your Guardfile watch patterns that seem to represent a Regexp. Guard matches String with == and Regexp with Regexp#match. You should either use plain String (without Regexp special characters) or real Regexp. MSG @@warning_printed = true end UI.info "\"#{@pattern}\" has been converted to #{ Regexp.new(@pattern).inspect }\n" @pattern = Regexp.new(@pattern) end end |
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
9 10 11 |
# File 'lib/guard/watcher.rb', line 9 def action @action end |
#pattern ⇒ Object
Returns the value of attribute pattern.
9 10 11 |
# File 'lib/guard/watcher.rb', line 9 def pattern @pattern end |
Class Method Details
.match_files(guard, files) ⇒ Array<Object>
Finds the files that matches a Guard.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/guard/watcher.rb', line 43 def self.match_files(guard, files) guard.watchers.inject([]) do |paths, watcher| files.each do |file| if matches = watcher.match_file?(file) if watcher.action result = watcher.call_action(matches) if guard.[:any_return] paths << result elsif result.respond_to?(:empty?) && !result.empty? paths << Array(result) end else paths << matches[0] end end end guard.[:any_return] ? paths : paths.flatten.map { |p| p.to_s } end end |
.match_files?(guards, files) ⇒ Boolean
Test if a file would be matched by any of the Guards watchers.
70 71 72 73 74 75 76 |
# File 'lib/guard/watcher.rb', line 70 def self.match_files?(guards, files) guards.any? do |guard| guard.watchers.any? do |watcher| files.any? { |file| watcher.match_file?(file) } end end end |
.match_guardfile?(files) ⇒ Boolean
Test if any of the files is the Guardfile.
96 97 98 |
# File 'lib/guard/watcher.rb', line 96 def self.match_guardfile?(files) files.any? { |file| "#{ Dir.pwd }/#{ file }" == Dsl.guardfile_path } end |
Instance Method Details
#call_action(matches) ⇒ String
Executes a watcher action.
105 106 107 108 109 110 111 |
# File 'lib/guard/watcher.rb', line 105 def call_action(matches) begin @action.arity > 0 ? @action.call(matches) : @action.call rescue Exception => e UI.error "Problem with watch action!\n#{ e. }\n\n#{ e.backtrace.join("\n") }" end end |
#match_file?(file) ⇒ Boolean
Test the watchers pattern against a file.
83 84 85 86 87 88 89 |
# File 'lib/guard/watcher.rb', line 83 def match_file?(file) if @pattern.is_a?(Regexp) file.match(@pattern) else file == @pattern ? [file] : nil end end |