file-monitor 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -17,19 +17,15 @@ Features
17
17
 
18
18
  If make a new directory in the watched directory, the new directory will be watched automatically.
19
19
 
20
- 2. Support Ignore Dirs:
20
+ 2. Support Filter:
21
21
 
22
- Any directory matched Regexp pattern ignored_dirs will not be watched.
23
-
24
- 3. Support Ignore Files:
25
-
26
- Any files matched Regexp pattern ignored_files will not be recorded.
27
-
28
- 4. Events Buffer Mechanism:
22
+ Support filter directories or files.
23
+
24
+ 3. Events Buffer Mechanism:
29
25
 
30
26
  To avoid run the check methods too quickly, for example, when delete 20 files at the same time, if without Events Buffer will run the check methods 20 times. the frequency of file-monitor is 0.2 second
31
27
 
32
- 5. High Performance
28
+ 4. High Performance
33
29
 
34
30
  For Ruby File Monitor use inotify library, so it's very fast, even watching thousands of directories.
35
31
 
@@ -48,74 +44,32 @@ Install from gem server
48
44
 
49
45
  Usage
50
46
  -----
51
- ### Using block
52
-
53
- #!/usr/bin/env ruby
54
- # coding: utf-8
55
- # File: examples/use-block.rb
56
-
57
- require 'file-monitor'
58
-
59
- dir = ARGV[0] || '.'
60
- m = FileMonitor.new(dir)
61
-
62
- # ignore any dirs contains .git on .svn
63
- m.ignored_dirs = /\.git|\.svn/
64
-
65
- # ignore any files contains .swp or ~
66
- m.ignored_files = /\.swp|~/
67
47
 
68
- # the block's events contains all file modified infomation in last 0.2 second
69
- m.run do|events|
70
- puts "#{events.size} events"
71
- puts "do something"
72
- end
48
+ require 'rubygems'
73
49
 
74
- ### Using inherit
50
+ # watch current working directory
51
+ FileMonitor.watch '.' do
75
52
 
76
- #!/usr/bin/env ruby
77
- # coding: utf-8
78
- # File: examples/use-inherit.rb
53
+ # do not watch directory end with git or svn
54
+ # the last charactor '/' has been trimmed already
55
+ dirs {
56
+ disallow /git$|svn$/
57
+ }
79
58
 
80
- require 'file-monitor'
59
+ # record ruby files only
60
+ # it equals files /\.rb$/
61
+ files {
62
+ disallow //
63
+ allow /\.rb$/
64
+ }
81
65
 
82
- class MyFileMonitor < FileMonitor
83
- def check(events)
84
- puts "#{events.size} events"
66
+ # The commands will be runned when file changed
67
+ # the events contains all file modified infomation in last 0.2 second
68
+ exec {|events|
69
+ puts events.size()
85
70
  puts "do something"
86
- end
87
- end
88
-
89
- dir = ARGV[0] || '.'
90
- m = MyFileMonitor.new(dir)
91
- m.ignored_dirs = /\.git|\.svn/
92
- m.ignored_files = /\.swp|~/
93
- m.run
94
-
95
- If block exists, the check method will be ignored.
96
-
97
- ### Filter mode
98
-
99
- #!/usr/bin/env ruby
100
- # coding: utf-8
101
- # File: examples/use-filter.rb
102
-
103
- require 'file-monitor.rb'
104
-
105
- m = FileMonitor.new('.')
106
- m.filter_dirs {
107
- disallow /\.git|\.svn/
108
- }
71
+ }
109
72
 
110
- # record .rb files only
111
- m.filter_files {
112
- disallow /.*/
113
- allow /\.rb$/
114
- }
115
-
116
- m.run do|events|
117
- puts events.size()
118
- puts "do something"
119
73
  end
120
74
 
121
75
  Examples
@@ -154,9 +108,9 @@ f5.rb requires sinatra.
154
108
 
155
109
  $ ruby examples/use-block.rb
156
110
  watching .
157
- watching ./lib/
158
- ignore ./.git/
159
- watching ./examples/
111
+ watching ./lib
112
+ ignore ./.git
113
+ watching ./examples
160
114
 
161
115
  Edit and save README.md by gvim, examples/use-block outputs
162
116
 
@@ -12,10 +12,16 @@ require 'file-monitor'
12
12
  dir = ARGV[0] || '.'
13
13
  m = FileMonitor.new(dir)
14
14
  # ignore any dirs end with .git on .svn
15
- m.ignored_dirs = /\.git|\.svn/
15
+ # The alias of m.filter_dirs {
16
+ # disallow /\.git|\.svn/
17
+ # }
18
+ m.ignore_dirs /\.git|\.svn/
16
19
 
17
20
  # ignore any files end with .swp or ~
