Parent

Listen::Listener

Attributes

adapter[R]
adapter_options[R]
block[R]
directories[R]
directories_records[R]
use_relative_paths[R]

Public Class Methods

new(*args, &block) click to toggle source

Initializes the directories listener.

@param [String] directory the directories to listen to @param [Hash] options the listen options @option options [Regexp] ignore a pattern for ignoring paths @option options [Regexp] filter a pattern for filtering paths @option options [Float] latency the delay between checking for changes in seconds @option options [Boolean] relative_paths whether or not to use relative-paths in the callback @option options [Boolean] force_polling whether to force the polling adapter or not @option options [String, Boolean] polling_fallback_message to change polling fallback message or remove it

@yield [modified, added, removed] the changed files @yieldparam [Array<String>] modified the list of modified files @yieldparam [Array<String>] added the list of added files @yieldparam [Array<String>] removed the list of removed files

# File lib/listen/listener.rb, line 30
def initialize(*args, &block)
  options     = args.last.is_a?(Hash) ? args.pop : {}
  directories = args.flatten
  initialize_directories_and_directories_records(directories)
  initialize_relative_paths_usage(options)
  @block = block

  ignore(*options.delete(:ignore))
  filter(*options.delete(:filter))

  @adapter_options = options
end

Public Instance Methods

change(&block) click to toggle source

Sets the callback that gets called on changes.

@example Assign a callback to be called on changes

callback = lambda { |modified, added, removed| ... }
change &callback

@param [Proc] block the callback proc

@return [Listen::Listener] the listener

# File lib/listen/listener.rb, line 222
def change(&block) # modified, added, removed
  @block = block
  self
end
filter(*regexps) click to toggle source

Adds filtering patterns to the listener.

@param (see Listen::DirectoryRecord#filter)

@return [Listen::Listener] the listener

@see Listen::DirectoryRecord#filter

# File lib/listen/listener.rb, line 135
def filter(*regexps)
  directories_records.each { |r| r.filter(*regexps) }
  self
end
filter!(*regexps) click to toggle source

Replaces filtering patterns in the listener.

@param (see Listen::DirectoryRecord#filter!)

@return [Listen::Listener] the listener

@see Listen::DirectoryRecord#filter!

# File lib/listen/listener.rb, line 148
def filter!(*regexps)
  directories_records.each { |r| r.filter!(*regexps) }
  self
end
force_polling(value) click to toggle source

Sets whether the use of the polling adapter should be forced or not.

@example Forcing the use of the polling adapter

force_polling true

@param [Boolean] value whether to force the polling adapter or not

@return [Listen::Listener] the listener

# File lib/listen/listener.rb, line 178
def force_polling(value)
  @adapter_options[:force_polling] = value
  self
end
ignore(*regexps) click to toggle source

Adds ignoring patterns to the listener.

@param (see Listen::DirectoryRecord#ignore)

@return [Listen::Listener] the listener

@see Listen::DirectoryRecord#ignore

# File lib/listen/listener.rb, line 109
def ignore(*regexps)
  directories_records.each { |r| r.ignore(*regexps) }
  self
end
ignore!(*regexps) click to toggle source

Replaces ignoring patterns in the listener.

@param (see Listen::DirectoryRecord#ignore!)

@return [Listen::Listener] the listener

@see Listen::DirectoryRecord#ignore!

# File lib/listen/listener.rb, line 122
def ignore!(*regexps)
  directories_records.each { |r| r.ignore!(*regexps) }
  self
end
latency(seconds) click to toggle source

Sets the latency for the adapter. This is a helper method to simplify changing the latency directly from the listener.

@example Wait 0.5 seconds each time before checking changes

latency 0.5

@param [Float] seconds the amount of delay, in seconds

@return [Listen::Listener] the listener

# File lib/listen/listener.rb, line 163
def latency(seconds)
  @adapter_options[:latency] = seconds
  self
end
on_change(directories, options = {}) click to toggle source

Runs the callback passing it the changes if there are any.

@param (see Listen::DirectoryRecord#fetch_changes)

@see Listen::DirectoryRecord#fetch_changes

# File lib/listen/listener.rb, line 233
def on_change(directories, options = {})
  changes = fetch_records_changes(directories, options)
  unless changes.values.all? { |paths| paths.empty? }
    block.call(changes[:modified], changes[:added], changes[:removed])
  end
end
pause() click to toggle source

Pauses the listener.

@return [Listen::Listener] the listener

# File lib/listen/listener.rb, line 78
def pause
  adapter.pause
  self
end
paused?() click to toggle source

Returns whether the listener is paused or not.

@return [Boolean] adapter paused status

# File lib/listen/listener.rb, line 97
def paused?
  !!adapter && adapter.paused?
end
polling_fallback_message(value) click to toggle source

Defines a custom polling fallback message or disable it.

@example Disabling the polling fallback message

polling_fallback_message false

@param [String, Boolean] value to change polling fallback message or remove it

@return [Listen::Listener] the listener

# File lib/listen/listener.rb, line 207
def polling_fallback_message(value)
  @adapter_options[:polling_fallback_message] = value
  self
end
relative_paths(value) click to toggle source

Sets whether the paths in the callback should be relative or absolute.

@example Enabling relative paths in the callback

relative_paths true

@param [Boolean] value whether to enable relative paths in the callback or not

@return [Listen::Listener] the listener

# File lib/listen/listener.rb, line 193
def relative_paths(value)
  @use_relative_paths = value
  self
end
start(deprecated_blocking = nil) click to toggle source

Starts the listener by initializing the adapter and building the directory record concurrently, then it starts the adapter to watch for changes. The current thread is not blocked after starting.

@see Listen::Listener#start!

# File lib/listen/listener.rb, line 49
def start(deprecated_blocking = nil)
  Kernel.warn "[Listen warning]:\n#{BLOCKING_PARAMETER_DEPRECATION_MESSAGE}" unless deprecated_blocking.nil?
  setup
  adapter.start
end
start!() click to toggle source

Starts the listener by initializing the adapter and building the directory record concurrently, then it starts the adapter to watch for changes. The current thread is blocked after starting.

@see Listen::Listener#start

@since 1.0.0

# File lib/listen/listener.rb, line 63
def start!
  setup
  adapter.start!
end
stop() click to toggle source

Stops the listener.

# File lib/listen/listener.rb, line 70
def stop
  adapter && adapter.stop
end
unpause() click to toggle source

Unpauses the listener.

@return [Listen::Listener] the listener

# File lib/listen/listener.rb, line 87
def unpause
  build_directories_records
  adapter.unpause
  self
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.