filewatcher 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 18429964b2631c8f50fc42b2a919d3dd859608cd
4
- data.tar.gz: e1206dff831c1bb8d2495cc4110b21971c2e6c33
3
+ metadata.gz: 931616017d17d3dbca73187c1b6fe31887975115
4
+ data.tar.gz: 24cec3cbfe8256a906a0067d628438d2bc4176d8
5
5
  SHA512:
6
- metadata.gz: 38c927c846a8844443afb7cb0b403ede8eac5205f3d6833a4220d1e5e6bece967e33602adcdba5b44b922e12bf3300e5043acfb35922067d7cce486de50ddd77
7
- data.tar.gz: 6d732bb3d0cfae8f6c57f5477077ccbc9a3b43b631ec58336f8b5953186233b9cdf8bec08fe136e857d747d6f08b1e9a45b7b9ac799e76c3119c1f4f67e0bda1
6
+ metadata.gz: eddb01f13c12345438352bb1450f22078462d1b392baacdd0796ddf40a82f80aab5164fc2be36d0c15493cf257164de2f41a2b4a46cfe84400b67c4a1438d6d4
7
+ data.tar.gz: efeff47ff4fb01fc3e9af4e9f7a0af47fc33d59fa6b67a19d077055438c43c4839428bc4f8ca8de42d74e44093df2f092532a36e27f5e6efa05b91283a25f91c
@@ -96,6 +96,9 @@ To detect if a file is updated or deleted:
96
96
  if(event == :deleted)
97
97
  puts "Deleted " + filename
98
98
  end
99
+ if(event == :new)
100
+ puts "New file " + filename
101
+ end
99
102
  end
100
103
 
101
104
  = TODO
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
@@ -4,40 +4,26 @@
4
4
  class FileWatcher
5
5
 
6
6
  def self.VERSION
7
- return "0.2.1"
7
+ return "0.3.0"
8
8
  end
9
9
 
10
- def initialize(filenames,print_filelist=false)
11
- if(filenames.kind_of?String)
12
- filenames = [filenames]
13
- end
14
-
15
- filenames = expand_directories(filenames)
16
-
17
- if(print_filelist)
18
- if(print_filelist.kind_of?String)
19
- puts print_filelist
20
- else
21
- puts "Watching:"
22
- end
23
- filenames.each do |filename|
24
- puts filename
25
- end
26
- end
27
-
10
+ def initialize(unexpanded_filenames, print_filelist=false)
11
+ @unexpanded_filenames = unexpanded_filenames
28
12
  @last_mtimes = { }
29
- filenames.each do |filename|
13
+ @filenames = expand_directories(@unexpanded_filenames)
14
+
15
+ puts "Watching:" if print_filelist
16
+ @filenames.each do |filename|
30
17
  raise "File does not exist" unless File.exist?(filename)
31
18
  @last_mtimes[filename] = File.stat(filename).mtime
19
+ puts filename if print_filelist
32
20
  end
33
- @filenames = filenames
34
- @deleted_files = []
35
21
  end
36
22
 
37
23
  def watch(sleep=1, &on_update)
38
24
  loop do
39
25
  begin
40
- Kernel.sleep sleep until file_updated?
26
+ Kernel.sleep sleep until filesystem_updated?
41
27
  rescue SystemExit,Interrupt
42
28
  Kernel.exit
43
29
  end
@@ -45,33 +31,45 @@ class FileWatcher
45
31
  end
46
32
  end
47
33
 
48
- def file_updated?
49
- @filenames.each do |filename|
50
-
51
- if(not(@deleted_files.include?(filename)))
34
+ def filesystem_updated?
35
+ filenames = expand_directories(@unexpanded_filenames)
52
36
 
53
- if(not(File.exist?(filename)))
54
- @deleted_files << filename
55
- @updated_file = filename
56
- @event = :delete
57
- return true
58
- end
59
- mtime = File.stat(filename).mtime
37
+ if(filenames.size > @filenames.size)
38
+ filename = (filenames - @filenames).first
39
+ @filenames << filename
40
+ @last_mtimes[filename] = File.stat(filename).mtime
41
+ @updated_file = filename
42
+ @event = :new
43
+ return true
44
+ end
60
45
 
61
- updated = @last_mtimes[filename] < mtime
62
- @last_mtimes[filename] = mtime
63
- if(updated)
64
- @updated_file = filename
65
- @event = :changed
66
- return true
67
- end
46
+ if(filenames.size < @filenames.size)
47
+ filename = (@filenames - filenames).first
48
+ @filenames.delete(filename)
49
+ @last_mtimes.delete(filename)
50
+ @updated_file = filename
51
+ @event = :delete
52
+ return true
53
+ end
68
54
 
55
+ @filenames.each do |filename|
56
+ mtime = File.stat(filename).mtime
57
+ updated = @last_mtimes[filename] < mtime
58
+ @last_mtimes[filename] = mtime
59
+ if(updated)
60
+ @updated_file = filename
61
+ @event = :changed
62
+ return true
69
63
  end
70
64
  end
65
+
71
66
  return false
72
67
  end
73
68
 
74
69
  def expand_directories(filenames)
70
+ if(filenames.kind_of?String)
71
+ filenames = [filenames]
72
+ end
75
73
  files = []
76
74
  filenames.each do |filename|
77
75
  if(File.directory?(filename))
@@ -5,22 +5,12 @@ class TestFilewatcher < Test::Unit::TestCase
5
5
 
6
6
  should "should detect directories and extract filenames" do
7
7
 
8
- puts " "
9
-
10
8
  FileWatcher.new(["test/"],"Watching files. Ctrl-C to abort.").watch(0.1) do |filename, event|
11
- puts "updated: " + filename.to_s + " event:" + event.to_s
12
- end
13
-
14
- FileWatcher.new(["./test/", "Rakefile", "lib"]).watch(0.1) do |filename|
15
- puts "updated: " + filename
9
+ puts "updated: '#{filename.to_s} event:'#{event.to_s}'"
16
10
  end
17
11
 
18
12
  end
19
13
 
20
- should "return version number" do
21
- assert FileWatcher.VERSION.size > 2
22
- end
23
-
24
14
  # should "should detect changes in files" do
25
15
 
26
16
  # @pid = fork do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filewatcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Flemming
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-04 00:00:00.000000000 Z
11
+ date: 2014-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thoughtbot-shoulda
@@ -74,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
74
  version: '0'
75
75
  requirements: []
76
76
  rubyforge_project:
77
- rubygems_version: 2.0.0
77
+ rubygems_version: 2.0.3
78
78
  signing_key:
79
79
  specification_version: 4
80
80
  summary: Simple filewatcher.