Class: Guard::Watcher
- Inherits:
-
Object
- Object
- Guard::Watcher
- Defined in:
- lib/guard/watcher.rb,
lib/guard/watcher/pattern.rb,
lib/guard/watcher/pattern/matcher.rb,
lib/guard/watcher/pattern/simple_path.rb,
lib/guard/watcher/pattern/match_result.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 plugin.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Compare with other watcher.
-
#initialize(pattern, action = nil) ⇒ Watcher
constructor
Initializes a file watcher.
- #match(string_or_pathname) ⇒ Object
- #to_s ⇒ Object (also: #inspect)
Constructor Details
#initialize(pattern, action = nil) ⇒ Watcher
Initializes a file watcher.
23 24 25 26 |
# File 'lib/guard/watcher.rb', line 23 def initialize(pattern, action = nil) @action = action @pattern = Pattern.create(pattern) end |
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
14 15 16 |
# File 'lib/guard/watcher.rb', line 14 def action @action end |
#pattern ⇒ Object
Returns the value of attribute pattern.
14 15 16 |
# File 'lib/guard/watcher.rb', line 14 def pattern @pattern end |
Class Method Details
.match_files(guard, files) ⇒ Array<Object>
Finds the files that matches a Guard plugin.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/guard/watcher.rb', line 41 def self.match_files(guard, files) return [] if files.empty? files.inject([]) do |paths, file| guard.watchers.each do |watcher| matches = watcher.match(file) next(paths) unless matches if watcher.action result = watcher.call_action(matches) if guard.[:any_return] paths << result elsif result.respond_to?(:empty?) && !result.empty? paths << Array(result) else next(paths) end else paths << matches[0] end break if guard.[:first_match] end guard.[:any_return] ? paths : paths.flatten.map(&:to_s) end end |
Instance Method Details
#==(other) ⇒ true, false
Compare with other watcher
31 32 33 |
# File 'lib/guard/watcher.rb', line 31 def ==(other) action == other.action && pattern == other.pattern end |
#match(string_or_pathname) ⇒ Object
69 70 71 72 |
# File 'lib/guard/watcher.rb', line 69 def match(string_or_pathname) m = pattern.match(string_or_pathname) m ? Pattern::MatchResult.new(m, string_or_pathname) : nil end |
#to_s ⇒ Object Also known as: inspect
88 89 90 |
# File 'lib/guard/watcher.rb', line 88 def to_s pattern end |