filewatch 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/filewatch/tail.rb +57 -0
  2. metadata +3 -2
@@ -0,0 +1,57 @@
1
+ require "filewatch/watch"
2
+ require "filewatch/namespace"
3
+
4
+ class FileWatch::Tail
5
+ # This class exists to wrap inotify, kqueue, periodic polling, etc,
6
+ # to provide you with a way to watch files and directories.
7
+ #
8
+ # For now, it only supports inotify.
9
+ def initialize
10
+ @watch = FileWatch::Watch.new
11
+ @files = {}
12
+ end
13
+
14
+ public
15
+ def watch(path, *what_to_watch)
16
+ @watch.watch(path, *what_to_watch)
17
+
18
+ if File.file?(path)
19
+ @files[path] = File.new(path, "r")
20
+
21
+ # TODO(sissel): Support 'since'-like support.
22
+ # Always start at the end of the file, this may change in the future.
23
+ @files[path].sysseek(0, IO::SEEK_END)
24
+ end
25
+ end # def watch
26
+
27
+ def subscribe(handler=nil, &block)
28
+ @watch.subscribe(nil) do |event|
29
+ path = event.name
30
+ if @files.include?(path)
31
+ file = @files[path]
32
+ event.actions.each do |action|
33
+ method = "file_action_#{action}".to_sym
34
+ if respond_to?(method)
35
+ send(method, file, event, &block)
36
+ else
37
+ $stderr.puts "Unsupported method #{self.class.name}##{method}"
38
+ end
39
+ end
40
+ else
41
+ $stderr.puts "Event on unwatched file: #{event}"
42
+ end
43
+ end
44
+ end # def subscribe
45
+
46
+ def file_action_modify(file, event)
47
+ loop do
48
+ begin
49
+ data = file.sysread(4096)
50
+ yield event.name, data
51
+ rescue EOFError
52
+ break
53
+ end
54
+ end
55
+ end
56
+
57
+ end # class FileWatch::Tail
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: filewatch
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.1
5
+ version: 0.1.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jordan Sissel
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-18 00:00:00 -07:00
13
+ date: 2011-03-19 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
@@ -25,6 +25,7 @@ extra_rdoc_files: []
25
25
  files:
26
26
  - lib/filewatch/watch.rb
27
27
  - lib/filewatch/namespace.rb
28
+ - lib/filewatch/tail.rb
28
29
  - lib/filewatch/stringpipeio.rb
29
30
  - lib/filewatch/inotify/emhandler.rb
30
31
  - lib/filewatch/inotify/fd.rb