Class: Listen::Listener
- Inherits:
-
Object
- Object
- Listen::Listener
- Defined in:
- lib/listen/listener.rb
Constant Summary collapse
- DEFAULT_IGNORED_PATHS =
Default paths that gets ignored by the listener
%w[.bundle .git .DS_Store log tmp vendor]
Instance Attribute Summary collapse
-
#directory ⇒ Object
Returns the value of attribute directory.
-
#file_filters ⇒ Object
Returns the value of attribute file_filters.
-
#ignored_paths ⇒ Object
Returns the value of attribute ignored_paths.
-
#paths ⇒ Object
Returns the value of attribute paths.
-
#paused ⇒ Object
Returns the value of attribute paused.
-
#sha1_checksums ⇒ Object
Returns the value of attribute sha1_checksums.
Instance Method Summary collapse
-
#change(&block) ⇒ Listen::Listener
Set change callback block to the listener.
-
#diff(directories, options = {}) ⇒ Hash<Array>
Detect changes diff in a directory.
-
#filter(*regexps) ⇒ Listen::Listener
Add file filters to the listener.
-
#force_polling(value) ⇒ Listen::Listener
Defines whether the use of the polling adapter should be forced or not.
-
#ignore(*paths) ⇒ Listen::Listener
Add ignored path to the listener.
-
#init_paths ⇒ Object
Initialize the @paths double levels Hash with all existing paths and set diffed_at.
-
#initialize(directory, options = {}) {|modified, added, removed| ... } ⇒ Listen::Listener
constructor
Initialize the file listener.
-
#latency(seconds) ⇒ Listen::Listener
Sets the latency for the adapter.
-
#on_change(directories, diff_options = {}) ⇒ Object
Call @block callback when there is a diff in the passed directory.
-
#pause ⇒ Object
Stop the adapter and wait.
-
#paused? ⇒ Boolean
Is this listener paused.
-
#polling_fallback_message(value) ⇒ Listen::Listener
Defines a custom polling fallback message of disable it.
-
#start ⇒ Object
Initialize the adapter and the @paths concurrently and start the adapter.
-
#stop ⇒ Object
Stop the adapter.
-
#unpause ⇒ Object
UnPause and re-initialize @paths and start the adapter.
Constructor Details
#initialize(directory, options = {}) {|modified, added, removed| ... } ⇒ Listen::Listener
Initialize the file listener.
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/listen/listener.rb', line 34 def initialize(directory, = {}, &block) @directory = directory @ignored_paths = DEFAULT_IGNORED_PATHS @file_filters = [] @sha1_checksums = {} @block = block @ignored_paths += Array(.delete(:ignore)) if [:ignore] @file_filters += Array(.delete(:filter)) if [:filter] = end |
Instance Attribute Details
#directory ⇒ Object
Returns the value of attribute directory.
12 13 14 |
# File 'lib/listen/listener.rb', line 12 def directory @directory end |
#file_filters ⇒ Object
Returns the value of attribute file_filters.
12 13 14 |
# File 'lib/listen/listener.rb', line 12 def file_filters @file_filters end |
#ignored_paths ⇒ Object
Returns the value of attribute ignored_paths.
12 13 14 |
# File 'lib/listen/listener.rb', line 12 def ignored_paths @ignored_paths end |
#paths ⇒ Object
Returns the value of attribute paths.
12 13 14 |
# File 'lib/listen/listener.rb', line 12 def paths @paths end |
#paused ⇒ Object
Returns the value of attribute paused.
12 13 14 |
# File 'lib/listen/listener.rb', line 12 def paused @paused end |
#sha1_checksums ⇒ Object
Returns the value of attribute sha1_checksums.
12 13 14 |
# File 'lib/listen/listener.rb', line 12 def sha1_checksums @sha1_checksums end |
Instance Method Details
#change(&block) ⇒ Listen::Listener
Set change callback block to the listener.
171 172 173 174 |
# File 'lib/listen/listener.rb', line 171 def change(&block) # modified, added, removed @block = block self end |
#diff(directories, options = {}) ⇒ Hash<Array>
Detect changes diff in a directory.
202 203 204 205 206 207 208 209 210 211 |
# File 'lib/listen/listener.rb', line 202 def diff(directories, = {}) @changes = { :modified => [], :added => [], :removed => [] } directories = directories.sort_by { |el| el.length }.reverse # diff sub-dir first directories.each do |directory| detect_modifications_and_removals(directory, ) detect_additions(directory, ) end @diffed_at = Time.now.to_i @changes end |
#filter(*regexps) ⇒ Listen::Listener
Add file filters to the listener.
112 113 114 115 |
# File 'lib/listen/listener.rb', line 112 def filter(*regexps) @file_filters.push(*regexps) self end |
#force_polling(value) ⇒ Listen::Listener
Defines whether the use of the polling adapter should be forced or not.
142 143 144 145 |
# File 'lib/listen/listener.rb', line 142 def force_polling(value) [:force_polling] = value self end |
#ignore(*paths) ⇒ Listen::Listener
Add ignored path to the listener.
98 99 100 101 |
# File 'lib/listen/listener.rb', line 98 def ignore(*paths) @ignored_paths.push(*paths) self end |
#init_paths ⇒ Object
Initialize the @paths double levels Hash with all existing paths and set diffed_at.
189 190 191 192 193 |
# File 'lib/listen/listener.rb', line 189 def init_paths @paths = Hash.new { |h,k| h[k] = {} } all_existing_paths { |path| insert_path(path) } @diffed_at = Time.now.to_i end |
#latency(seconds) ⇒ Listen::Listener
Sets the latency for the adapter. This is a helper method to simplify changing the latency directly from the listener.
127 128 129 130 |
# File 'lib/listen/listener.rb', line 127 def latency(seconds) [:latency] = seconds self end |
#on_change(directories, diff_options = {}) ⇒ Object
Call @block callback when there is a diff in the passed directory.
180 181 182 183 184 185 |
# File 'lib/listen/listener.rb', line 180 def on_change(directories, = {}) changes = diff(directories, ) unless changes.values.all? { |paths| paths.empty? } @block.call(changes[:modified],changes[:added],changes[:removed]) end end |
#pause ⇒ Object
Stop the adapter and wait
67 68 69 70 71 |
# File 'lib/listen/listener.rb', line 67 def pause @paused = true @adapter.stop sleep 0.1 while @paused end |
#paused? ⇒ Boolean
Is this listener paused
85 86 87 |
# File 'lib/listen/listener.rb', line 85 def paused? @paused == true end |
#polling_fallback_message(value) ⇒ Listen::Listener
Defines a custom polling fallback message of disable it.
156 157 158 159 |
# File 'lib/listen/listener.rb', line 156 def (value) [:polling_fallback_message] = value self end |
#start ⇒ Object
Initialize the adapter and the @paths concurrently and start the adapter.
48 49 50 51 52 53 |
# File 'lib/listen/listener.rb', line 48 def start Thread.new { @adapter = initialize_adapter } init_paths sleep 0.01 while @adapter.nil? @adapter.start end |
#stop ⇒ Object
Stop the adapter.
57 58 59 60 61 62 63 |
# File 'lib/listen/listener.rb', line 57 def stop if @paused @paused = false else @adapter.stop end end |
#unpause ⇒ Object
UnPause and re-initialize @paths and start the adapter
75 76 77 78 79 |
# File 'lib/listen/listener.rb', line 75 def unpause @paused = false init_paths @adapter.start end |