18
- m.ignored_files = /\.swp|~/
21
+ # The alias of m.filter_files {
22
+ # disallow /\.swp|~/
23
+ # }
24
+ m.ignore_files /\.swp|~/
19
25
 
20
26
  # the block's events contains all file modified infomation in last 0.2 second
21
27
  m.run do|events|
@@ -19,9 +19,15 @@ end
19
19
  dir = ARGV[0] || '.'
20
20
  m = MyFileMonitor.new(dir)
21
21
  # ignore any dirs end with .git on .svn
22
- m.ignored_dirs = /\.git|\.svn/
22
+ # The alias of m.filter_dirs {
23
+ # disallow /\.git|\.svn/
24
+ # }
25
+ m.ignore_dirs /\.git|\.svn/
23
26
 
24
27
  # ignore any files end with .swp or ~
25
- m.ignored_files = /\.swp|~/
28
+ # The alias of m.filter_files {
29
+ # disallow /\.swp|~/
30
+ # }
31
+ m.ignore_files /\.swp|~/
26
32
 
27
33
  m.run
@@ -31,11 +31,27 @@ class FileMonitorFilter
31
31
  def reset
32
32
  @patterns = []
33
33
  end
34
+
35
+ alias_method :d, :disallow
36
+ alias_method :a, :allow
34
37
  end
35
38
 
36
39
  class FileMonitor
37
40
  # do the action every @frequency second, to avoid check too frequently
38
- attr_accessor :frequency
41
+ def frequency=(frequency)
42
+ @frequency = frequency
43
+ end
44
+
45
+ def frequency(*args)
46
+ if args.size()
47
+ @frequency = args[0]
48
+ end
49
+ @frequency
50
+ end
51
+
52
+ def set_frequency(frequency)
53
+ @frequency = frequency
54
+ end
39
55
 
40
56
  def initialize(project_dir = '.')
41
57
  @notifier = INotify::Notifier.new
@@ -69,19 +85,37 @@ class FileMonitor
69
85
  end
70
86
 
71
87
  # New Filter mode
72
- def filter(type, &block)
73
- @filters[type].instance_eval &block
88
+ def filter(type, args = [], &block)
89
+ @filters[type].reset()
90
+ if block_given?
91
+ if args.size() > 0
92
+ $stderr.puts "filter's params #{args.to_s} has been ignored for the block is given"
93
+ end
94
+ @filters[type].instance_eval &block
95
+ elsif args.size()
96
+ @filters[type].instance_eval do
97
+ disallow //
98
+ for arg in args
99
+ allow arg
100
+ end
101
+ end
102
+ else
103
+ raise 'filter needs params or block'
104
+ end
74
105
  end
75
106
 
76
- def filter_files(&block)
77
- filter :files, &block
107
+ def filter_files(*args, &block)
108
+ filter :files, args, &block
78
109
  end
79
110
 
80
- def filter_dirs(&block)
81
- filter :dirs, &block
111
+ def filter_dirs(*args, &block)
112
+ filter :dirs, args, &block
82
113
  end
83
114
 
84
115
  def ignored_dir?(path)
116
+ if path == '.'
117
+ return false
118
+ end
85
119
  @filters[:dirs].ignored? path
86
120
  end
87
121
 
@@ -92,9 +126,17 @@ class FileMonitor
92
126
  def push_event event
93
127
  @events << event
94
128
  end
129
+
130
+ def trim_dir(path)
131
+ if path =~ /(.*)\/$/
132
+ path = $1
133
+ end
134
+ path
135
+ end
95
136
 
96
137
  # Watch a directory
97
138
  def watch(dir)
139
+ dir = trim_dir(dir)
98
140
  if ignored_dir?(dir)
99
141
  puts "ignore #{dir}"
100
142
  return false
@@ -192,4 +234,19 @@ class FileMonitor
192
234
  end
193
235
  end
194
236
  end
237
+
238
+ alias_method :dirs, :filter_dirs
239
+ alias_method :files, :filter_files
240
+ alias_method :exec, :run
241
+
242
+ def self.create(dir = '.', &block)
243
+ m = FileMonitor.new(dir)
244
+ m.instance_eval &block
245
+ m
246
+ end
247
+
248
+ def self.watch(dir = '.', &block)
249
+ self.create(dir, &block)
250
+ end
251
+
195
252
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - JiangMiao
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-10-16 00:00:00 +08:00
17
+ date: 2011-10-17 00:00:00 +08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -47,7 +47,7 @@ dependencies:
47
47
  version: 0.8.8
48
48
  type: :development
49
49
  version_requirements: *id002
50
- description: Ruby File Monitor is a easy way to watch the directories and files, do anything when them changed.
50
+ description: Ruby File Monitor is a easy way to watch the directories and files, execute commands when them changed.
51
51
  email: jiangfriend@gmail.com
52
52
  executables: []
53
53