Parent

Files

Guard::Watcher

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.

Attributes

action[RW]
pattern[RW]

Public Class Methods

match_files(guard, files) click to toggle source

Finds the files that matches a Guard plugin.

@param [Guard::Plugin] guard the Guard plugin which watchers are used @param [Array<String>] files the changed files @return [Array<Object>] the matched watcher response

# File lib/guard/watcher.rb, line 48
def self.match_files(guard, files)
  return [] if files.empty?

  guard.watchers.inject([]) do |paths, watcher|
    files.each do |file|
      if matches = watcher.match(file)
        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)
          end
        else
          paths << matches[0]
        end
      end
    end

    guard.options[:any_return] ? paths : paths.flatten.map { |p| p.to_s }
  end
end
match_files?(plugins, files) click to toggle source

Tests if a file would be matched by any of the Guard plugin watchers.

@param [Array<Guard::Plugin>] plugins the Guard plugins to use the

watchers from

@param [Array<String>] files the files to test @return [Boolean] Whether a file matches

# File lib/guard/watcher.rb, line 78
def self.match_files?(plugins, files)
  plugins.any? do |plugin|
    plugin.watchers.any? do |watcher|
      files.any? { |file| watcher.match(file) }
    end
  end
end
match_guardfile?(files) click to toggle source

Tests if any of the files is the Guardfile.

@param [Array<String>] files the files to test @return [Boolean] whether one of these files is the Guardfile

# File lib/guard/watcher.rb, line 91
def self.match_guardfile?(files)
  files.any? { |file| "#{ Dir.pwd }/#{ file }" == ::Guard.evaluator.guardfile_path }
end
new(pattern, action = nil) click to toggle source

Initializes a file watcher.

@param [String, Regexp] pattern the pattern to be watched by the Guard

plugin

@param [Block] action the action to execute before passing the result to

the Guard plugin
# File lib/guard/watcher.rb, line 21
def initialize(pattern, action = nil)
  @pattern, @action = pattern, action
  @@warning_printed ||= false

  # deprecation warning
  if @pattern.is_a?(String) && @pattern =~ /(^(\^))|(>?(\\\.)|(\.\*))|(\(.*\))|(\[.*\])|(\$$)/
    unless @@warning_printed
      ::Guard::UI.info "*"*20 + "\nDEPRECATION WARNING!\n" + "*"*20
      ::Guard::UI.info             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.
      @@warning_printed = true
    end

    ::Guard::UI.info "\"#{@pattern}\" has been converted to #{ Regexp.new(@pattern).inspect }\n"
    @pattern = Regexp.new(@pattern)
  end
end

Public Instance Methods

call_action(matches) click to toggle source

Executes a watcher action.

@param [String, MatchData] matches the matched path or the match from the

Regex

@return [String] the final paths

# File lib/guard/watcher.rb, line 122
def call_action(matches)
  begin
    @action.arity > 0 ? @action.call(matches) : @action.call
  rescue Exception => e
    ::Guard::UI.error "Problem with watch action!\n#{ e.message }\n\n#{ e.backtrace.join("\n") }"
  end
end
match(file) click to toggle source

Test the watchers pattern against a file.

@param [String] file the file to test @return [Array<String>] an array of matches (or containing a single path

if the pattern is a string)
# File lib/guard/watcher.rb, line 101
def match(file)
  f = file
  deleted = file.start_with?('!')
  f = deleted ? f[1..-1] : f
  if @pattern.is_a?(Regexp)
    if m = f.match(@pattern)
      m = m.to_a
      m[0] = file
      m
    end
  else
    f == @pattern ? [file] : nil
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.