Class: Listen::Adapter

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

Constant Summary collapse

DEFAULT_LATENCY =

The default delay between checking for changes.

0.1
POLLING_FALLBACK_MESSAGE =

The default warning message when falling back to polling adapter.

"WARNING: Listen fallen back to polling, learn more at https://github.com/guard/listen#fallback."

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory, options = {}) {|changed_dirs, options| ... } ⇒ Listen::Adapter

Initialize the adapter.

Parameters:

  • directory (String, Pathname)

    the directory to watch

  • options (Hash) (defaults to: {})

    the adapter options

Options Hash (options):

  • latency (Float)

    the delay between checking for changes in seconds

Yields:

  • (changed_dirs, options)

    callback Callback called when a change happens

Yield Parameters:

  • changed_dirs (Array<String>)

    the changed directories

  • options (Hash)

    callback options (like :recursive => true)



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

def initialize(directory, options = {}, &callback)
  @directory = directory
  @callback  = callback
  @latency ||= DEFAULT_LATENCY
  @latency   = options[:latency] if options[:latency]
end

Instance Attribute Details

#latencyObject

Returns the value of attribute latency.



5
6
7
# File 'lib/listen/adapter.rb', line 5

def latency
  @latency
end

Class Method Details

.select_and_initialize(directory, options = {}) {|changed_dirs, options| ... } ⇒ Listen::Adapter

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

Parameters:

  • directory (String, Pathname)

    the directory to watch

  • options (Hash) (defaults to: {})

    the adapter options

Options Hash (options):

  • force_polling (Boolean)

    to force polling or not

  • polling_fallback_message (String, Boolean)

    to change polling fallback message or remove it

  • latency (Float)

    the delay between checking for changes in seconds

Yields:

  • (changed_dirs, options)

    callback Callback called when a change happens

Yield Parameters:

  • changed_dirs (Array<String>)

    the changed directories

  • options (Hash)

    callback options (like :recursive => true)

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/listen/adapter.rb', line 27

def self.select_and_initialize(directory, options = {}, &callback)
  return Adapters::Polling.new(directory, options, &callback) if options.delete(:force_polling)

  if Adapters::Darwin.usable_and_work?(directory, options)
    Adapters::Darwin.new(directory, options, &callback)
  elsif Adapters::Linux.usable_and_work?(directory, options)
    Adapters::Linux.new(directory, options, &callback)
  elsif Adapters::Windows.usable_and_work?(directory, options)
    Adapters::Windows.new(directory, options, &callback)
  else
    unless options[:polling_fallback_message] == false
      Kernel.warn(options[:polling_fallback_message] || POLLING_FALLBACK_MESSAGE)
    end
    Adapters::Polling.new(directory, options, &callback)
  end
end

Instance Method Details

#startObject

Start the adapter.



65
66
67
# File 'lib/listen/adapter.rb', line 65

def start
  @stop = false
end

#stopObject

Stop the adapter.



71
72
73
# File 'lib/listen/adapter.rb', line 71

def stop
  @stop = true
end