Class: Guard::Watcher

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, action = nil) ⇒ Watcher

Initializes a file watcher.

Parameters:

  • pattern (String, Regexp)

    the pattern to be watched by the Guard plugin

  • action (Block) (defaults to: nil)

    the action to execute before passing the result to the Guard plugin



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

#actionObject

Returns the value of attribute action.



14
15
16
# File 'lib/guard/watcher.rb', line 14

def action
  @action
end

#patternObject

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.

Parameters:

  • guard (Guard::Plugin)

    the Guard plugin which watchers are used

  • files (Array<String>)

    the changed files

Returns:

  • (Array<Object>)

    the matched watcher response



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.options[: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.options[:first_match]
    end

    guard.options[:any_return] ? paths : paths.flatten.map(&:to_s)
  end
end

Instance Method Details

#==(other) ⇒ true, false

Compare with other watcher

Parameters:

Returns:

  • (true, false)

    equal or not



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_sObject Also known as: inspect



88
89
90
# File 'lib/guard/watcher.rb', line 88

def to_s
  pattern
end