pa 1.1.4 → 1.2.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/Gemfile +4 -4
- data/Gemfile.lock +8 -8
- data/lib/pa/cmd.rb +124 -87
- data/lib/pa/directory.rb +55 -19
- data/lib/pa/path.rb +38 -180
- data/lib/pa/state.rb +12 -4
- data/lib/pa/version.rb +1 -1
- data/lib/pa.rb +318 -38
- data/spec/pa/cmd_spec.rb +50 -17
- data/spec/pa/directory_spec.rb +107 -16
- data/spec/pa/path_spec.rb +71 -173
- data/spec/pa/state_spec.rb +9 -3
- data/spec/pa_spec.rb +261 -1
- data/spec/spec_helper.rb +39 -0
- metadata +9 -2
data/lib/pa/path.rb
CHANGED
@@ -12,31 +12,11 @@ attribute absolute and dir return String, method absolute_path(), dirname() retu
|
|
12
12
|
|
13
13
|
=end
|
14
14
|
class Pa
|
15
|
-
NAME_EXT_PAT = /^(.+?)(?:\.([^.]+))?$/
|
16
15
|
module Path
|
17
16
|
extend Util::Concern
|
18
17
|
|
19
18
|
module ClassMethods
|
20
|
-
|
21
|
-
#
|
22
|
-
# return obj#path if object has a 'path' instance method
|
23
|
-
#
|
24
|
-
# nil -> nil
|
25
|
-
#
|
26
|
-
#
|
27
|
-
# @param [String,#path] obj
|
28
|
-
# @return [String,nil] path
|
29
|
-
def get(obj)
|
30
|
-
if obj.respond_to?(:path)
|
31
|
-
obj.path
|
32
|
-
elsif String === obj
|
33
|
-
obj
|
34
|
-
elsif obj.nil?
|
35
|
-
nil
|
36
|
-
else
|
37
|
-
raise ArgumentError, "Pa.get() not support type -- #{obj.inspect}(#{obj.class})"
|
38
|
-
end
|
39
|
-
end
|
19
|
+
DELEGATE_METHODS = [:pwd, :dir, :absolute, :expand, :real, :parent]
|
40
20
|
|
41
21
|
# return current work directory
|
42
22
|
# @return [String] path
|
@@ -69,7 +49,6 @@ class Pa
|
|
69
49
|
end
|
70
50
|
end # def dsymlink?
|
71
51
|
|
72
|
-
|
73
52
|
def dir2(path)
|
74
53
|
File.dirname(path)
|
75
54
|
end
|
@@ -88,13 +67,23 @@ class Pa
|
|
88
67
|
def base2(name, o={})
|
89
68
|
name = File.basename(get(name))
|
90
69
|
if o[:ext]
|
91
|
-
name, ext = name.match(
|
70
|
+
name, ext = name.match(/^(.+?)(?:\.([^.]+))?$/).captures
|
92
71
|
[ name, (ext || "")]
|
93
72
|
else
|
94
73
|
name
|
95
74
|
end
|
96
75
|
end
|
97
76
|
|
77
|
+
def base(*args, &blk)
|
78
|
+
rst = base2(*args, &blk)
|
79
|
+
|
80
|
+
if Array===rst
|
81
|
+
[ Pa(rst[0]), rst[1] ]
|
82
|
+
else
|
83
|
+
rst
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
98
87
|
# ext of a path
|
99
88
|
#
|
100
89
|
# @example
|
@@ -108,6 +97,8 @@ class Pa
|
|
108
97
|
ext
|
109
98
|
end
|
110
99
|
|
100
|
+
alias ext ext2
|
101
|
+
|
111
102
|
# alias from File.absolute_path
|
112
103
|
# @param [String,Pa] path
|
113
104
|
# @return [String]
|
@@ -131,6 +122,8 @@ class Pa
|
|
131
122
|
get(path).sub /^#{Regexp.escape(ENV["HOME"])}/, "~"
|
132
123
|
end
|
133
124
|
|
125
|
+
alias shorten shorten2
|
126
|
+
|
134
127
|
# real path
|
135
128
|
def real2(path)
|
136
129
|
File.realpath get(path)
|
@@ -148,171 +141,36 @@ class Pa
|
|
148
141
|
end
|
149
142
|
path
|
150
143
|
end
|
151
|
-
|
152
|
-
# split path
|
153
|
-
#
|
154
|
-
# @example
|
155
|
-
# path="/home/a/file"
|
156
|
-
# split2(path) #=> "/home/a", "file"
|
157
|
-
# split2(path, :all => true) #=> "/", "home", "a", "file"
|
158
|
-
#
|
159
|
-
# @param [String,Pa] name
|
160
|
-
# @param [Hash] o option
|
161
|
-
# @option o [Boolean] :all split all parts
|
162
|
-
# @return [Array<String>]
|
163
|
-
def split2(name, o={})
|
164
|
-
dir, fname = File.split(get(name))
|
165
|
-
ret = Util.wrap_array(File.basename(fname))
|
166
|
-
|
167
|
-
if o[:all]
|
168
|
-
loop do
|
169
|
-
dir1, fname = File.split(dir)
|
170
|
-
break if dir1 == dir
|
171
|
-
ret.unshift fname
|
172
|
-
dir = dir1
|
173
|
-
end
|
174
|
-
end
|
175
|
-
ret.unshift dir
|
176
|
-
ret
|
177
|
-
end
|
178
|
-
|
179
|
-
# special case
|
180
|
-
def split(*args)
|
181
|
-
dir, *names = split2(*args)
|
182
|
-
[ Pa(dir), *names]
|
183
|
-
end
|
184
144
|
|
185
|
-
|
186
|
-
|
187
|
-
# @param [*Array<String>] *paths
|
188
|
-
# @return [String]
|
189
|
-
def join2(*paths)
|
190
|
-
paths.map!{|v|get(v)}
|
191
|
-
|
192
|
-
# skip nil
|
193
|
-
paths.compact!
|
145
|
+
DELEGATE_METHODS.each { |mth|
|
146
|
+
mth2 = "#{mth}2"
|
194
147
|
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
148
|
+
class_eval <<-EOF
|
149
|
+
def #{mth}(*args, &blk)
|
150
|
+
Pa(Pa.#{mth2}(*args, &blk))
|
151
|
+
end
|
152
|
+
EOF
|
153
|
+
}
|
200
154
|
end
|
201
155
|
|
202
156
|
module InstanceMethods
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
# @return [String] ext "", "ogg"
|
207
|
-
attr_reader :ext2
|
208
|
-
|
209
|
-
# @return [String] ext "", ".ogg"
|
210
|
-
attr_reader :fext2
|
211
|
-
|
212
|
-
def initialize_variables
|
213
|
-
super
|
214
|
-
@absolute2 = File.absolute_path(@path)
|
215
|
-
@dir2 = File.dirname(@path)
|
216
|
-
@base2 = File.basename(@path)
|
217
|
-
@name2, @ext2 = @base2.match(NAME_EXT_PAT).captures
|
218
|
-
@ext2 ||= ""
|
219
|
-
@fext2 = @ext2.empty? ? "" : "."+@ext2
|
220
|
-
end
|
221
|
-
|
222
|
-
alias a2 absolute2
|
223
|
-
alias d2 dir2
|
224
|
-
alias b2 base2
|
225
|
-
alias n2 name2
|
226
|
-
alias fname2 base2
|
227
|
-
alias fn2 fname2
|
228
|
-
alias e2 ext2
|
229
|
-
alias fe2 fext2
|
230
|
-
|
231
|
-
# fix name,2 => String
|
232
|
-
alias base base2
|
233
|
-
alias fname fname2
|
234
|
-
alias name name2
|
235
|
-
alias ext ext2
|
236
|
-
alias fext fext2
|
237
|
-
|
238
|
-
alias b base
|
239
|
-
alias fn fname
|
240
|
-
alias n name
|
241
|
-
alias e ext
|
242
|
-
alias fe fext
|
243
|
-
|
244
|
-
def short2
|
245
|
-
@short2 ||= Pa.shorten2(@path)
|
246
|
-
end
|
247
|
-
|
248
|
-
# add string to path
|
249
|
-
#
|
250
|
-
# @example
|
251
|
-
# pa = Pa('/home/foo/a.txt')
|
252
|
-
# pa+'~' #=> new Pa('/home/foo/a.txt~')
|
253
|
-
#
|
254
|
-
# @param [String] str
|
255
|
-
# @return [Pa]
|
256
|
-
def +(str)
|
257
|
-
Pa(path+str)
|
258
|
-
end
|
259
|
-
|
260
|
-
# @return [String]
|
261
|
-
def sub2(*args, &blk)
|
262
|
-
path.sub(*args, &blk)
|
263
|
-
end
|
264
|
-
|
265
|
-
# @return [String]
|
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))
|
278
|
-
end
|
279
|
-
|
280
|
-
# @return [Pa]
|
281
|
-
def sub!(*args,&blk)
|
282
|
-
self.replace path.sub(*args,&blk)
|
283
|
-
end
|
284
|
-
|
285
|
-
# @return [Pa]
|
286
|
-
def gsub!(*args,&blk)
|
287
|
-
self.replace path.gsub(*args,&blk)
|
288
|
-
end
|
289
|
-
|
290
|
-
# @return [MatchData]
|
291
|
-
def match(*args,&blk)
|
292
|
-
path.match(*args,&blk)
|
293
|
-
end
|
294
|
-
|
295
|
-
# @return [Boolean]
|
296
|
-
def start_with?(*args)
|
297
|
-
path.start_with?(*args)
|
298
|
-
end
|
299
|
-
|
300
|
-
# @return [Boolean]
|
301
|
-
def end_with?(*args)
|
302
|
-
path.end_with?(*args)
|
303
|
-
end
|
157
|
+
DELEGATE_METHODS2 = [ :parent2 ]
|
158
|
+
DELEGATE_METHODS = [ :parent]
|
304
159
|
|
305
|
-
|
306
|
-
|
160
|
+
DELEGATE_METHODS2.each do |mth2|
|
161
|
+
class_eval <<-EOF
|
162
|
+
def #{mth2}(*args, &blk)
|
163
|
+
Pa.#{mth2}(path, *args, &blk)
|
164
|
+
end
|
165
|
+
EOF
|
307
166
|
end
|
308
167
|
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
end
|
168
|
+
DELEGATE_METHODS.each do |mth|
|
169
|
+
class_eval <<-EOF
|
170
|
+
def #{mth}(*args, &blk)
|
171
|
+
Pa(#{mth}2(*args, &blk))
|
172
|
+
end
|
173
|
+
EOF
|
316
174
|
end
|
317
175
|
end
|
318
176
|
end
|
data/lib/pa/state.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
class Pa
|
2
2
|
module State
|
3
3
|
extend Util::Concern
|
4
|
-
module ClassMethods
|
5
|
-
# goes to File
|
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
4
|
|
5
|
+
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? ]
|
6
|
+
|
7
|
+
module ClassMethods
|
8
8
|
# delegated from File
|
9
9
|
FILE_DELEGATED_METHODS.each { |name|
|
10
10
|
module_eval <<-METHOD, __FILE__, __LINE__
|
@@ -64,7 +64,6 @@ class Pa
|
|
64
64
|
end
|
65
65
|
end # def type
|
66
66
|
|
67
|
-
|
68
67
|
# is path a mountpoint?
|
69
68
|
#
|
70
69
|
# @param[String] path
|
@@ -82,6 +81,15 @@ class Pa
|
|
82
81
|
end
|
83
82
|
|
84
83
|
module InstanceMethods
|
84
|
+
# delegated from File
|
85
|
+
FILE_DELEGATED_METHODS.each { |name|
|
86
|
+
module_eval <<-METHOD, __FILE__, __LINE__
|
87
|
+
def #{name}(*args)
|
88
|
+
File.#{name}(*args, path)
|
89
|
+
end
|
90
|
+
METHOD
|
91
|
+
}
|
92
|
+
|
85
93
|
def chmod(mode)
|
86
94
|
File.chmod(mode, path)
|
87
95
|
end
|
data/lib/pa/version.rb
CHANGED