Parent

Guard::Listener

The Listener is the base class for all listener implementations.

@abstract

Constants

DEFAULT_IGNORE_PATHS

Default paths that gets ignored by the listener

Attributes

changed_files[RW]
directory[R]
ignore_paths[R]

Public Class Methods

new(directory = Dir.pwd, options = {}) click to toggle source

Initialize the listener.

@param [String] directory the root directory to listen to @option options [Boolean] relativize_paths use only relative paths @option options [Boolean] watch_all_modifications to enable deleted and moved file listening. @option options [Array<String>] ignore_paths the paths to ignore by the listener

# File lib/guard/listener.rb, line 60
def initialize(directory = Dir.pwd, options = {})
  @sha1_checksums_hash      = {}
  @file_timestamp_hash      = {}
  @changed_files            = []
  @paused                   = false

  @directory                = directory.to_s

  options                   = options.inject({}) { |h,(k,v)| h[k.to_sym] = v; h }
  @relativize_paths         = options.fetch(:relativize_paths, true)
  @watch_all_modifications  = options.fetch(:watch_all_modifications, false)

  @ignore_paths             = DEFAULT_IGNORE_PATHS
  @ignore_paths            |= options[:ignore_paths] if options[:ignore_paths]

  update_last_event
  start_reactor
end
select_and_init(options = nil) click to toggle source

Select the appropriate listener implementation for the current OS and initializes it.

@param [Hash] options the options for the listener @option options [String] watchdir the directory to watch @return [Guard::Listener] the chosen listener

# File lib/guard/listener.rb, line 35
def self.select_and_init(options = nil)
  watchdir = options && options[:watchdir] && File.expand_path(options[:watchdir])
  watchdir = Dir.pwd unless watchdir

  no_vendor = options && options[:no_vendor] ? options[:no_vendor] : false

  if mac? && Darwin.usable?(no_vendor)
    Darwin.new(watchdir, options)
  elsif linux? && Linux.usable?(no_vendor)
    Linux.new(watchdir, options)
  elsif windows? && Windows.usable?(no_vendor)
    Windows.new(watchdir, options)
  else
    UI.info 'Using polling (Please help us to support your system better than that).'
    Polling.new(watchdir, options)
  end
end

Public Instance Methods

all_files() click to toggle source

Get all files that are in the watched directory.

@return [Array<String>] the list of files

# File lib/guard/listener.rb, line 190
def all_files
  potentially_modified_files([@directory], :all => true)
end
clear_changed_files() click to toggle source

Clear the list of changed files.

# File lib/guard/listener.rb, line 123
def clear_changed_files
  @changed_files.clear
end
exclude_ignored_paths(dirs, ignore_paths = self.ignore_paths) click to toggle source

Removes the ignored paths from the directory list.

@param [Array<String>] dirs the directory to listen to @param [Array<String>] ignore_paths the paths to ignore @return children of the passed dirs that are not in the ignore_paths list

# File lib/guard/listener.rb, line 235
def exclude_ignored_paths(dirs, ignore_paths = self.ignore_paths)
  Dir.glob(dirs.map { |d| "#{d.sub(%r{/+$}, '')}/*" }, File::FNM_DOTMATCH).reject do |path|
    ignore_paths.include?(File.basename(path))
  end
end
modified_files(dirs, options = {}) click to toggle source

Get the modified files.

If the `:watch_all_modifications` option is true, then moved and deleted files are also reported, but prefixed by an exclamation point.

@example Deleted or moved file

!/home/user/dir/file.rb

@param [Array<String>] dirs the watched directories @param [Hash] options the listener options @option options [Symbol] all whether to files in sub directories @return [Array<String>] paths of files that have been modified

# File lib/guard/listener.rb, line 154
def modified_files(dirs, options = {})
  last_event = @last_event
  files = []

  if watch_all_modifications?
    deleted_files = @file_timestamp_hash.collect do |path, ts|
      unless File.exists?(path)
        @sha1_checksums_hash.delete(path)
        @file_timestamp_hash.delete(path)
        "!#{path}"
      end
    end
    files.concat(deleted_files.compact)
  end
  update_last_event
  updated_files = potentially_modified_files(dirs, options).select do |path|
    file_modified?(path, last_event)
  end
  files.concat(updated_files)

  relativize_paths(files)
end
on_change(&callback) click to toggle source

Store a listener callback.

@param [Block] callback the callback to store

# File lib/guard/listener.rb, line 131
def on_change(&callback)
  @callback = callback
end
pause() click to toggle source

Pause the listener to ignore change events.

# File lib/guard/listener.rb, line 111
def pause
  @paused = true
end
paused?() click to toggle source
# File lib/guard/listener.rb, line 24
def paused?
  @paused
end
relativize_paths(paths) click to toggle source

Scopes all given paths to the current directory.

@param [Array<String>] paths the paths to change @return [Array<String>] all paths now relative to the current dir

# File lib/guard/listener.rb, line 199
def relativize_paths(paths)
  return paths unless relativize_paths?
  paths.map do |path|
    path.gsub(%{^(!)?#{ @directory }/},'\1')
  end
end
relativize_paths?() click to toggle source

Use paths relative to the current directory.

@return [Boolean] whether to use relative or absolute paths

# File lib/guard/listener.rb, line 210
def relativize_paths?
  !!@relativize_paths
end
run() click to toggle source

Unpause the listener to listen again to change events.

# File lib/guard/listener.rb, line 117
def run
  @paused = false
end
start() click to toggle source

Start watching the root directory.

# File lib/guard/listener.rb, line 99
def start
  watch(@directory)
  timestamp_files if watch_all_modifications?
end
start_reactor() click to toggle source

Start the listener thread.

# File lib/guard/listener.rb, line 81
def start_reactor
  return if ENV["GUARD_ENV"] == 'test'

  Thread.new do
    loop do
      if @changed_files != [] && !@paused
        changed_files = @changed_files.dup
        clear_changed_files
        ::Guard.run_on_change(changed_files)
      else
        sleep 0.1
      end
    end
  end
end
stop() click to toggle source

Stop listening for events.

# File lib/guard/listener.rb, line 106
def stop
end
timestamp_files() click to toggle source

Populate initial timestamp file hash to watch for deleted or moved files.

# File lib/guard/listener.rb, line 225
def timestamp_files
  all_files.each { |path| set_file_timestamp_hash(path) }
end
update_last_event() click to toggle source

Updates the timestamp of the last event.

# File lib/guard/listener.rb, line 137
def update_last_event
  @last_event = Time.now
end
watch(directory) click to toggle source

Register a directory to watch. Must be implemented by the subclasses.

@param [String] directory the directory to watch

# File lib/guard/listener.rb, line 182
def watch(directory)
  raise NotImplementedError, "do whatever you want here, given the directory as only argument"
end
watch_all_modifications?() click to toggle source

test if the listener should also watch for deleted and moved files

@return [Boolean] whether to watch all file modifications or not

# File lib/guard/listener.rb, line 219
def watch_all_modifications?
  !!@watch_all_modifications
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.