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
# 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 Gem::LoadError
  Kernel.warn BUNDLER_DECLARE_GEM
end

Instance Method Details

#_change(type) ⇒ Object (private)



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

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



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

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)



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

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

#_worker_callbackObject (private)



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

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

#startObject



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

def start
  worker = _init_worker
  worker.run!
end