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,
lib/guard/watcher/pattern/pathname_path.rb,
lib/guard/watcher/pattern/deprecated_regexp.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.
Defined Under Namespace
Classes: Pattern
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.
-
#call_action(matches) ⇒ String
Executes a watcher action.
-
#initialize(pattern, action = nil) ⇒ Watcher
constructor
Initializes a file watcher.
- #match(string_or_pathname) ⇒ Object
Constructor Details
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
15 16 17 |
# File 'lib/guard/watcher.rb', line 15 def action @action end |
#pattern ⇒ Object
Returns the value of attribute pattern.
15 16 17 |
# File 'lib/guard/watcher.rb', line 15 def pattern @pattern end |
Class Method Details
.match_files(guard, files) ⇒ Array<Object>
Finds the files that matches a Guard plugin.
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 68 |
# File 'lib/guard/watcher.rb', line 42 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
32 33 34 |
# File 'lib/guard/watcher.rb', line 32 def ==(other) action == other.action && pattern == other.pattern end |
#call_action(matches) ⇒ String
Executes a watcher action.
81 82 83 84 85 86 |
# File 'lib/guard/watcher.rb', line 81 def call_action(matches) @action.arity > 0 ? @action.call(matches) : @action.call rescue => ex UI.error "Problem with watch action!\n#{ex.}" UI.error ex.backtrace.join("\n") end |
#match(string_or_pathname) ⇒ Object
70 71 72 73 |
# File 'lib/guard/watcher.rb', line 70 def match(string_or_pathname) m = pattern.match(string_or_pathname) m.nil? ? nil : Pattern::MatchResult.new(m, string_or_pathname) end |