file-find 0.2.5 → 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.
- data/CHANGES +4 -0
- data/MANIFEST +1 -1
- data/README +3 -1
- data/lib/file/find.rb +48 -9
- data/test/test_file_find.rb +30 -1
- metadata +2 -2
data/CHANGES
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
== 0.3.0 - 30-Dec-2008
|
2
|
+
* Added support for FileTest operations. All options passed to the constructor
|
3
|
+
that end with '?' are now validated and treated as FileTest operations.
|
4
|
+
|
1
5
|
== 0.2.5 - 29-Dec-2008
|
2
6
|
* Added maxdepth and mindepth support.
|
3
7
|
* Added a 'clean' rake task to cleanup any test-unit results files.
|
data/MANIFEST
CHANGED
data/README
CHANGED
@@ -43,6 +43,9 @@ few options, hasn't been updated in over six years and isn't packaged properly.
|
|
43
43
|
* size
|
44
44
|
* user (name or id)
|
45
45
|
|
46
|
+
In addition to the above options, FileTest methods such as 'readable?' and
|
47
|
+
'writable?' may be used as keys, with true or false for their values.
|
48
|
+
|
46
49
|
See the RDoc documentation for more details about these options.
|
47
50
|
|
48
51
|
= Future Plans
|
@@ -52,7 +55,6 @@ http://www.rubyforge.org/projects/shards.
|
|
52
55
|
|
53
56
|
Some specific things I plan on adding:
|
54
57
|
|
55
|
-
* general FileTest operations
|
56
58
|
* exec
|
57
59
|
* links
|
58
60
|
* support for :user and :group on MS Windows
|
data/lib/file/find.rb
CHANGED
@@ -6,7 +6,7 @@ include Sys
|
|
6
6
|
|
7
7
|
class File::Find
|
8
8
|
# The version of this library
|
9
|
-
VERSION = '0.
|
9
|
+
VERSION = '0.3.0'
|
10
10
|
|
11
11
|
# :stopdoc:
|
12
12
|
VALID_OPTIONS = %w/
|
@@ -58,6 +58,11 @@ class File::Find
|
|
58
58
|
#
|
59
59
|
attr_accessor :group
|
60
60
|
|
61
|
+
# An array of two element arrays for storing FileTest methods and their
|
62
|
+
# boolean value.
|
63
|
+
#
|
64
|
+
attr_accessor :filetest
|
65
|
+
|
61
66
|
# Controls the behavior of how symlinks are followed. If set to true (the
|
62
67
|
# default), then follows the file pointed to. If false, it considers the
|
63
68
|
# symlink itself.
|
@@ -135,13 +140,17 @@ class File::Find
|
|
135
140
|
# object serve as the rules for determining what files the File::Find#find
|
136
141
|
# method will search for.
|
137
142
|
#
|
143
|
+
# In addition to the standard list of valid options, you may also use
|
144
|
+
# FileTest methods as options, setting their value to true or false.
|
145
|
+
#
|
138
146
|
# Example:
|
139
147
|
#
|
140
148
|
# rule = File::Find.new(
|
141
|
-
# :name
|
142
|
-
# :follow
|
143
|
-
# :path
|
144
|
-
#
|
149
|
+
# :name => "*.rb",
|
150
|
+
# :follow => false,
|
151
|
+
# :path => ['/usr/local/lib', '/opt/local/lib'],
|
152
|
+
# :readable? => true
|
153
|
+
# )
|
145
154
|
#
|
146
155
|
def initialize(options = {})
|
147
156
|
@options = options
|
@@ -161,6 +170,7 @@ class File::Find
|
|
161
170
|
@previous = nil
|
162
171
|
@maxdepth = nil
|
163
172
|
@mindepth = nil
|
173
|
+
@filetest = []
|
164
174
|
|
165
175
|
validate_and_set_options(options) unless options.empty?
|
166
176
|
|
@@ -262,6 +272,22 @@ class File::Find
|
|
262
272
|
|
263
273
|
next unless Dir[glob].include?(file)
|
264
274
|
|
275
|
+
unless @filetest.empty?
|
276
|
+
file_test = true
|
277
|
+
|
278
|
+
@filetest.each{ |array|
|
279
|
+
meth = array[0]
|
280
|
+
bool = array[1]
|
281
|
+
|
282
|
+
unless File.send(meth, file) == bool
|
283
|
+
file_test = false
|
284
|
+
break
|
285
|
+
end
|
286
|
+
}
|
287
|
+
|
288
|
+
next unless file_test
|
289
|
+
end
|
290
|
+
|
265
291
|
if @atime
|
266
292
|
date1 = Date.parse(Time.now.to_s)
|
267
293
|
date2 = Date.parse(stat_info.atime.to_s)
|
@@ -356,15 +382,28 @@ class File::Find
|
|
356
382
|
private
|
357
383
|
|
358
384
|
# This validates that the keys are valid. If they are, it sets the value
|
359
|
-
# of that key's corresponding method to the given value.
|
385
|
+
# of that key's corresponding method to the given value. If a key ends
|
386
|
+
# with a '?', it's validated as a File method.
|
360
387
|
#
|
361
388
|
def validate_and_set_options(options)
|
362
389
|
options.each do |key, value|
|
363
390
|
key = key.to_s.downcase
|
364
|
-
|
365
|
-
|
391
|
+
|
392
|
+
if key[-1].chr == '?'
|
393
|
+
sym = key.to_sym
|
394
|
+
|
395
|
+
unless File.respond_to?(sym)
|
396
|
+
raise ArgumentError, "invalid option '#{key}'"
|
397
|
+
end
|
398
|
+
|
399
|
+
@filetest << [sym, value]
|
400
|
+
else
|
401
|
+
unless VALID_OPTIONS.include?(key)
|
402
|
+
raise ArgumentError, "invalid option '#{key}'"
|
403
|
+
end
|
404
|
+
|
405
|
+
send("#{key}=", value)
|
366
406
|
end
|
367
|
-
send("#{key}=", value)
|
368
407
|
end
|
369
408
|
end
|
370
409
|
|
data/test/test_file_find.rb
CHANGED
@@ -52,7 +52,7 @@ class TC_File_Find < Test::Unit::TestCase
|
|
52
52
|
end
|
53
53
|
|
54
54
|
def test_version
|
55
|
-
assert_equal('0.
|
55
|
+
assert_equal('0.3.0', File::Find::VERSION)
|
56
56
|
end
|
57
57
|
|
58
58
|
def test_path
|
@@ -100,6 +100,34 @@ class TC_File_Find < Test::Unit::TestCase
|
|
100
100
|
assert_nothing_raised{ @rule1.find }
|
101
101
|
end
|
102
102
|
|
103
|
+
def test_find
|
104
|
+
assert_kind_of(Array, @rule1.find)
|
105
|
+
assert_nil(@rule1.find{})
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_filetest_basic
|
109
|
+
assert_respond_to(@rule1, :filetest)
|
110
|
+
assert_respond_to(@rule1, :filetest=)
|
111
|
+
assert_nothing_raised{ @rule1.filetest }
|
112
|
+
assert_kind_of(Array, @rule1.filetest)
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_filetest_valid_options
|
116
|
+
assert_nothing_raised{ File::Find.new(:readable? => true) }
|
117
|
+
assert_nothing_raised{ File::Find.new(:writable? => true) }
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_filetest
|
121
|
+
rule = File::Find.new(:name => "*.doc", :writable? => true)
|
122
|
+
File.chmod(0644, @file4)
|
123
|
+
|
124
|
+
assert_equal([@file4], rule.find.map{ |f| File.basename(f) })
|
125
|
+
|
126
|
+
File.chmod(0444, @file4)
|
127
|
+
|
128
|
+
assert_equal([], rule.find)
|
129
|
+
end
|
130
|
+
|
103
131
|
def test_mtime
|
104
132
|
rule1 = File::Find.new(:name => "*.rb", :mtime => 0)
|
105
133
|
rule2 = File::Find.new(:name => "*.rb", :mtime => 1)
|
@@ -353,6 +381,7 @@ class TC_File_Find < Test::Unit::TestCase
|
|
353
381
|
def test_expected_errors
|
354
382
|
assert_raise(Errno::ENOENT){ File::Find.new(:path => '/bogus/dir').find }
|
355
383
|
assert_raise(ArgumentError){ File::Find.new(:bogus => 1) }
|
384
|
+
assert_raise(ArgumentError){ File::Find.new(:bogus? => true) }
|
356
385
|
end
|
357
386
|
|
358
387
|
def teardown
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: file-find
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Berger
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-12-
|
12
|
+
date: 2008-12-30 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|