pa 1.2.1 → 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/pa.rb +38 -4
- data/lib/pa/cmd.rb +5 -3
- data/lib/pa/directory.rb +116 -65
- data/lib/pa/path.rb +0 -1
- data/lib/pa/util.rb +5 -0
- data/lib/pa/version.rb +1 -1
- data/pa.gemspec +1 -4
- data/spec/pa/directory_spec.rb +115 -32
- data/spec/pa/util_spec.rb +13 -0
- data/spec/pa_spec.rb +0 -1
- metadata +3 -2
data/lib/pa.rb
CHANGED
@@ -207,13 +207,21 @@ class Pa
|
|
207
207
|
DELEGATE_METHODS = [ :dir, :build, :join ]
|
208
208
|
|
209
209
|
attr_reader :path2
|
210
|
-
attr_reader :absolute2, :dir2, :dir_strict2, :base2, :fname2, :name2, :short2, :ext2, :fext2
|
210
|
+
attr_reader :absolute2, :dir2, :dir_strict2, :base2, :fname2, :name2, :short2, :ext2, :fext2, :rel, :rea
|
211
|
+
attr_reader :options
|
211
212
|
|
213
|
+
# @param [Hash] o option
|
214
|
+
# @option o [String] rel relative path
|
215
|
+
# @option o [String] base_dir
|
212
216
|
# @param [String, #path] path
|
213
|
-
def initialize(path)
|
214
|
-
# convert ~ to ENV["HOME"]
|
217
|
+
def initialize(path, o={})
|
215
218
|
@path2 = Pa.get(path)
|
219
|
+
# convert ~ to ENV["HOME"]
|
216
220
|
@path2.sub!(/^~/, ENV["HOME"]) if @path2 # nil
|
221
|
+
@options = o
|
222
|
+
|
223
|
+
@rel = o[:rel] || ""
|
224
|
+
@base_dir = o[:base_dir]
|
217
225
|
|
218
226
|
initialize_variables
|
219
227
|
end
|
@@ -223,8 +231,34 @@ class Pa
|
|
223
231
|
end
|
224
232
|
include chainable
|
225
233
|
|
234
|
+
def rel
|
235
|
+
raise Error, "don't have a :rel option" unless rel?
|
236
|
+
|
237
|
+
@rel ||= options[:rel]
|
238
|
+
end
|
239
|
+
|
240
|
+
def base_dir
|
241
|
+
raise Error, "don't have a :base_dir option" unless base_dir?
|
242
|
+
|
243
|
+
@base_dir ||= options[:base_dir]
|
244
|
+
end
|
245
|
+
|
246
|
+
def rea
|
247
|
+
raise Error, "don't have a :base_dir option" unless base_dir?
|
248
|
+
|
249
|
+
@rea ||= File.join(options[:base_dir], path)
|
250
|
+
end
|
251
|
+
|
252
|
+
def rel?
|
253
|
+
options.has_key?(:rel)
|
254
|
+
end
|
255
|
+
|
256
|
+
def base_dir?
|
257
|
+
options.has_key?(:base_dir)
|
258
|
+
end
|
259
|
+
|
226
260
|
def absolute2
|
227
|
-
@absolute2 ||= File.absolute_path(path)
|
261
|
+
@absolute2 ||= File.absolute_path(options[:base_dir] ? rea : path)
|
228
262
|
end
|
229
263
|
|
230
264
|
# => ".", "..", "/", "c:"
|
data/lib/pa/cmd.rb
CHANGED
@@ -93,7 +93,7 @@ class Pa
|
|
93
93
|
# @overload touch(*paths, o={})
|
94
94
|
# @param [String] *paths
|
95
95
|
# @param [Hash] o option
|
96
|
-
# @option o [Fixnum,String] :mode
|
96
|
+
# @option o [Fixnum,String] :mode (0664)
|
97
97
|
# @option o [Boolean] :mkdir auto mkdir if path contained directory not exists.
|
98
98
|
# @option o [Boolean] :force
|
99
99
|
# @option o [Boolean] :verbose
|
@@ -119,7 +119,7 @@ class Pa
|
|
119
119
|
# @overload mkdir(*paths, o={})
|
120
120
|
# @param [String, Pa] *paths
|
121
121
|
# @param [Hash] o option
|
122
|
-
# @option o [Fixnum] :mode
|
122
|
+
# @option o [Fixnum] :mode (0775)
|
123
123
|
# @option o [Boolean] :force
|
124
124
|
# @option o [Boolean] :verbose
|
125
125
|
# @return [nil]
|
@@ -308,6 +308,8 @@ class Pa
|
|
308
308
|
# yield # use yield to do the actuactal cp work
|
309
309
|
# end
|
310
310
|
#
|
311
|
+
# default: preverse mode, not owner.
|
312
|
+
#
|
311
313
|
# @overload cp(src_s, dest, o)
|
312
314
|
# @param [Array<String>, String] src_s support globbing
|
313
315
|
# @param [String,Pa] dest
|
@@ -460,7 +462,7 @@ class Pa
|
|
460
462
|
end
|
461
463
|
|
462
464
|
def _mkdir(paths, o)
|
463
|
-
o[:mode] ||=
|
465
|
+
o[:mode] ||= 0775
|
464
466
|
paths.map!{|v|get(v)}
|
465
467
|
paths.each {|p|
|
466
468
|
puts "mkdir #{p}" if o[:verbose]
|
data/lib/pa/directory.rb
CHANGED
@@ -106,50 +106,60 @@ class Pa
|
|
106
106
|
# @prarm [Hash] o
|
107
107
|
# @option o [Boolean] :dot (true) include dot file
|
108
108
|
# @option o [Boolean] :backup (true) include backup file
|
109
|
-
# @option o [Boolean] :absolute (false) return absolute path
|
110
109
|
# @option o [Boolean] :error (false) yield(pa, err) instead of raise Errno::EPERM when Dir.open(dir)
|
110
|
+
# @option o [Boolean] :file (false) return path and not raise Errno:ENOTDIR if path is a file.
|
111
|
+
# @option o [String,Pa] :base_dir (nil) base directory.
|
111
112
|
# @return [Enumerator<String>]
|
112
|
-
# @overload each(path=".", o={})
|
113
|
+
# @overload each(path=".", o={}){|path, abs, fname, err, [rea]|}
|
113
114
|
# @yieldparam [String] path
|
115
|
+
# @yieldparam [String] abs absolute path
|
116
|
+
# @yieldparam [String] fname a basename
|
117
|
+
# @yieldparam [String] err error
|
118
|
+
# @yieldparam [String] rea real relative path with o[:base_dir]
|
114
119
|
# @return [nil]
|
115
120
|
def each2(*args, &blk)
|
116
121
|
return Pa.to_enum(:each2, *args) unless blk
|
117
122
|
|
118
|
-
(
|
123
|
+
(dir,), o = Util.extract_options(args)
|
124
|
+
dir = dir ? get(dir) : "."
|
119
125
|
o = {dot: true, backup: true}.merge(o)
|
120
126
|
|
121
|
-
|
122
|
-
raise Errno::ENOENT, "`#{
|
123
|
-
|
127
|
+
rea_dir = o[:base_dir] ? File.join(get(o[:base_dir]), dir) : dir
|
128
|
+
raise Errno::ENOENT, "`#{rea_dir}' doesn't exists." unless File.exists?(rea_dir)
|
129
|
+
|
130
|
+
if not File.directory?(rea_dir)
|
131
|
+
if o[:file]
|
132
|
+
rea_path = rea_dir
|
133
|
+
blk.call dir, File.absolute_path(rea_path), File.basename(rea_path), nil, rea_path
|
134
|
+
return
|
135
|
+
else
|
136
|
+
raise Errno::ENOTDIR, "`#{rea_dir}' is not a directoy."
|
137
|
+
end
|
138
|
+
end
|
124
139
|
|
125
140
|
begin
|
126
|
-
|
141
|
+
d = Dir.open(rea_dir)
|
127
142
|
rescue Errno::EPERM => err
|
128
143
|
end
|
129
144
|
raise err if err and !o[:error]
|
130
145
|
|
131
|
-
while (entry=
|
146
|
+
while (entry=d.read)
|
132
147
|
next if %w(. ..).include? entry
|
133
148
|
next if not o[:dot] and entry=~/^\./
|
134
149
|
next if not o[:backup] and entry=~/~$/
|
135
150
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
# => "foo" not "./foo"
|
140
|
-
path=="." ? entry : File.join(path, entry)
|
141
|
-
end
|
142
|
-
|
143
|
-
blk.call p, err
|
144
|
-
end
|
151
|
+
path = Util.join(dir, entry)
|
152
|
+
rea_path = Util.join(rea_dir, entry)
|
153
|
+
blk.call path, File.absolute_path(rea_path), File.basename(rea_path), err, rea_path
|
145
154
|
end
|
155
|
+
end
|
146
156
|
|
147
157
|
def each(*args, &blk)
|
148
158
|
return Pa.to_enum(:each, *args) unless blk
|
149
159
|
|
150
160
|
args, o = Util.extract_options(args)
|
151
|
-
each2(*args, o) { |path, err|
|
152
|
-
blk.call Pa(path), err
|
161
|
+
each2(*args, o) { |path, abs, fname, err, rea|
|
162
|
+
blk.call Pa(path), abs, fname, err, rea
|
153
163
|
}
|
154
164
|
end
|
155
165
|
|
@@ -160,27 +170,29 @@ class Pa
|
|
160
170
|
# * each2_r(){path, relative, err}
|
161
171
|
#
|
162
172
|
# @overload each2_r(path=".", o={})
|
173
|
+
# @option o [String, Pa] :base_dir (nil) base directory.
|
163
174
|
# @return [Enumerator<String>]
|
164
175
|
# @overload each2_r(path=".", o={})
|
165
176
|
# @yieldparam [String] path
|
166
|
-
# @yieldparam [String]
|
177
|
+
# @yieldparam [String] abs
|
178
|
+
# @yieldparam [String] rel/rea relative path
|
167
179
|
# @yieldparam [Errno::ENOENT,Errno::EPERM] err
|
168
180
|
# @return [nil]
|
169
181
|
def each2_r(*args, &blk)
|
170
182
|
return Pa.to_enum(:each2_r, *args) if not blk
|
171
183
|
|
172
|
-
(
|
173
|
-
|
184
|
+
(dir,), o = Util.extract_options(args)
|
185
|
+
dir ||= "."
|
174
186
|
|
175
|
-
_each2_r(
|
187
|
+
_each2_r(dir, "", o, &blk)
|
176
188
|
end
|
177
189
|
|
178
190
|
def each_r(*args, &blk)
|
179
191
|
return Pa.to_enum(:each_r, *args) if not blk
|
180
192
|
|
181
193
|
args, o = Util.extract_options(args)
|
182
|
-
each2_r *args, o do |path, err|
|
183
|
-
blk.call Pa(path), err
|
194
|
+
each2_r *args, o do |path, abs, rel, err, rea|
|
195
|
+
blk.call Pa(path), abs, rel, err, rea
|
184
196
|
end
|
185
197
|
end
|
186
198
|
|
@@ -192,81 +204,120 @@ class Pa
|
|
192
204
|
# @Example
|
193
205
|
# Pa.ls2(".") {|path, fname| Pa.directory?(path)} # list only directories
|
194
206
|
#
|
195
|
-
# @overload ls2(
|
207
|
+
# @overload ls2(*dirs, o={})
|
208
|
+
# @option o [Boolean] :absolute (false) return absolute path instead.
|
209
|
+
# @option o [Boolean] :include (false) return "<path>/foo"
|
196
210
|
# @return [Array<String>]
|
197
|
-
# @overload ls2(
|
211
|
+
# @overload ls2(*dirs, o={}){|path, abs, fname|}
|
198
212
|
# @yieldparam [String] path
|
213
|
+
# @yieldparam [String] abs
|
199
214
|
# @yieldparam [String] fname
|
200
215
|
# @return [Array<String>]
|
201
216
|
def ls2(*args, &blk)
|
202
|
-
|
203
|
-
|
217
|
+
dirs, o = Util.extract_options(args)
|
218
|
+
dirs << "." if dirs.empty?
|
204
219
|
blk ||= proc { true }
|
205
220
|
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
blk_ret = blk.call(path, base)
|
210
|
-
file = o[:absolute] ? path : base
|
211
|
-
m << file if blk_ret
|
212
|
-
}
|
213
|
-
end
|
214
|
-
|
215
|
-
# @overload ls(path=".", o={})
|
216
|
-
# @return [Array<Pa>]
|
217
|
-
# @overload ls(path=".", o={})
|
218
|
-
# @yieldparam [Pa] path
|
219
|
-
# @yieldparam [String] fname
|
220
|
-
# @return [Array<String>]
|
221
|
-
def ls(*args, &blk)
|
222
|
-
(path,), o = Util.extract_options(args)
|
223
|
-
path ||= "."
|
224
|
-
blk ||= proc { true }
|
221
|
+
dirs.each.with_object([]) { |dir, m|
|
222
|
+
each2(dir, o) { |path, abs, fname, err, rea|
|
225
223
|
|
226
|
-
|
227
|
-
|
224
|
+
view_path = if o[:absolute]
|
225
|
+
abs
|
226
|
+
elsif o[:include]
|
227
|
+
path
|
228
|
+
else
|
229
|
+
fname
|
230
|
+
end
|
228
231
|
|
229
|
-
|
230
|
-
|
231
|
-
m << Pa(file) if blk_ret
|
232
|
+
m << view_path if blk.call(path, abs, fname, err, rea)
|
233
|
+
}
|
232
234
|
}
|
233
235
|
end
|
234
236
|
|
235
237
|
# ls2 with recursive
|
236
238
|
# @see ls2
|
237
239
|
#
|
238
|
-
# @overload ls2_r(
|
240
|
+
# @overload ls2_r(*dirs, o={})
|
239
241
|
# @return [Array<String>]
|
240
|
-
# @overload ls2_r(
|
241
|
-
# @yieldparam [
|
242
|
+
# @overload ls2_r(*dirs, o={}){|pa, abs, rel, err, [rea]|
|
243
|
+
# @yieldparam [String] path
|
244
|
+
# @yieldparam [String] abs
|
242
245
|
# @yieldparam [String] rel
|
246
|
+
# @yieldparam [Exception] err
|
247
|
+
# @yieldparam [String] rea
|
243
248
|
# @return [Array<String>]
|
244
249
|
def ls2_r(*args, &blk)
|
245
|
-
|
246
|
-
|
247
|
-
|
250
|
+
dirs, o = Util.extract_options(args)
|
251
|
+
dirs << "." if dirs.empty?
|
252
|
+
blk ||= proc { true }
|
253
|
+
|
254
|
+
dirs.each.with_object([]) { |dir, m|
|
255
|
+
each2_r(dir, o) { |path, abs, rel, err, rea|
|
256
|
+
view_path = if o[:absolute]
|
257
|
+
abs
|
258
|
+
elsif o[:include]
|
259
|
+
path
|
260
|
+
else
|
261
|
+
rel
|
262
|
+
end
|
263
|
+
|
264
|
+
m << view_path if blk.call(path, abs, rel, err, rea)
|
265
|
+
}
|
248
266
|
}
|
249
267
|
end
|
250
268
|
|
269
|
+
# @overload ls(*paths, o={})
|
270
|
+
# @params [Array] paths (["."])
|
271
|
+
# @return [Array<Pa>]
|
272
|
+
# @overload ls(*paths, o={}){|pa, abs, fname, err, [rea]|}
|
273
|
+
# @yieldparam [Pa] pa
|
274
|
+
# @yieldparam [String] abs
|
275
|
+
# @yieldparam [String] fname
|
276
|
+
# @yieldparam [Exception] err
|
277
|
+
# @yieldparam [String] rea
|
278
|
+
# @return [Array<String>]
|
279
|
+
def ls(*args, &blk)
|
280
|
+
dirs, o = Util.extract_options(args)
|
281
|
+
blk ||= proc { true }
|
282
|
+
ret = []
|
283
|
+
|
284
|
+
ls2(*dirs, o) { |path, abs, fname, err, rea|
|
285
|
+
view_path = if o[:absolute]
|
286
|
+
abs
|
287
|
+
elsif o[:include]
|
288
|
+
path
|
289
|
+
else
|
290
|
+
fname
|
291
|
+
end
|
292
|
+
|
293
|
+
ret << Pa(view_path) if blk.call(Pa(path), abs, fname, err, rea)
|
294
|
+
}
|
295
|
+
|
296
|
+
ret
|
297
|
+
end
|
298
|
+
|
251
299
|
def ls_r(*args, &blk)
|
252
300
|
args, o = Util.extract_options(args)
|
253
301
|
ls2_r(*args, o, &blk)
|
254
302
|
end
|
255
303
|
|
256
304
|
private
|
305
|
+
|
306
|
+
# I'm rescurive.
|
257
307
|
# @param [String] path
|
258
308
|
def _each2_r(path, relative, o, &blk)
|
309
|
+
relative = relative == "" ? nil : relative
|
259
310
|
o.merge!(error: true)
|
260
311
|
|
261
|
-
Pa.each2(path, o) do |path2, err|
|
312
|
+
Pa.each2(path, o) do |path2, abs, fname, err, rea|
|
262
313
|
# fix for File.join with empty string
|
263
|
-
|
264
|
-
|
314
|
+
rel = File.join(*[relative, File.basename(path2)].compact)
|
315
|
+
rea = o[:base_dir] ? File.join(get(o[:base_dir]), rel) : rel
|
265
316
|
|
266
|
-
blk.call path2,
|
317
|
+
blk.call path2, abs, rel, err, rea
|
267
318
|
|
268
|
-
if File.directory?(
|
269
|
-
_each2_r(path2,
|
319
|
+
if File.directory?(abs)
|
320
|
+
_each2_r(path2, rel, o, &blk)
|
270
321
|
end
|
271
322
|
end
|
272
323
|
end
|
data/lib/pa/path.rb
CHANGED
data/lib/pa/util.rb
CHANGED
data/lib/pa/version.rb
CHANGED
data/pa.gemspec
CHANGED
data/spec/pa/directory_spec.rb
CHANGED
@@ -67,10 +67,19 @@ describe Pa do
|
|
67
67
|
FileUtils.touch %w[filea .filea filea~ dira/dirb/b]
|
68
68
|
end
|
69
69
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
70
|
+
it "(:base_dir => x) to build clean path" do
|
71
|
+
Pa.each2("dira").to_a.map{|v|v[0]}.should == %w[dira/dirb]
|
72
|
+
Pa.each2("dirb", :base_dir => "dira").to_a.map{|v|v[0]}.should == %w[dirb/b]
|
73
|
+
Pa.each2("dirb", :base_dir => Pa("dira")).to_a.map{|v|v[0]}.should == %w[dirb/b]
|
74
|
+
end
|
75
|
+
|
76
|
+
it "yields {|path, abs, fname, err, rea|}" do
|
77
|
+
Pa.each2("dira").to_a.sort[0].should == ["dira/dirb", File.join(Dir.pwd, "dira/dirb"), "dirb", nil, "dira/dirb"]
|
78
|
+
Pa.each2("dirb", :base_dir => "dira").to_a.sort[0].should == ["dirb/b", File.join(Dir.pwd, "dira/dirb/b"), "b", nil, "dira/dirb/b"]
|
79
|
+
end
|
80
|
+
|
81
|
+
it "list a directory" do
|
82
|
+
Pa.each2.to_a.map{|v|v[0]}.sort.should == %w[.filea dira filea filea~]
|
74
83
|
end
|
75
84
|
|
76
85
|
it "return a Enumerator when call without block" do
|
@@ -85,29 +94,29 @@ describe Pa do
|
|
85
94
|
lambda { Pa.each2("filea"){} }.should raise_error(Errno::ENOTDIR)
|
86
95
|
end
|
87
96
|
|
88
|
-
|
89
|
-
|
90
|
-
|
97
|
+
it "(:file => true) return path if path is a file." do
|
98
|
+
Pa.each2("filea", :file => true).to_a[0][0].should == "filea"
|
99
|
+
end
|
91
100
|
|
92
|
-
it ".
|
93
|
-
Pa.each2
|
101
|
+
it "(.) return 'foo' not '.foo'" do
|
102
|
+
Pa.each2.to_a.map{|v|v[0]}.sort.should == %w(.filea dira filea filea~)
|
94
103
|
end
|
95
104
|
|
96
|
-
|
97
|
-
|
98
|
-
|
105
|
+
it "with :dot => false -> list all files except dot file" do
|
106
|
+
Pa.each2(:dot => false).to_a.map{|v|v[0]}.sort.should == %w[dira filea filea~]
|
107
|
+
end
|
99
108
|
|
100
|
-
it "
|
101
|
-
|
102
|
-
Pa.each2(:absolute => true).with_object([]){|(pa),m|m<<pa}.sort.should == b
|
109
|
+
it "with :backup => false" do
|
110
|
+
Pa.each2(:backup => false).to_a.map{|v|v[0]}.sort.should == %w[.filea dira filea]
|
103
111
|
end
|
104
112
|
|
105
|
-
it "
|
113
|
+
it "returns Pa" do
|
106
114
|
Pa.each { |pa|
|
107
115
|
pa.should be_an_instance_of Pa
|
108
116
|
break
|
109
117
|
}
|
110
118
|
end
|
119
|
+
|
111
120
|
end
|
112
121
|
|
113
122
|
describe ".each" do
|
@@ -141,15 +150,30 @@ describe Pa do
|
|
141
150
|
FileUtils.touch %w[filea .filea filea~ dira/dirb/b]
|
142
151
|
end
|
143
152
|
|
144
|
-
|
153
|
+
it "=> Enumerator when call without any arguments" do
|
145
154
|
Pa.each2_r.should be_an_instance_of Enumerator
|
146
|
-
Pa.each2_r.with_object([]){|(pa,r),m|m<<r}.sort.should == %w[.filea dira dira/dirb dira/dirb/b filea filea~]
|
147
155
|
end
|
148
156
|
|
149
|
-
it "
|
150
|
-
|
157
|
+
it "list directory recursive" do
|
158
|
+
Pa.each2_r.map{|v,|v}.sort.should == %w[.filea dira dira/dirb dira/dirb/b filea filea~]
|
159
|
+
end
|
160
|
+
|
161
|
+
it "(:base_dir => x) to build clean path" do
|
162
|
+
Pa.each2_r("dira").to_a.map{|v|v[0]}.should == %w[dira/dirb dira/dirb/b]
|
163
|
+
Pa.each2_r(".", :base_dir => "dira").to_a.map{|v|v[0]}.should == %w[dirb dirb/b]
|
164
|
+
Pa.each2_r(".", :base_dir => Pa("dira")).to_a.map{|v|v[0]}.should == %w[dirb dirb/b]
|
151
165
|
end
|
166
|
+
end
|
152
167
|
|
168
|
+
describe ".each_r" do
|
169
|
+
# filea .filea filea~
|
170
|
+
# dira/
|
171
|
+
# dirb/
|
172
|
+
# b
|
173
|
+
before :each do
|
174
|
+
FileUtils.mkdir_p %w[dira/dirb]
|
175
|
+
FileUtils.touch %w[filea .filea filea~ dira/dirb/b]
|
176
|
+
end
|
153
177
|
|
154
178
|
it "#each_r returns Pa" do
|
155
179
|
Pa.each_r { |pa|
|
@@ -163,22 +187,77 @@ describe Pa do
|
|
163
187
|
# filea
|
164
188
|
# dira/
|
165
189
|
# fileb
|
190
|
+
# dirb/
|
191
|
+
# dirb1/
|
192
|
+
# fileb1
|
166
193
|
before :each do
|
167
|
-
FileUtils.mkdir_p %w[dira]
|
168
|
-
FileUtils.touch %w[filea dira/fileb]
|
194
|
+
FileUtils.mkdir_p %w[dira dirb/dirb1]
|
195
|
+
FileUtils.touch %w[filea dira/fileb dirb/dirb1/fileb1]
|
169
196
|
end
|
170
197
|
|
171
198
|
it "works" do
|
172
|
-
Pa.ls2.should == %w[filea dira]
|
173
|
-
Pa.ls2(
|
199
|
+
Pa.ls2.should == %w[filea dira dirb]
|
200
|
+
Pa.ls2("dira").should == %w[fileb]
|
174
201
|
end
|
175
202
|
|
176
|
-
it "
|
177
|
-
Pa.ls2(
|
203
|
+
it "list multi paths" do
|
204
|
+
Pa.ls2("dira", "dirb").should == %w[fileb dirb1]
|
178
205
|
end
|
179
206
|
|
180
|
-
|
181
|
-
|
207
|
+
it "(:absolute => true) returns absolute path" do
|
208
|
+
Pa.ls2("dira", :absolute => true).should == [File.join(Dir.pwd, "dira/fileb")]
|
209
|
+
end
|
210
|
+
|
211
|
+
it %~(:include => true) returns "<path>/foo"~ do
|
212
|
+
Pa.ls2("dira", :include => true).should == %w[dira/fileb]
|
213
|
+
end
|
214
|
+
|
215
|
+
it "(:base_dir => x)" do
|
216
|
+
Pa.ls2("dirb1", :base_dir => "dirb").should == %w[fileb1]
|
217
|
+
Pa.ls2("dirb1", :base_dir => "dirb", :include => true).should == %w[dirb1/fileb1]
|
218
|
+
end
|
219
|
+
|
220
|
+
it "call a block returns filtered result" do
|
221
|
+
Pa.ls2 {|p| File.directory?(p)}.should == %w[dira dirb]
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
describe ".ls2_r" do
|
226
|
+
# filea
|
227
|
+
# dira/
|
228
|
+
# fileb
|
229
|
+
# dirb/
|
230
|
+
# dirb1/
|
231
|
+
# fileb1
|
232
|
+
before :each do
|
233
|
+
FileUtils.mkdir_p %w[dira dirb/dirb1]
|
234
|
+
FileUtils.touch %w[filea dira/fileb dirb/dirb1/fileb1]
|
235
|
+
end
|
236
|
+
|
237
|
+
it "works" do
|
238
|
+
Pa.ls2_r.should == %w[filea dira dira/fileb dirb dirb/dirb1 dirb/dirb1/fileb1]
|
239
|
+
Pa.ls2_r("dirb").should == %w[dirb1 dirb1/fileb1]
|
240
|
+
end
|
241
|
+
|
242
|
+
it "list multi paths" do
|
243
|
+
Pa.ls2_r("dira", "dirb").should == %w[fileb dirb1 dirb1/fileb1]
|
244
|
+
end
|
245
|
+
|
246
|
+
it "(:absolute => true) returns absolute path" do
|
247
|
+
Pa.ls2_r("dirb", :absolute => true).should == %w[dirb/dirb1 dirb/dirb1/fileb1].map{|v|File.join(Dir.pwd, v)}
|
248
|
+
end
|
249
|
+
|
250
|
+
it %~(:include => true) returns "<path>/foo"~ do
|
251
|
+
Pa.ls2_r("dirb", :include => true).should == %w[dirb/dirb1 dirb/dirb1/fileb1]
|
252
|
+
end
|
253
|
+
|
254
|
+
it "(:base_dir => x)" do
|
255
|
+
Pa.ls2_r("dirb1", :base_dir => "dirb").should == %w[fileb1]
|
256
|
+
Pa.ls2_r("dirb1", :base_dir => "dirb", :include => true).should == %w[dirb1/fileb1]
|
257
|
+
end
|
258
|
+
|
259
|
+
it "call a block returns filtered result" do
|
260
|
+
Pa.ls2_r {|p, fn| File.directory?(p)}.should == %w[dira dirb dirb/dirb1]
|
182
261
|
end
|
183
262
|
end
|
184
263
|
|
@@ -192,16 +271,20 @@ describe Pa do
|
|
192
271
|
end
|
193
272
|
|
194
273
|
it "works" do
|
195
|
-
Pa.ls.should == %w[filea dira].map{|v|
|
196
|
-
Pa.ls(Dir.pwd).should == %w[filea dira].map{|v|
|
274
|
+
Pa.ls.should == %w[filea dira].map{|v|Pa(v)}
|
275
|
+
Pa.ls(Dir.pwd).should == %w[filea dira].map{|v|Pa(v)}
|
197
276
|
end
|
198
277
|
|
278
|
+
it "list multi paths" do
|
279
|
+
Pa.ls(".", "dira").should == %w[filea dira fileb].map{|v|Pa(v)}
|
280
|
+
end
|
281
|
+
|
199
282
|
it "with :absolute => true" do
|
200
|
-
Pa.ls(:absolute => true).should == %w[filea dira].map{|v|
|
283
|
+
Pa.ls(:absolute => true).should == %w[filea dira].map{|v|Pa(File.join(Dir.pwd, v))}
|
201
284
|
end
|
202
285
|
|
203
286
|
it "call a block" do
|
204
|
-
Pa.ls{|p, fn| p.directory? }.should == %w[dira].map{|v|
|
287
|
+
Pa.ls{|p, fn| p.directory? }.should == %w[dira].map{|v|Pa(v)}
|
205
288
|
end
|
206
289
|
end
|
207
290
|
|
data/spec/pa_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pa
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-21 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! 'a path library for Ruby
|
15
15
|
|
@@ -39,6 +39,7 @@ files:
|
|
39
39
|
- spec/pa/directory_spec.rb
|
40
40
|
- spec/pa/path_spec.rb
|
41
41
|
- spec/pa/state_spec.rb
|
42
|
+
- spec/pa/util_spec.rb
|
42
43
|
- spec/pa_spec.rb
|
43
44
|
- spec/spec_helper.rb
|
44
45
|
homepage: http://github.com/GutenYe/pa
|