pa 1.3.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1 +1,5 @@
1
+ /tags
1
2
  /*.gem
3
+ /spec/tmp/*
4
+ /.yardoc
5
+ .rbx
@@ -1,10 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.8.7
4
- - 1.9.2
5
3
  - 1.9.3
6
- - jruby-18mode
7
- - jruby-19mode
8
- - rbx-18mode
9
4
  - rbx-19mode
10
5
  script: bundle exec rspec spec
@@ -0,0 +1,6 @@
1
+ *Pa 1.3.0 (Aug 28, 2012)*
2
+ * change 'Pa.ext2' return from "ext" to ".ext", 'Pa.fext2' return from ".ext" to "ext"
3
+ * remove 'Pa.fname2'
4
+ * remove 'Pa.build2' and change 'Pa#build2' to 'Pa#change2'
5
+
6
+ * add 'Pa.relative_to?', 'Pa.relative_to2', 'Pa.has_ext?', 'Pa.delete_ext2', 'Pa.add_ext2'
@@ -1,22 +1,21 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- awesome_print (1.0.2)
5
4
  diff-lcs (1.1.3)
6
- pd (1.0.5)
7
- awesome_print
8
- rspec (2.8.0)
9
- rspec-core (~> 2.8.0)
10
- rspec-expectations (~> 2.8.0)
11
- rspec-mocks (~> 2.8.0)
12
- rspec-core (2.8.0)
13
- rspec-expectations (2.8.0)
14
- diff-lcs (~> 1.1.2)
15
- rspec-mocks (2.8.0)
16
- thor (0.14.6)
5
+ pd (1.1.0)
6
+ rspec (2.11.0)
7
+ rspec-core (~> 2.11.0)
8
+ rspec-expectations (~> 2.11.0)
9
+ rspec-mocks (~> 2.11.0)
10
+ rspec-core (2.11.1)
11
+ rspec-expectations (2.11.2)
12
+ diff-lcs (~> 1.1.3)
13
+ rspec-mocks (2.11.2)
14
+ thor (0.15.4)
17
15
  watchr (0.7)
18
16
 
19
17
  PLATFORMS
18
+ java
20
19
  ruby
21
20
 
22
21
  DEPENDENCIES
data/README.md CHANGED
@@ -1,17 +1,13 @@
1
1
  # Pa, a path libraray for Ruby [![Build Status](https://secure.travis-ci.org/GutenYe/pa.png)](http://travis-ci.org/GutenYe/pa)
2
2
 
3
- | Homepage: | https://github.com/GutenYe/pa |
3
+ | | |
4
4
  |----------------|--------------------------------------|
5
+ | Homepage: | https://github.com/GutenYe/pa |
5
6
  | Author: | Guten |
6
7
  | License: | MIT-LICENSE |
7
8
  | Documentation: | http://rubydoc.info/gems/pa/frames |
8
9
  | Issue Tracker: | https://github.com/GutenYe/pa/issues |
9
-
10
-
11
- Overview
12
- --------
13
-
14
- a path library for Ruby
10
+ | Platforms: | Ruby 1.9.3, Rubinius |
15
11
 
16
12
  Usage
17
13
  -----
@@ -49,13 +45,12 @@ Contributors
49
45
 
50
46
  * [contributors](https://github.com/GutenYe/pa/contributors)
51
47
 
52
-
53
48
  Copyright
54
49
  ---------
55
50
 
56
51
  (the MIT License)
57
52
 
58
- Copyright (c) 2011 Guten
53
+ Copyright (c) 2011-2012 Guten
59
54
 
60
55
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
61
56
 
data/lib/pa.rb CHANGED
@@ -9,24 +9,27 @@ all class methods support Pa as parameter.
9
9
 
10
10
  support "~/foo" path. Pa("~/foo") is "/home/x/foo"
11
11
 
12
- Examples:
12
+ Filename parts:
13
13
  ---------
14
- pa = Pa('/home/a.vim')
15
- pa.dir #=> '/home'
16
- pa.base #=> 'a.vim'
17
- pa.name #=> 'a'
18
- pa.ext #=> 'vim'
19
- pa.fext #=> '.vim'
20
14
 
21
- pa.dir_pa #=> Pa('/home') # similar, but return <#Pa>
15
+ /home/guten.ogg
22
16
 
23
- Filename parts:
17
+ dir: /home
18
+ base: guten.ogg
19
+ name: guten
20
+ ext: .ogg
21
+ fext: ogg
22
+
23
+ Examples:
24
24
  ---------
25
- /home/guten.ogg
26
- base: guten.ogg
27
- dir: /home
28
- ext: ogg
29
- name: guten
25
+
26
+ pa = Pa('/home/a.vim')
27
+ pa.dir2 -> '/home'
28
+ pa.base2 -> 'a.vim'
29
+ pa.name2 -> 'a'
30
+ pa.ext2 -> '.vim'
31
+
32
+ pa.dir -> Pa('/home') # similar, but return <#Pa>
30
33
 
31
34
  Additional method list
32
35
  ---------------------
@@ -34,20 +37,25 @@ Additional method list
34
37
  * Pa.expand _aliss from `File.expand_path`_
35
38
 
36
39
  === create, modify path
40
+
37
41
  Example1:
42
+
38
43
  pa = Pa('/home/foo')
39
- pa.join('a.txt') #=> new Pa('/home/foo/a.txt')
44
+ pa.join2('a.txt') -> '/home/foo/a.txt'
45
+ pa.join('a.txt') -> Pa('/home/foo/a.txt')
40
46
 
41
47
  Example2:
48
+
42
49
  pa1 = Pa('/home/foo/a.txt')
43
50
  pa2 = Pa('/home/bar/b.txt')
44
51
  pa1+'~' #=> new Pa('/home/foo/a.txt~')
45
52
  Pa.join(pa1.dir, pa2.base) #=> '/home/foo/b.txt'
46
53
 
47
54
  Example3:
55
+
48
56
  pa1 = Pa('/home/foo/a.txt')
49
57
  pa2 = Pa('/home/bar')
50
- pa2.join(pa1.base) #=> new Pa('/home/bar/a.txt')
58
+ pa2.join2(pa1.base) -> '/home/bar/a.txt'
51
59
 
52
60
  **Attributes**
53
61
 
@@ -57,10 +65,9 @@ Example3:
57
65
  absolute a absolute path
58
66
  dir d dirname of a path
59
67
  base b basename of a path
60
- fname fn alias of base
61
- name n filename of a path
62
- ext e extname of a path, return "" or "ogg"
63
- fext fe return "" or ".ogg"
68
+ ext e extname of a path
69
+ name n filename without ext
70
+ fext fe extname without "."
64
71
 
65
72
  == used with rspec
66
73
 
@@ -75,9 +82,9 @@ class Pa
75
82
  Error = Class.new Exception
76
83
  EUnkonwType = Class.new Error
77
84
 
78
- class << self
79
- DELEGATE_METHODS = [:join, :build]
85
+ DELEGATE_CLASS_METHODS = [:absolute, :dir, :dir_stict, :name, :ext, :fext]
80
86
 
87
+ class << self
81
88
  # get path of an object.
82
89
  #
83
90
  # return obj#path if object has a 'path' instance method
@@ -99,26 +106,98 @@ class Pa
99
106
  end
100
107
  end
101
108
 
109
+ def absolute2(path)
110
+ File.absolute_path(get(path), ".") # rbx
111
+ end
112
+
113
+ # => ".", "..", "/", "c:"
114
+ #
115
+ # "foo" => "."
116
+ # "./foo" => "."
117
+ # "../../foo" => "../.."
118
+ #
119
+ def dir2(path)
120
+ File.dirname(get(path))
121
+ end
122
+
123
+ # Pa("foo") => ""
124
+ # Pa("./foo") => "."
125
+ def dir_strict2(path)
126
+ dir = File.dirname(get(path))
127
+
128
+ if %w[.].include?(dir) && path !~ %r~^\./~
129
+ ""
130
+ else
131
+ dir
132
+ end
133
+ end
134
+
135
+ # get a basename of a path
136
+ #
137
+ # @example
138
+ # Pa.basename("foo.bar.c", ext: true) #=> \["foo.bar", "c"]
139
+ #
140
+ # @param [String,Pa] name
141
+ # @param [Hash] o options
142
+ # @option o [Boolean, String] :ext (false) return \[name, ext] if true
143
+ #
144
+ # @return [String] basename of a path unless o[:ext]
145
+ # @return [Array<String>] \[name, ext] if o[:ext].
146
+ def base2(name, o={})
147
+ name = File.basename(get(name))
148
+ if o[:ext]
149
+ name, ext = name.match(/^(.+?)(?:\.([^.]+))?$/).captures
150
+ [ name, (ext || "")]
151
+ else
152
+ name
153
+ end
154
+ end
155
+
156
+ def base(*args, &blk)
157
+ ret = base2(*args, &blk)
158
+
159
+ if Array === ret
160
+ [ Pa(ret[0]), ret[1] ]
161
+ else
162
+ Pa(ret)
163
+ end
164
+ end
165
+
166
+ def name2(path)
167
+ File.basename(get(path)).match(/^(.+?)(?:\.([^.]+))?$/)[1]
168
+ end
169
+
170
+ # -> ".ogg", ""
171
+ def ext2(path)
172
+ File.extname(get(path))
173
+ end
174
+
175
+ # => "ogg", ""
176
+ def fext2(path)
177
+ File.extname(get(path)).gsub(/^\./, "")
178
+ end
179
+
102
180
  # split path
103
181
  #
104
182
  # @example
183
+ #
105
184
  # path="/home/a/file"
106
- # split2(path) #=> "/home/a", "file"
107
- # split2(path, :all => true) #=> "/", "home", "a", "file"
185
+ # split2(path) -> ["/home/a", "file"]
186
+ # split2(path, :all => true) -> ["/", "home", "a", "file"]
108
187
  #
109
- # @param [String,Pa] name
188
+ # @param [String,Pa] path
110
189
  # @param [Hash] o option
111
190
  # @option o [Boolean] :all split all parts
112
191
  # @return [Array<String>]
113
- def split2(name, o={})
114
- dir, fname = File.split(get(name))
115
- ret = Util.wrap_array(File.basename(fname))
192
+ def split2(path, o={})
193
+ dir, base = File.split(get(path))
194
+ ret = Util.wrap_array(File.basename(base))
116
195
 
117
196
  if o[:all]
118
197
  loop do
119
- dir1, fname = File.split(dir)
198
+ dir1, base = File.split(dir)
120
199
  break if dir1 == dir
121
- ret.unshift fname
200
+ ret.unshift base
122
201
  dir = dir1
123
202
  end
124
203
  end
@@ -134,6 +213,10 @@ class Pa
134
213
 
135
214
  # join paths, skip nil and empty string.
136
215
  #
216
+ # @example
217
+ #
218
+ # Pa.join2("", "foo", nil, "bar") -> "foo/bar"
219
+ #
137
220
  # @param [*Array<String>] *paths
138
221
  # @return [String]
139
222
  def join2(*paths)
@@ -147,29 +230,10 @@ class Pa
147
230
  File.join(*paths)
148
231
  end
149
232
 
150
- # build a path
151
- # options :path, :dir, :fname, :base, :name, :fext, :ext
152
- # use Pa.join2
153
- # @example
154
- #
155
- # Pa.build2(dir: "/home", name: "guten", ext: "avi") => "/home/guten.avi
156
- # Pa.build2("/home/guten.avi") { |pa| "#{pa.dir}/foo.#{pa.ext}" } => "/home/foo.avi
157
- #
158
- # @overload build2(path){|pa|=>String}
159
- # @overload build2(data={})
160
- # @overload build2(data={}){}
161
- def build2(*args, &blk)
162
- data = Hash===args.last ? args.pop : {}
163
- path = args[0] || build_path2(data)
164
- blk ||= proc {|pa| pa.p }
165
-
166
- blk.call(Pa(path))
167
- end
168
-
169
- DELEGATE_METHODS.each { |mth|
170
- class_eval <<-EOF
171
- def #{mth}(*args, &blk)
172
- Pa(Pa.#{mth}2(*args, &blk))
233
+ DELEGATE_CLASS_METHODS.each {|meth|
234
+ eval <<-EOF
235
+ def #{meth}(*args, &blk)
236
+ Pa(#{meth}2(*args, &blk))
173
237
  end
174
238
  EOF
175
239
  }
@@ -185,31 +249,17 @@ class Pa
185
249
  Pa(obj)
186
250
  end
187
251
  end
188
-
189
- def build_path2(data={})
190
- if data[:path]
191
- path = data[:path]
192
- elsif data[:fname] || data[:base]
193
- path = join2(data[:dir], data[:fname] || data[:base])
194
- else
195
- path = join2(data[:dir], data[:name])
196
- if data[:fext]
197
- path << data[:fext]
198
- elsif data[:ext]
199
- path << ".#{data[:ext]}"
200
- end
201
- end
202
-
203
- path
204
- end
205
252
  end
206
253
 
254
+ DELEGATE_ATTR_METHODS2 = [ :dir2, :dir_strict2, :base2, :name2, :ext2, :fext2]
255
+ DELEGATE_ATTR_METHODS = [ :absolute, :dir, :dir_strict, :rel, :rea ]
207
256
  DELEGATE_METHODS2 = [ :join2 ]
208
- DELEGATE_METHODS = [ :build, :join ]
209
- DELEGATE_ATTR_METHODS = [ :dir, :rel, :rea ]
257
+ DELEGATE_METHODS = [ :change, :join]
258
+ DELEGATE_TO_PATH2 = [ :sub2, :gsub2 ]
259
+ DELEGATE_TO_PATH = [:match, :start_with?, :end_with?]
210
260
 
211
261
  attr_reader :path2
212
- attr_reader :absolute2, :dir2, :dir_strict2, :base2, :fname2, :name2, :short2, :ext2, :fext2, :rel2, :rea2
262
+ attr_reader :absolute2, :dir2, :dir_strict2, :base2, :name2, :short2, :ext2, :fext2, :rel2, :rea2
213
263
  attr_reader :options
214
264
 
215
265
  # @param [Hash] o option
@@ -232,6 +282,54 @@ class Pa
232
282
  end
233
283
  include chainable
234
284
 
285
+ DELEGATE_ATTR_METHODS2.each {|meth2|
286
+ class_eval <<-EOF
287
+ def #{meth2}(*args, &blk)
288
+ @#{meth2} ||= Pa.#{meth2}(path, *args, &blk)
289
+ end
290
+ EOF
291
+ }
292
+
293
+ DELEGATE_ATTR_METHODS.each {|meth|
294
+ class_eval <<-EOF
295
+ def #{meth}(*args, &blk)
296
+ @#{meth} ||= Pa(#{meth}2(*args, &blk))
297
+ end
298
+ EOF
299
+ }
300
+
301
+ DELEGATE_METHODS2.each { |meth2|
302
+ class_eval <<-EOF
303
+ def #{meth2}(*args, &blk)
304
+ Pa.#{meth2}(path, *args, &blk)
305
+ end
306
+ EOF
307
+ }
308
+
309
+ DELEGATE_METHODS.each {|meth|
310
+ class_eval <<-EOF
311
+ def #{meth}(*args, &blk)
312
+ Pa(#{meth}2(*args, &blk))
313
+ end
314
+ EOF
315
+ }
316
+
317
+ DELEGATE_TO_PATH2.each {|meth2|
318
+ class_eval <<-EOF
319
+ def #{meth2}(*args, &blk)
320
+ path.#{meth2[0...-1]}(*args, &blk)
321
+ end
322
+ EOF
323
+ }
324
+
325
+ DELEGATE_TO_PATH.each {|meth|
326
+ class_eval <<-EOF
327
+ def #{meth}(*args, &blk)
328
+ path.#{meth}(*args, &blk)
329
+ end
330
+ EOF
331
+ }
332
+
235
333
  def base_dir
236
334
  @base_dir ||= (options[:base_dir] || ".")
237
335
  end
@@ -245,69 +343,12 @@ class Pa
245
343
  end
246
344
 
247
345
  def absolute2
248
- @absolute2 ||= File.absolute_path(rea2)
249
- end
250
-
251
- def absolute
252
- @absolute ||= Pa(absolute2)
253
- end
254
-
255
- # => ".", "..", "/", "c:"
256
- #
257
- # "foo" => "."
258
- # "./foo" => "."
259
- # "../../foo" => "../.."
260
- #
261
- def dir2
262
- @dir2 ||= File.dirname(path)
263
- end
264
-
265
- def dir
266
- @dir ||= Pa(dir2)
267
- end
268
-
269
- # Pa("foo") => ""
270
- # Pa("./foo") => "."
271
- def dir_strict2
272
- return @dir_strict2 if @dir_strict2
273
-
274
- dir = File.dirname(path)
275
-
276
- @dir_strict2 = if %w[.].include?(dir) && path !~ %r~^\./~
277
- ""
278
- else
279
- dir
280
- end
281
- end
282
-
283
- def dir_strict
284
- @dir_strict ||= Pa(dir_strict2)
285
- end
286
-
287
- def base2
288
- @base2 ||= File.basename(path)
346
+ @absolute2 ||= Pa.absolute2(rea2)
289
347
  end
290
348
 
291
- def name2
292
- @name2 ||= File.basename(path).match(/^(.+?)(?:\.([^.]+))?$/)[1]
293
- end
294
-
295
- # => "ogg", ""
296
- def ext2
297
- @ext2 ||= File.basename(path).match(/^(.+?)(?:\.([^.]+))?$/)[2] || ""
298
- end
299
-
300
- # => ".ogg", ""
301
- def fext2
302
- @fext2 ||= ext2.empty? ? "" : ".#{ext2}"
303
- end
304
-
305
- alias fname2 base2
306
-
307
349
  # both x, x2 return String
308
350
  alias path path2
309
351
  alias base base2
310
- alias fname fname2
311
352
  alias name name2
312
353
  alias ext ext2
313
354
  alias fext fext2
@@ -320,12 +361,10 @@ class Pa
320
361
  alias d_s2 dir_strict2
321
362
  alias b2 base2
322
363
  alias n2 name2
323
- alias fn2 fname2
324
364
  alias e2 ext2
325
365
  alias fe2 fext2
326
366
  alias p path
327
367
  alias b base
328
- alias fn fname
329
368
  alias n name
330
369
  alias e ext
331
370
  alias fe fext
@@ -355,6 +394,7 @@ class Pa
355
394
  # @return [Pa] the same Pa object
356
395
  def replace(path)
357
396
  @path2 = Pa.get(path)
397
+
358
398
  initialize_variables
359
399
  end
360
400
 
@@ -395,16 +435,6 @@ class Pa
395
435
  @short ||= Pa(short2)
396
436
  end
397
437
 
398
- # @return [String]
399
- def sub2(*args, &blk)
400
- path.sub(*args, &blk)
401
- end
402
-
403
- # @return [String]
404
- def gsub2(*args, &blk)
405
- path.gsub(*args, &blk)
406
- end
407
-
408
438
  # @return [Pa]
409
439
  def sub(*args, &blk)
410
440
  Pa(sub2(*args, &blk))
@@ -417,67 +447,35 @@ class Pa
417
447
 
418
448
  # @return [Pa]
419
449
  def sub!(*args,&blk)
420
- self.replace path.sub(*args,&blk)
450
+ replace path.sub(*args,&blk)
421
451
  end
422
452
 
423
453
  # @return [Pa]
424
454
  def gsub!(*args,&blk)
425
- self.replace path.gsub(*args,&blk)
455
+ replace path.gsub(*args,&blk)
426
456
  end
427
457
 
428
- # @return [MatchData]
429
- def match(*args,&blk)
430
- path.match(*args,&blk)
431
- end
432
-
433
- # @return [Boolean]
434
- def start_with?(*args)
435
- path.start_with?(*args)
436
- end
437
-
438
- # @return [Boolean]
439
- def end_with?(*args)
440
- path.end_with?(*args)
441
- end
442
-
443
- # @return [String]
444
- def build2(data={}, &blk)
458
+ # Change some parts of the path.
459
+ #
460
+ # path
461
+ # dir base
462
+ # dir name ext
463
+ # ...
464
+ #
465
+ # @return [String] path
466
+ def change2(data={}, &blk)
445
467
  return Pa.new(blk.call(self)) if blk
446
468
 
447
- d = if data[:path]
448
- {path: data[:path]}
449
- elsif data[:fname] || data[:base]
450
- {dir: dir_strict2, fname: data[:fname], base: data[:base]}
469
+ if data[:path]
470
+ return data[:path]
471
+ elsif data[:base]
472
+ return File.join(data[:dir] || dir2, data[:base])
451
473
  else
452
- {dir: dir_strict2, name: name2, ext: ext2}.merge(data)
453
- end
474
+ dir, name, ext = data[:dir] || dir2, data[:name] || name2, data[:ext] || ext2
454
475
 
455
- Pa.build2(d)
476
+ File.join(dir, name)+ext
477
+ end
456
478
  end
457
-
458
- DELEGATE_METHODS2.each { |mth2|
459
- class_eval <<-EOF
460
- def #{mth2}(*args, &blk)
461
- Pa.#{mth2}(path, *args, &blk)
462
- end
463
- EOF
464
- }
465
-
466
- DELEGATE_METHODS.each {|mth|
467
- class_eval <<-EOF
468
- def #{mth}(*args, &blk)
469
- Pa(#{mth}2(*args, &blk))
470
- end
471
- EOF
472
- }
473
-
474
- DELEGATE_ATTR_METHODS.each {|mth|
475
- class_eval <<-EOF
476
- def #{mth}(*args, &blk)
477
- @#{mth} ||= Pa(#{mth}2(*args, &blk))
478
- end
479
- EOF
480
- }
481
479
  end
482
480
 
483
481
  require "pa/path"