Class: Listen::MultiListener

Inherits:
Listener show all
Defined in:
lib/listen/multi_listener.rb

Constant Summary

Constants inherited from Listener

Listener::DEFAULT_TO_RELATIVE_PATHS

Instance Attribute Summary collapse

Attributes inherited from Listener

#directory, #directory_record

Instance Method Summary collapse

Methods inherited from Listener

#change, #force_polling, #latency, #pause, #paused?, #polling_fallback_message, #relative_paths, #stop

Constructor Details

#initialize(*args) {|modified, added, removed| ... } ⇒ MultiListener

Initializes the multiple directories listener.

Parameters:

  • directories (String)

    the directories to listen to

  • options (Hash)

    the listen options

Yields:

  • (modified, added, removed)

    the changed files

Yield Parameters:

  • modified (Array<String>)

    the list of modified files

  • added (Array<String>)

    the list of added files

  • removed (Array<String>)

    the list of removed files



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/listen/multi_listener.rb', line 20

def initialize(*args, &block)
  options     = args.last.is_a?(Hash) ? args.pop : {}
  directories = args

  @block               = block
  @directories         = directories.map  { |d| Pathname.new(d).realpath.to_s }
  @directories_records = @directories.map { |d| DirectoryRecord.new(d) }

  ignore(*options.delete(:ignore)) if options[:ignore]
  filter(*options.delete(:filter)) if options[:filter]

  @adapter_options = options
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



3
4
5
# File 'lib/listen/multi_listener.rb', line 3

def adapter
  @adapter
end

#directoriesObject (readonly)

Returns the value of attribute directories.



3
4
5
# File 'lib/listen/multi_listener.rb', line 3

def directories
  @directories
end

#directories_recordsObject (readonly)

Returns the value of attribute directories_records.



3
4
5
# File 'lib/listen/multi_listener.rb', line 3

def directories_records
  @directories_records
end

Instance Method Details

#filter(*regexps) ⇒ Listen::Listener

Adds file filters to the listener.

Parameters:

  • regexp (Regexp)

    a pattern for filtering paths

Returns:



85
86
87
88
# File 'lib/listen/multi_listener.rb', line 85

def filter(*regexps)
  @directories_records.each { |r| r.filter(*regexps) }
  self
end

#filter!(*regexps) ⇒ Listen::Listener

Replaces file filters in the listener.

Parameters:

  • regexp (Regexp)

    a pattern for filtering paths

Returns:



96
97
98
99
# File 'lib/listen/multi_listener.rb', line 96

def filter!(*regexps)
  @directories_records.each { |r| r.filter!(*regexps) }
  self
end

#ignore(*paths) ⇒ Listen::Listener

Adds ignored paths to the listener.

Parameters:

  • regexp (Regexp)

    a pattern for ignoring paths

Returns:



63
64
65
66
# File 'lib/listen/multi_listener.rb', line 63

def ignore(*paths)
  @directories_records.each { |r| r.ignore(*paths) }
  self
end

#ignore!(*paths) ⇒ Listen::Listener

Replaces ignored paths in the listener.

Parameters:

  • regexp (Regexp)

    a pattern for ignoring paths

Returns:



74
75
76
77
# File 'lib/listen/multi_listener.rb', line 74

def ignore!(*paths)
  @directories_records.each { |r| r.ignore!(*paths) }
  self
end

#on_change(directories_to_search, options = {}) ⇒ Object

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

Parameters:

  • directories (Array)

    the list of directories scan for changes

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


105
106
107
108
109
110
# File 'lib/listen/multi_listener.rb', line 105

def on_change(directories_to_search, options = {})
  changes = fetch_records_changes(directories_to_search, options)
  unless changes.values.all? { |paths| paths.empty? }
    @block.call(changes[:modified],changes[:added],changes[:removed])
  end
end

#start(blocking = true) ⇒ Object

Starts the listener by initializing the adapter and building the directory record concurrently, then it starts the adapter to watch for changes.

Parameters:

  • blocking (Boolean) (defaults to: true)

    whether or not to block the current thread after starting



40
41
42
43
44
45
# File 'lib/listen/multi_listener.rb', line 40

def start(blocking = true)
  t = Thread.new { @directories_records.each { |r| r.build } }
  @adapter = initialize_adapter
  t.join
  @adapter.start(blocking)
end

#unpauseListen::Listener

Unpauses the listener.

Returns:



51
52
53
54
55
# File 'lib/listen/multi_listener.rb', line 51

def unpause
  @directories_records.each { |r| r.build }
  @adapter.paused = false
  self
end