pa 1.1.3 → 1.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +0 -2
- data/lib/pa.rb +4 -15
- data/lib/pa/cmd.rb +8 -10
- data/lib/pa/{dir.rb → directory.rb} +54 -38
- data/lib/pa/ext.rb +35 -0
- data/lib/pa/path.rb +77 -51
- data/lib/pa/state.rb +10 -5
- data/lib/pa/version.rb +1 -1
- data/spec/pa/cmd_spec.rb +4 -7
- data/spec/pa/{dir_spec.rb → directory_spec.rb} +30 -13
- data/spec/pa/path_spec.rb +177 -50
- data/spec/spec_helper.rb +1 -0
- metadata +6 -5
data/Gemfile.lock
CHANGED
data/lib/pa.rb
CHANGED
@@ -75,7 +75,7 @@ class Pa
|
|
75
75
|
# dir -> dir2
|
76
76
|
name2 = "#{name}2".to_sym
|
77
77
|
if public_methods.include?(name2)
|
78
|
-
ret = __send__(name2, *args)
|
78
|
+
ret = __send__(name2, *args, &blk)
|
79
79
|
return case ret
|
80
80
|
when Array
|
81
81
|
ret.map{|v| Pa(v)}
|
@@ -130,17 +130,7 @@ class Pa
|
|
130
130
|
|
131
131
|
# missing method goes to Pa.class-method
|
132
132
|
def method_missing(name, *args, &blk)
|
133
|
-
|
134
|
-
|
135
|
-
case ret
|
136
|
-
# e.g. readlink ..
|
137
|
-
when String
|
138
|
-
Pa(ret)
|
139
|
-
# e.g. directory?
|
140
|
-
else
|
141
|
-
ret
|
142
|
-
end
|
143
|
-
|
133
|
+
self.class.__send__(name, path, *args, &blk)
|
144
134
|
end
|
145
135
|
|
146
136
|
def <=> other
|
@@ -158,16 +148,15 @@ end
|
|
158
148
|
|
159
149
|
require "pa/path"
|
160
150
|
require "pa/cmd"
|
161
|
-
require "pa/
|
151
|
+
require "pa/directory"
|
162
152
|
require "pa/state"
|
163
153
|
class Pa
|
164
154
|
include Path
|
165
|
-
include
|
155
|
+
include Directory
|
166
156
|
include State
|
167
157
|
include Cmd
|
168
158
|
end
|
169
159
|
|
170
|
-
|
171
160
|
module Kernel
|
172
161
|
private
|
173
162
|
# a very convient function.
|
data/lib/pa/cmd.rb
CHANGED
@@ -59,12 +59,11 @@ class Pa
|
|
59
59
|
File.readlink(get(path))
|
60
60
|
end
|
61
61
|
|
62
|
-
|
63
62
|
# change directory
|
64
63
|
#
|
65
64
|
# @param [String,Pa] path
|
66
65
|
def cd(path=ENV["HOME"], &blk)
|
67
|
-
|
66
|
+
Dir.chdir(get(path), &blk)
|
68
67
|
end
|
69
68
|
|
70
69
|
# chroot
|
@@ -73,7 +72,7 @@ class Pa
|
|
73
72
|
# @param [String] path
|
74
73
|
# @return [nil]
|
75
74
|
def chroot(path)
|
76
|
-
|
75
|
+
Dir.chroot(get(path))
|
77
76
|
end
|
78
77
|
|
79
78
|
# touch a blank file
|
@@ -135,12 +134,12 @@ class Pa
|
|
135
134
|
def mktmpdir(o={}, &blk)
|
136
135
|
p = _mktmpname(o)
|
137
136
|
File.mkdir(p)
|
138
|
-
begin blk.call(p) ensure
|
137
|
+
begin blk.call(p) ensure Dir.delete(p) end if blk
|
139
138
|
p
|
140
139
|
end # def mktmpdir
|
141
140
|
|
142
141
|
def home(user=nil)
|
143
|
-
|
142
|
+
Dir.home
|
144
143
|
end
|
145
144
|
|
146
145
|
# make temp file
|
@@ -301,14 +300,13 @@ class Pa
|
|
301
300
|
#
|
302
301
|
# @example
|
303
302
|
#
|
304
|
-
# Pa.
|
305
|
-
# Pa('/home/guten.jpg').rename {|pa| pa.name+'_1'+pa.fext} # => <#Pa('/home/guten_1.jpg')>
|
303
|
+
# Pa.rename2('/home/guten.jpg') {|pa| pa.name+'_1'+pa.fext} # => '/home/guten_1.jpg'
|
306
304
|
#
|
307
305
|
# @param [String,Pa] src
|
308
306
|
# @yieldparam [Pa] pa
|
309
307
|
# @yieldreturn [String] fname
|
310
308
|
# @return [String,Pa] # Pa.rename return String. Pa#rename return Pa.
|
311
|
-
def
|
309
|
+
def rename2(src, &blk)
|
312
310
|
src = Pa(src)
|
313
311
|
fname = blk.call(src)
|
314
312
|
src.dir.join(fname).path
|
@@ -429,7 +427,7 @@ class Pa
|
|
429
427
|
end
|
430
428
|
|
431
429
|
stack.reverse.each do |path|
|
432
|
-
|
430
|
+
Dir.mkdir(path)
|
433
431
|
File.chmod(o[:mode], path)
|
434
432
|
end
|
435
433
|
}
|
@@ -463,7 +461,7 @@ class Pa
|
|
463
461
|
pa.each {|pa1|
|
464
462
|
File.directory?(pa1.p) ? _rmdir(pa1, o) : File.delete(pa1.p)
|
465
463
|
}
|
466
|
-
File.directory?(pa.p) ?
|
464
|
+
File.directory?(pa.p) ? Dir.rmdir(pa.p) : File.delete(pa.p)
|
467
465
|
end
|
468
466
|
|
469
467
|
# I'm recursive
|
@@ -20,7 +20,7 @@
|
|
20
20
|
|
21
21
|
=end
|
22
22
|
class Pa
|
23
|
-
module
|
23
|
+
module Directory
|
24
24
|
extend Util::Concern
|
25
25
|
|
26
26
|
module ClassMethods
|
@@ -40,32 +40,36 @@ class Pa
|
|
40
40
|
def glob2(*args, &blk)
|
41
41
|
paths, o = Util.extract_options(args)
|
42
42
|
paths.map!{|v|get(v)}
|
43
|
+
blk ||= proc { |path| path }
|
43
44
|
|
44
45
|
flag = 0
|
45
46
|
o.each do |option, value|
|
46
|
-
next if option==:use_pa
|
47
47
|
flag |= File.const_get("FNM_#{option.upcase}") if value
|
48
48
|
end
|
49
49
|
|
50
|
-
|
50
|
+
files = Dir.glob(paths, flag)
|
51
51
|
|
52
52
|
# delete . .. for '.*'
|
53
|
-
%w(. ..).each {|v|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
ret.
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
ret
|
62
|
-
end
|
53
|
+
%w(. ..).each {|v| files.delete(v)}
|
54
|
+
|
55
|
+
ret = []
|
56
|
+
files.each { |path|
|
57
|
+
ret << blk.call(path)
|
58
|
+
}
|
59
|
+
|
60
|
+
ret
|
63
61
|
end
|
64
62
|
|
65
63
|
def glob(*args, &blk)
|
66
64
|
args, o = Util.extract_options(args)
|
67
|
-
|
68
|
-
|
65
|
+
ret = []
|
66
|
+
blk ||= proc { |path| path }
|
67
|
+
|
68
|
+
glob2 *args, o do |path|
|
69
|
+
ret << blk.call(Pa(path))
|
70
|
+
end
|
71
|
+
|
72
|
+
ret
|
69
73
|
end
|
70
74
|
|
71
75
|
# is directory empty?
|
@@ -73,7 +77,7 @@ class Pa
|
|
73
77
|
# @param [String] path
|
74
78
|
# @return [Boolean]
|
75
79
|
def empty?(path)
|
76
|
-
|
80
|
+
Dir.entries(get(path)).empty?
|
77
81
|
end
|
78
82
|
|
79
83
|
# traverse directory
|
@@ -108,7 +112,7 @@ class Pa
|
|
108
112
|
raise Errno::ENOTDIR, "`#{path}' not a directoy." unless File.directory?(path)
|
109
113
|
|
110
114
|
begin
|
111
|
-
dir =
|
115
|
+
dir = Dir.open(path)
|
112
116
|
rescue Errno::EPERM => err
|
113
117
|
end
|
114
118
|
raise err if err and !o[:error]
|
@@ -120,19 +124,15 @@ class Pa
|
|
120
124
|
|
121
125
|
# => "foo" not "./foo"
|
122
126
|
pa = path=="." ? entry : File.join(path, entry)
|
123
|
-
|
124
|
-
if o[:error]
|
125
|
-
blk.call pa, err
|
126
|
-
else
|
127
|
-
blk.call pa
|
127
|
+
blk.call pa, err
|
128
128
|
end
|
129
129
|
end
|
130
|
-
end
|
131
130
|
|
132
131
|
def each(*args, &blk)
|
133
132
|
args, o = Util.extract_options(args)
|
134
|
-
o
|
135
|
-
|
133
|
+
each2 *args, o do |path, err|
|
134
|
+
blk.call Pa(path), err
|
135
|
+
end
|
136
136
|
end
|
137
137
|
|
138
138
|
# each with recursive
|
@@ -158,9 +158,12 @@ class Pa
|
|
158
158
|
end
|
159
159
|
|
160
160
|
def each_r(*args, &blk)
|
161
|
+
return Pa.to_enum(:each_r, *args) if not blk
|
162
|
+
|
161
163
|
args, o = Util.extract_options(args)
|
162
|
-
o
|
163
|
-
|
164
|
+
each2_r *args, o do |path, err|
|
165
|
+
blk.call Pa(path), err
|
166
|
+
end
|
164
167
|
end
|
165
168
|
|
166
169
|
# list directory contents
|
@@ -178,18 +181,31 @@ class Pa
|
|
178
181
|
# @yieldparam [String] fname
|
179
182
|
# @return [Array<String>]
|
180
183
|
def ls2(*args, &blk)
|
181
|
-
blk ||= proc {true}
|
182
|
-
each2(*args).with_object([]) { |path,m|
|
184
|
+
blk ||= proc { true }
|
185
|
+
each2(*args).with_object([]) { |(path),m|
|
183
186
|
base = File.basename(path)
|
184
187
|
ret = blk.call(path, base)
|
185
188
|
m << base if ret
|
186
189
|
}
|
187
190
|
end
|
188
191
|
|
192
|
+
# @overload ls(path=".", o={})
|
193
|
+
# @return [Array<Pa>]
|
194
|
+
# @overload ls(path=".", o={})
|
195
|
+
# @yieldparam [Pa] path
|
196
|
+
# @yieldparam [String] fname
|
197
|
+
# @return [Array<String>]
|
189
198
|
def ls(*args, &blk)
|
190
199
|
args, o = Util.extract_options(args)
|
191
|
-
|
192
|
-
|
200
|
+
blk ||= proc { true }
|
201
|
+
ret = []
|
202
|
+
|
203
|
+
ls2 *args do |path, fname|
|
204
|
+
rst = blk.call(Pa(path), fname)
|
205
|
+
ret << fname if rst
|
206
|
+
end
|
207
|
+
|
208
|
+
ret
|
193
209
|
end
|
194
210
|
|
195
211
|
# ls2 with recursive
|
@@ -217,16 +233,16 @@ class Pa
|
|
217
233
|
# @param [String] path
|
218
234
|
def _each2_r(path, relative, o, &blk)
|
219
235
|
o.merge!(error: true)
|
220
|
-
|
236
|
+
|
237
|
+
Pa.each2(path, o) do |path2, err|
|
221
238
|
# fix for File.join with empty string
|
222
|
-
joins=[ relative=="" ? nil : relative, File.basename(
|
223
|
-
|
239
|
+
joins=[ relative=="" ? nil : relative, File.basename(path2)].compact
|
240
|
+
relative2 = File.join(*joins)
|
224
241
|
|
225
|
-
|
226
|
-
blk.call path1, relative1, err
|
242
|
+
blk.call path2, relative2, err
|
227
243
|
|
228
|
-
if File.directory?(
|
229
|
-
_each2_r(
|
244
|
+
if File.directory?(path2)
|
245
|
+
_each2_r(path2, relative2, o, &blk)
|
230
246
|
end
|
231
247
|
end
|
232
248
|
end
|
data/lib/pa/ext.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# add Pa support in Marshal
|
2
|
+
module Marshal
|
3
|
+
class <<self
|
4
|
+
|
5
|
+
alias :original_load :load
|
6
|
+
|
7
|
+
# add support with Pa
|
8
|
+
#
|
9
|
+
# Marshal.load(Pa(path))
|
10
|
+
#
|
11
|
+
# @param [IO,String,Pa] obj
|
12
|
+
# @return [String]
|
13
|
+
def load(obj) original_load Pa===obj ? File.read(obj.p) : obj end
|
14
|
+
|
15
|
+
alias :original_dump :dump
|
16
|
+
|
17
|
+
# add support with Pa
|
18
|
+
#
|
19
|
+
# Marshal.dump(obj, Pa(path))
|
20
|
+
# dump(con, [obj], limit=-1)
|
21
|
+
#
|
22
|
+
# @param [String,Pa] obj
|
23
|
+
# @return [String]
|
24
|
+
def dump(obj, *args)
|
25
|
+
case args[0]
|
26
|
+
when String, Pa
|
27
|
+
path = String===args[0] ? args[0] : args[0].p
|
28
|
+
open(path, "wb"){|f| f.write(original_dump(con))}
|
29
|
+
else
|
30
|
+
original_dump con, *args
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
data/lib/pa/path.rb
CHANGED
@@ -15,11 +15,15 @@ class Pa
|
|
15
15
|
NAME_EXT_PAT = /^(.+?)(?:\.([^.]+))?$/
|
16
16
|
module Path
|
17
17
|
extend Util::Concern
|
18
|
+
|
18
19
|
module ClassMethods
|
19
20
|
# get path of an object.
|
20
21
|
#
|
21
22
|
# return obj#path if object has a 'path' instance method
|
22
23
|
#
|
24
|
+
# nil -> nil
|
25
|
+
#
|
26
|
+
#
|
23
27
|
# @param [String,#path] obj
|
24
28
|
# @return [String,nil] path
|
25
29
|
def get(obj)
|
@@ -27,8 +31,10 @@ class Pa
|
|
27
31
|
obj.path
|
28
32
|
elsif String === obj
|
29
33
|
obj
|
34
|
+
elsif obj.nil?
|
35
|
+
nil
|
30
36
|
else
|
31
|
-
raise
|
37
|
+
raise ArgumentError, "Pa.get() not support type -- #{obj.inspect}(#{obj.class})"
|
32
38
|
end
|
33
39
|
end
|
34
40
|
|
@@ -38,6 +44,32 @@ class Pa
|
|
38
44
|
Dir.getwd
|
39
45
|
end
|
40
46
|
|
47
|
+
# is path an absolute path ?
|
48
|
+
#
|
49
|
+
# @param [String,Pa] path
|
50
|
+
# @return [Boolean]
|
51
|
+
def absolute?(path)
|
52
|
+
path=get(path)
|
53
|
+
File.absolute_path(path) == path
|
54
|
+
end
|
55
|
+
|
56
|
+
# is path a dangling symlink?
|
57
|
+
#
|
58
|
+
# a dangling symlink is a dead symlink.
|
59
|
+
#
|
60
|
+
# @param [String,Pa] path
|
61
|
+
# @return [Boolean]
|
62
|
+
def dangling? path
|
63
|
+
path=get(path)
|
64
|
+
if File.symlink?(path)
|
65
|
+
src = File.readlink(path)
|
66
|
+
not File.exists?(src)
|
67
|
+
else
|
68
|
+
nil
|
69
|
+
end
|
70
|
+
end # def dsymlink?
|
71
|
+
|
72
|
+
|
41
73
|
def dir2(path)
|
42
74
|
File.dirname(path)
|
43
75
|
end
|
@@ -76,40 +108,53 @@ class Pa
|
|
76
108
|
ext
|
77
109
|
end
|
78
110
|
|
79
|
-
|
80
111
|
# alias from File.absolute_path
|
81
112
|
# @param [String,Pa] path
|
82
113
|
# @return [String]
|
83
|
-
def absolute2(path)
|
114
|
+
def absolute2(path)
|
115
|
+
File.absolute_path get(path)
|
116
|
+
end
|
84
117
|
|
85
118
|
# alias from File.expand_path
|
86
119
|
# @param [String,Pa] path
|
87
120
|
# @return [String]
|
88
|
-
def expand2(path)
|
121
|
+
def expand2(path)
|
122
|
+
File.expand_path get(path)
|
123
|
+
end
|
89
124
|
|
90
125
|
# shorten2 a path,
|
91
126
|
# convert /home/user/file to ~/file
|
92
127
|
#
|
93
128
|
# @param [String,Pa] path
|
94
129
|
# @return [String]
|
95
|
-
def shorten2(path)
|
96
|
-
get(path).sub
|
130
|
+
def shorten2(path)
|
131
|
+
get(path).sub /^#{Regexp.escape(ENV["HOME"])}/, "~"
|
97
132
|
end
|
98
133
|
|
134
|
+
# real path
|
135
|
+
def real2(path)
|
136
|
+
File.realpath get(path)
|
137
|
+
end
|
99
138
|
|
100
|
-
|
101
|
-
#
|
102
|
-
#
|
139
|
+
# get parent path
|
140
|
+
#
|
103
141
|
# @param [String,Pa] path
|
104
|
-
# @
|
105
|
-
|
142
|
+
# @param [Fixnum] n up level
|
143
|
+
# @return [String]
|
144
|
+
def parent2(path, n=1)
|
145
|
+
path = get(path)
|
146
|
+
n.times do
|
147
|
+
path = File.dirname(path)
|
148
|
+
end
|
149
|
+
path
|
150
|
+
end
|
106
151
|
|
107
152
|
# split path
|
108
153
|
#
|
109
154
|
# @example
|
110
155
|
# path="/home/a/file"
|
111
|
-
#
|
112
|
-
#
|
156
|
+
# split2(path) #=> "/home/a", "file"
|
157
|
+
# split2(path, :all => true) #=> "/", "home", "a", "file"
|
113
158
|
#
|
114
159
|
# @param [String,Pa] name
|
115
160
|
# @param [Hash] o option
|
@@ -117,7 +162,7 @@ class Pa
|
|
117
162
|
# @return [Array<String>]
|
118
163
|
def split2(name, o={})
|
119
164
|
dir, fname = File.split(get(name))
|
120
|
-
ret = Util.wrap_array(basename(fname
|
165
|
+
ret = Util.wrap_array(File.basename(fname))
|
121
166
|
|
122
167
|
if o[:all]
|
123
168
|
loop do
|
@@ -131,6 +176,7 @@ class Pa
|
|
131
176
|
ret
|
132
177
|
end
|
133
178
|
|
179
|
+
# special case
|
134
180
|
def split(*args)
|
135
181
|
dir, *names = split2(*args)
|
136
182
|
[ Pa(dir), *names]
|
@@ -151,38 +197,6 @@ class Pa
|
|
151
197
|
|
152
198
|
File.join(*paths)
|
153
199
|
end
|
154
|
-
|
155
|
-
# get parent path
|
156
|
-
#
|
157
|
-
# @param [String,Pa] path
|
158
|
-
# @param [Fixnum] n up level
|
159
|
-
# @return [String]
|
160
|
-
def parent2(path, n=1)
|
161
|
-
path = get(path)
|
162
|
-
n.times do
|
163
|
-
path = File.dirname(path)
|
164
|
-
end
|
165
|
-
path
|
166
|
-
end
|
167
|
-
|
168
|
-
# is path a dangling symlink?
|
169
|
-
#
|
170
|
-
# a dangling symlink is a dead symlink.
|
171
|
-
#
|
172
|
-
# @param [String,Pa] path
|
173
|
-
# @return [Boolean]
|
174
|
-
def dangling? path
|
175
|
-
path=get(path)
|
176
|
-
if File.symlink?(path)
|
177
|
-
src = File.readlink(path)
|
178
|
-
not File.exists?(src)
|
179
|
-
else
|
180
|
-
nil
|
181
|
-
end
|
182
|
-
end # def dsymlink?
|
183
|
-
|
184
|
-
# real path
|
185
|
-
def real2(path) File.realpath(get(path)) end
|
186
200
|
end
|
187
201
|
|
188
202
|
module InstanceMethods
|
@@ -215,11 +229,13 @@ class Pa
|
|
215
229
|
alias fe2 fext2
|
216
230
|
|
217
231
|
# fix name,2 => String
|
218
|
-
alias
|
232
|
+
alias base base2
|
219
233
|
alias fname fname2
|
234
|
+
alias name name2
|
220
235
|
alias ext ext2
|
221
236
|
alias fext fext2
|
222
237
|
|
238
|
+
alias b base
|
223
239
|
alias fn fname
|
224
240
|
alias n name
|
225
241
|
alias e ext
|
@@ -242,13 +258,23 @@ class Pa
|
|
242
258
|
end
|
243
259
|
|
244
260
|
# @return [String]
|
245
|
-
def sub2(*args
|
246
|
-
path.sub(*args
|
261
|
+
def sub2(*args, &blk)
|
262
|
+
path.sub(*args, &blk)
|
247
263
|
end
|
248
264
|
|
249
265
|
# @return [String]
|
250
|
-
def gsub2(*args
|
251
|
-
path.gsub(*args
|
266
|
+
def gsub2(*args, &blk)
|
267
|
+
path.gsub(*args, &blk)
|
268
|
+
end
|
269
|
+
|
270
|
+
# @return [Pa]
|
271
|
+
def sub(*args, &blk)
|
272
|
+
Pa(sub2(*args, &blk))
|
273
|
+
end
|
274
|
+
|
275
|
+
# @return [Pa]
|
276
|
+
def gsub(*args, &blk)
|
277
|
+
Pa(gsub2(*args, &blk))
|
252
278
|
end
|
253
279
|
|
254
280
|
# @return [Pa]
|
data/lib/pa/state.rb
CHANGED
@@ -3,11 +3,16 @@ class Pa
|
|
3
3
|
extend Util::Concern
|
4
4
|
module ClassMethods
|
5
5
|
# goes to File
|
6
|
-
[ :exists?, :atime, :ctime, :mtime, :stat, :lstat, :size, :zero?, :executable?, :executable_real?, :world_executable?, :readable?, :readable_real?, :world_readalbe?, :writeable?, :writeable_real?, :world_writeable?, :directory?, :file?, :blockdev?, :chardev?, :piple?, :socket?, :symlink?, :
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
FILE_DELEGATED_METHODS = [ :exists?, :atime, :ctime, :mtime, :stat, :lstat, :size, :zero?, :executable?, :executable_real?, :world_executable?, :readable?, :readable_real?, :world_readalbe?, :writeable?, :writeable_real?, :world_writeable?, :directory?, :file?, :blockdev?, :chardev?, :piple?, :socket?, :symlink?, :owned?, :grpowned?, :setgid?, :setuid?, :stricky?, :identical? ]
|
7
|
+
|
8
|
+
# delegated from File
|
9
|
+
FILE_DELEGATED_METHODS.each { |name|
|
10
|
+
module_eval <<-METHOD, __FILE__, __LINE__
|
11
|
+
def #{name}(*args)
|
12
|
+
File.#{name}(*args)
|
13
|
+
end
|
14
|
+
METHOD
|
15
|
+
}
|
11
16
|
|
12
17
|
# @see File.chmod
|
13
18
|
def chmod(mode, *paths)
|
data/lib/pa/version.rb
CHANGED
data/spec/pa/cmd_spec.rb
CHANGED
@@ -134,7 +134,7 @@ describe Pa do
|
|
134
134
|
|
135
135
|
it "_copy directory" do
|
136
136
|
Pa._copy 'dir', 'dirc'
|
137
|
-
Dir.entries('dirc').should == Dir.entries('dir')
|
137
|
+
Dir.entries('dirc').sort.should == Dir.entries('dir').sort
|
138
138
|
end
|
139
139
|
|
140
140
|
context "with :symlink" do
|
@@ -269,11 +269,10 @@ describe Pa do
|
|
269
269
|
end
|
270
270
|
end
|
271
271
|
|
272
|
-
describe ".
|
272
|
+
describe ".rename2" do
|
273
273
|
it "works" do
|
274
|
-
Pa.
|
275
|
-
|
276
|
-
}.should == '/home/guten_1.jpg'
|
274
|
+
a = Pa.rename2('/home/guten.jpg') {|pa| pa.name+'_1'+pa.fext }
|
275
|
+
a.should == '/home/guten_1.jpg'
|
277
276
|
end
|
278
277
|
end
|
279
278
|
|
@@ -284,6 +283,4 @@ describe Pa do
|
|
284
283
|
}.should == Pa('/home/guten_1.jpg')
|
285
284
|
end
|
286
285
|
end
|
287
|
-
|
288
|
-
|
289
286
|
end
|
@@ -29,17 +29,17 @@ describe Pa do
|
|
29
29
|
FileUtils.rm @files
|
30
30
|
end
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
36
|
-
end
|
32
|
+
it "returns 1 items" do
|
33
|
+
Pa.glob2("*").should have(1).items
|
34
|
+
end
|
37
35
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
36
|
+
it "returns 2 items with :dotmatch" do
|
37
|
+
Pa.glob2("*", dotmatch: true).should have(2).items
|
38
|
+
end
|
39
|
+
|
40
|
+
it "#glob returns Pa instead" do
|
41
|
+
Pa.glob("*")[0].should be_an_instance_of Pa
|
42
|
+
end
|
43
43
|
end
|
44
44
|
|
45
45
|
describe "#each2" do
|
@@ -77,13 +77,19 @@ describe Pa do
|
|
77
77
|
end
|
78
78
|
|
79
79
|
it "each2(.) return 'foo' not '.foo'" do
|
80
|
-
Pa.each2.with_object([]){|pa,m| m<<pa}.sort.should == %w(.fa dira fa fa~)
|
80
|
+
Pa.each2.with_object([]){|(pa),m| m<<pa}.sort.should == %w(.fa dira fa fa~)
|
81
81
|
end
|
82
82
|
|
83
83
|
it "each2(nodot: true) -> list all files except dot file" do
|
84
|
-
Pa.each2(nodot: true).with_object([]){|pa,m|m<<pa}.sort.should == %w(dira fa fa~)
|
84
|
+
Pa.each2(nodot: true).with_object([]){|(pa),m|m<<pa}.sort.should == %w(dira fa fa~)
|
85
85
|
end
|
86
86
|
|
87
|
+
it "each returns Pa" do
|
88
|
+
Pa.each { |pa|
|
89
|
+
pa.should be_an_instance_of Pa
|
90
|
+
break
|
91
|
+
}
|
92
|
+
end
|
87
93
|
end
|
88
94
|
|
89
95
|
describe "#each2_r" do
|
@@ -106,8 +112,14 @@ describe Pa do
|
|
106
112
|
Pa.each2_r.should be_an_instance_of Enumerator
|
107
113
|
Pa.each2_r.with_object([]){|(pa,r),m|m<<r}.sort.should == %w(.fa dira dira/dirb dira/dirb/b fa fa~)
|
108
114
|
end
|
109
|
-
end
|
110
115
|
|
116
|
+
it "#each_r returns Pa" do
|
117
|
+
Pa.each_r { |pa|
|
118
|
+
pa.should be_an_instance_of Pa
|
119
|
+
break
|
120
|
+
}
|
121
|
+
end
|
122
|
+
end
|
111
123
|
|
112
124
|
describe "#ls2" do
|
113
125
|
# filea
|
@@ -131,5 +143,10 @@ describe Pa do
|
|
131
143
|
it "call a block" do
|
132
144
|
Pa.ls2 { |pa, fname| File.directory?(pa) }.should == ["dira"]
|
133
145
|
end
|
146
|
+
|
147
|
+
it "#ls returns string" do
|
148
|
+
Pa.ls[0].should be_an_instance_of String
|
149
|
+
end
|
134
150
|
end
|
135
151
|
end
|
152
|
+
|
data/spec/pa/path_spec.rb
CHANGED
@@ -1,120 +1,247 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class T_FakePath
|
4
|
+
def path
|
5
|
+
"hello"
|
6
|
+
end
|
7
|
+
end
|
2
8
|
|
3
9
|
describe Pa do
|
4
|
-
context
|
5
|
-
it
|
6
|
-
Pa.dir(
|
10
|
+
context 'dir and dir2' do
|
11
|
+
it '#dir => Pa' do
|
12
|
+
Pa.dir('/home/guten').should be_an_instance_of Pa
|
7
13
|
end
|
8
14
|
|
9
|
-
it
|
10
|
-
Pa.dir2(
|
15
|
+
it '#dir2 => Pa' do
|
16
|
+
Pa.dir2('/home/guten').should be_an_instance_of String
|
11
17
|
end
|
12
|
-
|
13
18
|
end
|
14
19
|
|
15
|
-
describe
|
16
|
-
it
|
17
|
-
|
18
|
-
Pa.shorten2("/home/foo/file").should == "~/file"
|
20
|
+
describe 'NAME_EXT_PAT' do
|
21
|
+
it 'matchs `foo.bar`' do
|
22
|
+
'foo.bar'.match(Pa::NAME_EXT_PAT).captures.should == %w(foo bar)
|
19
23
|
end
|
20
24
|
|
21
|
-
it
|
22
|
-
|
23
|
-
Pa.shorten2("/home/bar/file").should == "/home/bar/file"
|
25
|
+
it 'matchs `foo`' do
|
26
|
+
'foo'.match(Pa::NAME_EXT_PAT).captures.should == ['foo', nil]
|
24
27
|
end
|
25
28
|
end
|
26
29
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
30
|
+
describe '.get' do
|
31
|
+
it 'get path from a path object' do
|
32
|
+
Pa.get(T_FakePath.new).should == 'hello'
|
33
|
+
end
|
31
34
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
+
it 'get path from a string' do
|
36
|
+
Pa.get('foo').should == 'foo'
|
37
|
+
end
|
35
38
|
|
39
|
+
it 'get nil from nil' do
|
40
|
+
Pa.get(nil).should == nil
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'otherwise raise ArgumentError' do
|
44
|
+
lambda { Pa.get([]) }.should raise_error(ArgumentError)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '.absolute?' do
|
49
|
+
it 'is true if path is a absolute path' do
|
50
|
+
Pa.absolute?('/').should == true
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'is false if path is a relative path' do
|
54
|
+
Pa.absolute?('.').should == false
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe '.dangling?' do
|
59
|
+
it 'works' do
|
60
|
+
olddir=Dir.pwd
|
61
|
+
Dir.chdir("#{$specdir}/data/tmp")
|
62
|
+
|
63
|
+
begin
|
64
|
+
File.open('fa', 'w'){|f| f.puts "guten" }
|
65
|
+
File.symlink('fa', 'symlink')
|
66
|
+
File.symlink('fb', 'dangling')
|
67
|
+
|
68
|
+
Pa.dangling?('symlink').should be_false
|
69
|
+
Pa.dangling?('dangling').should be_true
|
70
|
+
ensure
|
71
|
+
FileUtils.rm_r Dir.glob("*")
|
72
|
+
Dir.chdir(olddir)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe '.pwd2' do
|
78
|
+
olddir = Dir.getwd
|
79
|
+
Dir.chdir('/tmp')
|
80
|
+
begin
|
81
|
+
Pa.pwd2.should == '/tmp'
|
82
|
+
ensure
|
83
|
+
Dir.chdir(olddir)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe '.dir2' do
|
88
|
+
it "get a path's directory name" do
|
89
|
+
Pa.dir2('/home/guten').should == '/home'
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe '.base2' do
|
94
|
+
it 'get name, ext with :ext => true' do
|
95
|
+
Pa.base2('/home/foo.bar', ext: true).should == ['foo', 'bar']
|
96
|
+
end
|
36
97
|
end
|
37
98
|
|
38
|
-
|
39
|
-
|
40
|
-
|
99
|
+
describe '.ext2' do
|
100
|
+
it "get a path's extension" do
|
101
|
+
Pa.ext2('/home/a.txt').should == 'txt'
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'return nil when don extension' do
|
105
|
+
Pa.ext2('/home/a').should == nil
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'with complex' do
|
109
|
+
Pa.ext2('/home/a.b.c.txt').should == 'txt'
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe '.absolute2' do
|
114
|
+
it 'returns absolute_path' do
|
115
|
+
Pa.absolute2('.').should == File.absolute_path('.')
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe '.expand2' do
|
120
|
+
it 'expand_path' do
|
121
|
+
Pa.expand2('~').should == File.expand_path('~')
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe '.shorten2' do
|
126
|
+
it 'short /home/usr/file into ~/file' do
|
127
|
+
ENV['HOME'] = '/home/foo'
|
128
|
+
Pa.shorten2('/home/foo/file').should == '~/file'
|
129
|
+
end
|
130
|
+
|
131
|
+
it 'not short /home/other-user/file' do
|
132
|
+
ENV['HOME'] = '/home/foo'
|
133
|
+
Pa.shorten2('/home/bar/file').should == '/home/bar/file'
|
41
134
|
end
|
42
135
|
end
|
43
136
|
|
137
|
+
describe '.real2' do
|
138
|
+
Pa.real2('.').should == File.realpath('.')
|
139
|
+
end
|
140
|
+
|
44
141
|
describe '.parent2' do
|
45
142
|
before :each do
|
46
|
-
@path =
|
143
|
+
@path = '/home/foo/a.txt'
|
47
144
|
end
|
48
145
|
|
49
|
-
it
|
50
|
-
Pa.parent2(@path).should ==
|
146
|
+
it 'return parent path' do
|
147
|
+
Pa.parent2(@path).should == '/home/foo'
|
51
148
|
end
|
52
149
|
|
53
|
-
it
|
54
|
-
Pa.parent2(@path, 2).should ==
|
150
|
+
it 'return parent upto 2 level path' do
|
151
|
+
Pa.parent2(@path, 2).should == '/home'
|
55
152
|
end
|
56
153
|
end
|
57
|
-
|
58
|
-
|
59
|
-
|
154
|
+
|
155
|
+
describe 'split2' do
|
156
|
+
it 'split a path into two part: dirname and basename' do
|
157
|
+
Pa.split2('/home/b/a.txt').should == ['/home/b', 'a.txt']
|
158
|
+
end
|
159
|
+
|
160
|
+
it 'with :all options: split all parts' do
|
161
|
+
Pa.split2('/home/b/a.txt', :all => true).should == ['/', 'home', 'b', 'a.txt']
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
describe 'split' do
|
166
|
+
it 'is a special case' do
|
167
|
+
Pa.split('/home/b/a.txt').should == [Pa('/home/b'), 'a.txt']
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
describe '.join2' do
|
172
|
+
it 'join a path' do
|
173
|
+
Pa.join2('/a', 'b').should == '/a/b'
|
174
|
+
end
|
175
|
+
|
176
|
+
it 'skip nil values' do
|
177
|
+
Pa.join2('/a', 'b', nil).should == '/a/b'
|
178
|
+
end
|
179
|
+
|
180
|
+
it 'skip empty values' do
|
181
|
+
Pa.join2('/a', 'b', '').should == '/a/b'
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
describe '#==' do
|
186
|
+
it 'runs ok' do
|
60
187
|
(Pa('/home') == Pa('/home')).should be_true
|
61
188
|
end
|
62
189
|
end
|
63
190
|
|
64
|
-
describe
|
65
|
-
it
|
191
|
+
describe '#+' do
|
192
|
+
it 'runs ok' do
|
66
193
|
(Pa('/home')+'~').should == Pa('/home~')
|
67
194
|
end
|
68
195
|
end
|
69
196
|
|
70
|
-
describe
|
71
|
-
it
|
197
|
+
describe '#sub2' do
|
198
|
+
it 'runs ok' do
|
72
199
|
Pa('/home/foo').sub2(/o/,'').should == '/hme/foo'
|
73
200
|
end
|
74
201
|
end
|
75
202
|
|
76
|
-
describe
|
77
|
-
it
|
203
|
+
describe '#sub!' do
|
204
|
+
it 'runs ok' do
|
78
205
|
pa = Pa('/home/foo')
|
79
206
|
pa.sub!(/o/,'')
|
80
207
|
pa.should == Pa('/hme/foo')
|
81
208
|
end
|
82
209
|
end
|
83
210
|
|
84
|
-
describe
|
85
|
-
it
|
211
|
+
describe '#gsub2' do
|
212
|
+
it 'runs ok' do
|
86
213
|
Pa('/home/foo').gsub2(/o/,'').should == '/hme/f'
|
87
214
|
end
|
88
215
|
end
|
89
216
|
|
90
|
-
describe
|
91
|
-
it
|
217
|
+
describe '#gsub!' do
|
218
|
+
it 'runs ok' do
|
92
219
|
pa = Pa('/home/foo')
|
93
220
|
pa.gsub!(/o/,'')
|
94
221
|
pa.should == Pa('/hme/f')
|
95
222
|
end
|
96
223
|
end
|
97
224
|
|
98
|
-
describe
|
99
|
-
it
|
225
|
+
describe '#match' do
|
226
|
+
it 'runs ok' do
|
100
227
|
Pa('/home/foo').match(/foo/)[0].should == 'foo'
|
101
228
|
end
|
102
229
|
end
|
103
230
|
|
104
|
-
describe
|
105
|
-
it
|
231
|
+
describe '#start_with?' do
|
232
|
+
it 'runs ok' do
|
106
233
|
Pa('/home/foo').start_with?('/home').should be_true
|
107
234
|
end
|
108
235
|
end
|
109
236
|
|
110
|
-
describe
|
111
|
-
it
|
237
|
+
describe '#end_with?' do
|
238
|
+
it 'runs ok' do
|
112
239
|
Pa('/home/foo').end_with?('foo').should be_true
|
113
240
|
end
|
114
241
|
end
|
115
242
|
|
116
|
-
describe
|
117
|
-
it
|
243
|
+
describe '#=~' do
|
244
|
+
it 'runs ok' do
|
118
245
|
(Pa('/home/foo') =~ /foo/).should be_true
|
119
246
|
end
|
120
247
|
end
|
data/spec/spec_helper.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.1.
|
4
|
+
version: 1.1.4
|
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: 2011-
|
12
|
+
date: 2011-12-08 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! 'a path library for Ruby
|
15
15
|
|
@@ -27,7 +27,8 @@ files:
|
|
27
27
|
- Ragfile
|
28
28
|
- lib/pa.rb
|
29
29
|
- lib/pa/cmd.rb
|
30
|
-
- lib/pa/
|
30
|
+
- lib/pa/directory.rb
|
31
|
+
- lib/pa/ext.rb
|
31
32
|
- lib/pa/path.rb
|
32
33
|
- lib/pa/state.rb
|
33
34
|
- lib/pa/util.rb
|
@@ -35,7 +36,7 @@ files:
|
|
35
36
|
- pa.gemspec
|
36
37
|
- pa.watchr
|
37
38
|
- spec/pa/cmd_spec.rb
|
38
|
-
- spec/pa/
|
39
|
+
- spec/pa/directory_spec.rb
|
39
40
|
- spec/pa/path_spec.rb
|
40
41
|
- spec/pa/state_spec.rb
|
41
42
|
- spec/pa_spec.rb
|
@@ -60,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
61
|
version: '0'
|
61
62
|
requirements: []
|
62
63
|
rubyforge_project: xx
|
63
|
-
rubygems_version: 1.8.
|
64
|
+
rubygems_version: 1.8.11
|
64
65
|
signing_key:
|
65
66
|
specification_version: 3
|
66
67
|
summary: a path library for Ruby
|