directory_watcher 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +3 -0
- data/lib/directory_watcher.rb +40 -16
- metadata +3 -3
data/Rakefile
CHANGED
@@ -19,6 +19,9 @@ dispatched to registered observers. Three types of events are supported --
|
|
19
19
|
added, modified, and removed.
|
20
20
|
DESC
|
21
21
|
proj.changes = <<-CHANGES
|
22
|
+
Version 1.1.0 / 2007-11-28
|
23
|
+
* directory watcher now works with Ruby 1.9
|
24
|
+
|
22
25
|
Version 1.0.0 / 2007-08-21
|
23
26
|
* added a join method (much like Thread#join)
|
24
27
|
|
data/lib/directory_watcher.rb
CHANGED
@@ -149,9 +149,8 @@ require 'observer'
|
|
149
149
|
# Tim Pease
|
150
150
|
#
|
151
151
|
class DirectoryWatcher
|
152
|
-
include Observable
|
153
152
|
|
154
|
-
VERSION = '1.
|
153
|
+
VERSION = '1.1.0' # :nodoc:
|
155
154
|
|
156
155
|
# An +Event+ structure contains the _type_ of the event and the file _path_
|
157
156
|
# to which the event pertains. The type can be one of the following:
|
@@ -210,29 +209,54 @@ class DirectoryWatcher
|
|
210
209
|
@files = (opts[:pre_load] ? scan_files : Hash.new)
|
211
210
|
@events = []
|
212
211
|
@thread = nil
|
212
|
+
@observer_peers = {}
|
213
213
|
end
|
214
214
|
|
215
215
|
# call-seq:
|
216
|
-
# add_observer( observer )
|
216
|
+
# add_observer( observer, func = :update )
|
217
217
|
# add_observer {|*events| block}
|
218
218
|
#
|
219
219
|
# Adds the given _observer_ as an observer on this directory watcher. The
|
220
220
|
# _observer_ will now receive file events when they are generated. The
|
221
|
-
#
|
221
|
+
# second optional argument specifies a method to notify updates, of which
|
222
|
+
# the default value is +update+.
|
222
223
|
#
|
223
224
|
# Optionally, a block can be passed as the observer. The block will be
|
224
225
|
# executed with the file events passed as the arguments. A reference to the
|
225
226
|
# underlying +Proc+ object will be returned for use with the
|
226
227
|
# +delete_observer+ method.
|
227
228
|
#
|
228
|
-
def add_observer( observer = nil, &block )
|
229
|
+
def add_observer( observer = nil, func = :update, &block )
|
229
230
|
unless block.nil?
|
230
231
|
observer = block.to_proc
|
231
|
-
|
232
|
-
|
233
|
-
|
232
|
+
func = :call
|
233
|
+
end
|
234
|
+
|
235
|
+
unless observer.respond_to? func
|
236
|
+
raise NoMethodError, "observer does not respond to `#{func.to_s}'"
|
234
237
|
end
|
235
|
-
|
238
|
+
|
239
|
+
@observer_peers[observer] = func
|
240
|
+
observer
|
241
|
+
end
|
242
|
+
|
243
|
+
# Delete +observer+ as an observer of this directory watcher. It will no
|
244
|
+
# longer receive notifications.
|
245
|
+
#
|
246
|
+
def delete_observer( observer )
|
247
|
+
@observer_peers.delete observer
|
248
|
+
end
|
249
|
+
|
250
|
+
# Delete all observers associated with the directory watcher.
|
251
|
+
#
|
252
|
+
def delete_observers
|
253
|
+
@observer_peers.clear
|
254
|
+
end
|
255
|
+
|
256
|
+
# Return the number of observers associated with this directory watcher..
|
257
|
+
#
|
258
|
+
def count_observers
|
259
|
+
@observer_peers.size
|
236
260
|
end
|
237
261
|
|
238
262
|
# call-seq:
|
@@ -244,8 +268,8 @@ class DirectoryWatcher
|
|
244
268
|
#
|
245
269
|
def glob=( val )
|
246
270
|
@glob = case val
|
247
|
-
when String
|
248
|
-
when Array
|
271
|
+
when String; [File.join(@dir, val)]
|
272
|
+
when Array; val.flatten.map! {|g| File.join(@dir, g)}
|
249
273
|
else
|
250
274
|
raise(ArgumentError,
|
251
275
|
'expecting a glob pattern or an array of glob patterns')
|
@@ -324,7 +348,7 @@ class DirectoryWatcher
|
|
324
348
|
return if running?
|
325
349
|
|
326
350
|
@stop = false
|
327
|
-
@thread = Thread.new(self) {|dw| dw.
|
351
|
+
@thread = Thread.new(self) {|dw| dw.__send__ :run}
|
328
352
|
self
|
329
353
|
end
|
330
354
|
|
@@ -500,12 +524,12 @@ class DirectoryWatcher
|
|
500
524
|
# The file events array is cleared at the end of this method call.
|
501
525
|
#
|
502
526
|
def notify_observers
|
503
|
-
unless @events.empty?
|
504
|
-
@observer_peers.
|
505
|
-
begin; observer.
|
527
|
+
unless @events.empty?
|
528
|
+
@observer_peers.each do |observer, func|
|
529
|
+
begin; observer.send(func, *@events); rescue Exception; end
|
506
530
|
end
|
531
|
+
@events.clear
|
507
532
|
end
|
508
|
-
@events.clear
|
509
533
|
end
|
510
534
|
|
511
535
|
end # class DirectoryWatcher
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: directory_watcher
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.
|
7
|
-
date: 2007-
|
6
|
+
version: 1.1.0
|
7
|
+
date: 2007-11-28 00:00:00 -07:00
|
8
8
|
summary: A class for watching files within a directory and generating events when those files change
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -55,5 +55,5 @@ dependencies:
|
|
55
55
|
requirements:
|
56
56
|
- - ">="
|
57
57
|
- !ruby/object:Gem::Version
|
58
|
-
version: 1.
|
58
|
+
version: 1.3.0
|
59
59
|
version:
|