Class: Listen::Adapter::Windows

Inherits:
Base
  • Object
show all
Defined in:
lib/listen/adapter/windows.rb

Overview

Adapter implementation for Windows wdm.

Constant Summary collapse

BUNDLER_DECLARE_GEM =

The message to show when wdm gem isn't available

<<-EOS.gsub(/^ {6}/, '')
  Please add the following to your Gemfile to avoid polling for changes:
    require 'rbconfig'
    gem 'wdm', '>= 0.1.0' if RbConfig::CONFIG['target_os'] =~ /mswin|mingw|cygwin/i
EOS

Constants inherited from Base

Base::DEFAULT_LATENCY

Instance Attribute Summary

Attributes inherited from Base

#listener

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#_directories_path, #_latency, #_notify_change, #initialize

Constructor Details

This class inherits a constructor from Listen::Adapter::Base

Class Method Details

.usable?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
# File 'lib/listen/adapter/windows.rb', line 16

def self.usable?
  if RbConfig::CONFIG['target_os'] =~ /mswin|mingw|cygwin/i
    require 'wdm'
    true
  end
rescue LoadError
  Kernel.warn BUNDLER_DECLARE_GEM
  false
end

Instance Method Details

#_change(type) ⇒ Object (private)



54
55
56
57
58
59
60
61
# File 'lib/listen/adapter/windows.rb', line 54

def _change(type)
  { modified: [:modified],
    added:    [:added, :renamed_new_file],
    removed:  [:removed, :renamed_old_file] }.each do |change, types|
    return change if types.include?(type)
  end
  nil
end

#_init_workerWDM::Monitor (private)

Initializes a WDM monitor and adds a watcher for each directory passed to the adapter.

Returns:

  • (WDM::Monitor)

    initialized worker



38
39
40
41
42
# File 'lib/listen/adapter/windows.rb', line 38

def _init_worker
  WDM::Monitor.new.tap do |worker|
    _directories_path.each { |path| worker.watch_recursively(path, &_worker_callback) }
  end
end

#_path(path) ⇒ Object (private)



50
51
52
# File 'lib/listen/adapter/windows.rb', line 50

def _path(path)
  Pathname.new(path)
end

#_worker_callbackObject (private)



44
45
46
47
48
# File 'lib/listen/adapter/windows.rb', line 44

def _worker_callback
  lambda do |change|
      _notify_change(_path(change.path), type: 'File', change: _change(change.type))
  end
end

#startObject



26
27
28
29
# File 'lib/listen/adapter/windows.rb', line 26

def start
  worker = _init_worker
  Thread.new { worker.run! }
end