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,
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

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



24
25
26
27
# File 'lib/guard/watcher.rb', line 24

def initialize(pattern, action = nil)
  @action = action
  @pattern = Pattern.create(pattern)
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



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

def action
  @action
end

#patternObject

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.

Parameters:

  • guard (Guard::Plugin)

    the Guard plugin which watchers are used

  • files (Array<String>)

    the changed files

Returns:

  • (Array<Object>)

    the matched watcher response



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.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



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.

Parameters:

  • matches (String, MatchData)

    the matched path or the match from the Regex

Returns:

  • (String)

    the final paths



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.message}"
  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