pa 1.3.2 → 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_DISABLE_SHARED_GEMS: '1'
@@ -1,20 +1,25 @@
1
+ ## 1.4.0 (xx)
2
+
3
+ Changes
4
+
5
+ - `Pa.rm`, `Pa.cp` ... doesn't support globbing any more.
6
+ - `Pa.has_ext?(path, ext)` -> `Pa.has_ext?(path, *exts)`
7
+ - `Pa.delete_ext?(path, ext)` -> `Pa.delete_ext?(path, *exts)`
8
+
1
9
  ## 1.3.2 (Sep 9, 2012)
2
10
 
3
11
  Improvements
4
12
 
5
- - `Pa.expand2` and `Pa.real2` support second arguments as `(name, dir=".")` ([@GutenYe][])
13
+ - `Pa.expand2` and `Pa.real2` support second arguments as `(name, dir=".")` *GutenYe*
6
14
 
7
15
  ## 1.3.0 (Aug 28, 2012)
8
16
 
9
17
  Improvements
10
18
 
11
- - add `Pa.relative_to?`, `Pa.relative_to2`, `Pa.has_ext?`, `Pa.delete_ext2`, `Pa.add_ext2` ([@GutenYe][])
19
+ - add `Pa.relative_to?`, `Pa.relative_to2`, `Pa.has_ext?`, `Pa.delete_ext2`, `Pa.add_ext2` *GutenYe*
12
20
 
13
21
  Changes
14
22
 
15
23
  - change `Pa.ext2` return from "ext" to ".ext", `Pa.fext2` return from ".ext" to "ext"
16
24
  - remove `Pa.fname2`
17
25
  - remove `Pa.build2` and change `Pa#build2` to `Pa#change2`
18
-
19
- <!--- The following link definition list is generated by PimpMyChangelog --->
20
- [@GutenYe]: https://github.com/GutenYe
@@ -0,0 +1,17 @@
1
+ We love pull requests. Here's a quick guide:
2
+
3
+ 1. Fork the repo.
4
+
5
+ 2. Create a topic branch from where you want to base your work.
6
+
7
+ 3. Add a test for your change. Only refactoring and documentation changes
8
+ require no new tests. If you are adding functionality or fixing a bug, we need
9
+ a test!
10
+
11
+ 4. Make the test pass with `rake test`.
12
+
13
+ 5. Push to your fork and submit a pull request.
14
+
15
+ 6. Coding Style Guide: https://gist.github.com/1105334
16
+
17
+ And in case we didn't emphasize it enough: we love tests!
data/Gemfile CHANGED
@@ -6,6 +6,7 @@ group :development do
6
6
  gem "thor"
7
7
  gem "rspec"
8
8
  gem "guard"
9
+ gem "guard-rspec"
9
10
  gem "pimpmychangelog"
10
11
  end
11
12
 
@@ -6,6 +6,9 @@ GEM
6
6
  guard (1.3.2)
7
7
  listen (>= 0.4.2)
8
8
  thor (>= 0.14.6)
9
+ guard-rspec (2.1.0)
10
+ guard (>= 1.1)
11
+ rspec (~> 2.11)
9
12
  listen (0.4.7)
10
13
  rb-fchange (~> 0.0.5)
11
14
  rb-fsevent (~> 0.9.1)
@@ -33,6 +36,7 @@ PLATFORMS
33
36
 
34
37
  DEPENDENCIES
35
38
  guard
39
+ guard-rspec
36
40
  pd
37
41
  pimpmychangelog
38
42
  rspec
data/README.md CHANGED
@@ -36,21 +36,14 @@ Install
36
36
  Development [![Dependency Status](https://gemnasium.com/GutenYe/pa.png?branch=master)](https://gemnasium.com/GutenYe/pa) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/GutenYe/pa)
37
37
  ===========
38
38
 
39
- Contributing
39
+ Contributing
40
40
  -------------
41
41
 
42
42
  * Submit any bugs/features/ideas to github issue tracker.
43
43
 
44
- Pull requests are very welcome! Please try to follow these simple rules if applicable:
44
+ Please see [Contibution Documentation](https://github.com/GutenYe/pa/blob/master/CONTRIBUTING.md).
45
45
 
46
- * Please create a topic branch for every separate change you make.
47
- * Make sure your patches are well tested. All specs run with `rake test:portability` must pass.
48
- * Coding Style Guide: https://gist.github.com/1105334
49
-
50
- Contributors
51
- ------------
52
-
53
- [https://github.com/GutenYe/pa/contributors](https://github.com/GutenYe/pa/contributors)
46
+ A list of [Contributors](https://github.com/GutenYe/pa/contributors)
54
47
 
55
48
  Copyright
56
49
  ---------
data/lib/pa.rb CHANGED
@@ -171,6 +171,17 @@ class Pa
171
171
  def ext2(path)
172
172
  File.extname(get(path))
173
173
  end
174
+
175
+ # Return path without ext.
176
+ #
177
+ # Pa.head2("/foo/a.txt") -> "/foo/a")
178
+ #
179
+ def head2(path)
180
+ p = get(path)
181
+ ext = File.extname(p)
182
+
183
+ ext.empty? ? p : p[0...-ext.length]
184
+ end
174
185
 
175
186
  # => "ogg", ""
176
187
  def fext2(path)
@@ -251,7 +262,7 @@ class Pa
251
262
  end
252
263
  end
253
264
 
254
- DELEGATE_ATTR_METHODS2 = [ :dir2, :dir_strict2, :base2, :name2, :ext2, :fext2]
265
+ DELEGATE_ATTR_METHODS2 = [ :dir2, :dir_strict2, :base2, :name2, :ext2, :fext2, :head2]
255
266
  DELEGATE_ATTR_METHODS = [ :absolute, :dir, :dir_strict, :rel, :rea ]
256
267
  DELEGATE_METHODS2 = [ :join2 ]
257
268
  DELEGATE_METHODS = [ :change, :join]
@@ -259,7 +270,7 @@ class Pa
259
270
  DELEGATE_TO_PATH = [:match, :start_with?, :end_with?]
260
271
 
261
272
  attr_reader :path2
262
- attr_reader :absolute2, :dir2, :dir_strict2, :base2, :name2, :short2, :ext2, :fext2, :rel2, :rea2
273
+ attr_reader :absolute2, :dir2, :dir_strict2, :base2, :name2, :short2, :ext2, :fext2, :head2, :rel2, :rea2
263
274
  attr_reader :options
264
275
 
265
276
  # @param [Hash] o option
@@ -44,7 +44,7 @@ class Pa
44
44
  # @overload ln(src, dest)
45
45
  # @overload ln([src,..], directory)
46
46
  #
47
- # @param [Array<String>, String] src_s support globbing
47
+ # @param [Array<String>, String] src_s
48
48
  # @param [String,Pa] dest
49
49
  # @param [Hash] o
50
50
  # @option o [Boolean] :show_cmd puts cmd
@@ -94,7 +94,7 @@ class Pa
94
94
  # @option o [Boolean] :show_cmd puts cmd
95
95
  def cd(path=ENV["HOME"], o={}, &blk)
96
96
  p = get(path)
97
- puts "cd #{p}" if (o[:verbose] or o[:show_cmd])
97
+ puts _wrap_cmd("cd #{p}", o[:show_cmd]) if (o[:verbose] or o[:show_cmd])
98
98
  Dir.chdir(p, &blk)
99
99
  end
100
100
 
@@ -108,7 +108,7 @@ class Pa
108
108
  # @return [nil]
109
109
  def chroot(path, o={})
110
110
  p = get(path)
111
- puts "chdroot #{p}" if (o[:verbose] or o[:show_cmd])
111
+ puts _wrap_cmd("chdroot #{p}", o[:show_cmd]) if (o[:verbose] or o[:show_cmd])
112
112
  Dir.chroot(p)
113
113
  end
114
114
 
@@ -178,7 +178,7 @@ class Pa
178
178
  (name,), o = Util.extract_options(args)
179
179
 
180
180
  p = _mktmpname(name, o)
181
- puts "mktmpdir #{p}" if (o[:verbose] or o[:show_cmd])
181
+ puts _wrap_cmd("mktmpdir #{p}", o[:show_cmd]) if (o[:verbose] or o[:show_cmd])
182
182
 
183
183
  Dir.mkdir(p)
184
184
 
@@ -204,7 +204,7 @@ class Pa
204
204
  (name,), o = Util.extract_options(args)
205
205
 
206
206
  p = _mktmpname(name, o)
207
- puts "mktmpfile #{p}" if (o[:verbose] or o[:show_cmd])
207
+ puts _wrap_cmd("mktmpfile #{p}", o[:show_cmd]) if (o[:verbose] or o[:show_cmd])
208
208
 
209
209
  begin
210
210
  blk.call(p)
@@ -220,7 +220,7 @@ class Pa
220
220
  (name,), o = Util.extract_options(args)
221
221
 
222
222
  p = _mktmpname(name, o)
223
- puts "mktmpfile #{p}" if (o[:verbose] or o[:show_cmd])
223
+ puts _wrap_cmd("mktmpfile #{p}", o[:show_cmd]) if (o[:verbose] or o[:show_cmd])
224
224
 
225
225
  begin
226
226
  blk.call(Pa(p))
@@ -234,16 +234,17 @@ class Pa
234
234
  # rm file only
235
235
  #
236
236
  # @overload rm(*paths, o={})
237
- # @param [String] *paths support globbing
237
+ # @param [String] *paths
238
238
  # @param o [Boolean] :verbose verbose mode
239
239
  # @param o [Boolean] :show_cmd puts cmd
240
240
  # @return [nil]
241
241
  def rm(*paths)
242
242
  paths, o = Util.extract_options(paths)
243
243
  extra_doc = o[:force] ? "-f " : nil
244
- puts "rm #{extra_doc}#{paths.join(" ")}" if o[:show_cmd]
244
+ puts _wrap_cmd("rm #{extra_doc}#{paths.join(' ')}", o[:show_cmd]) if o[:show_cmd]
245
245
 
246
- Pa.glob(*paths) { |pa|
246
+ paths.each { |path|
247
+ pa = Pa(path)
247
248
  puts "rm #{extra_doc}#{pa.p}" if o[:verbose]
248
249
 
249
250
  if File.directory?(pa.p)
@@ -266,7 +267,7 @@ class Pa
266
267
 
267
268
  # rm directory only. still remove if directory is not empty.
268
269
  #
269
- # @param [String] *paths support globbing
270
+ # @param [String] *paths
270
271
  # @param [Hash] o options
271
272
  # @option o [Boolean] :verbose verbose mode
272
273
  # @option o [Boolean] :show_cmd puts cmd
@@ -274,8 +275,9 @@ class Pa
274
275
  def rmdir(*paths)
275
276
  paths, o = Util.extract_options(paths)
276
277
  extra_doc = o[:force] ? "-f " : nil
277
- puts "rmdir #{extra_doc}#{paths.join(" ")}" if o[:show_cmd]
278
- Pa.glob(*paths) { |pa|
278
+ puts _wrap_cmd("rmdir #{extra_doc}#{paths.join(" ")}", o[:show_cmd]) if o[:show_cmd]
279
+ paths.each { |path|
280
+ pa = Pa(path)
279
281
  puts " rmdir #{extra_doc}#{pa.p}" if o[:verbose]
280
282
 
281
283
  if not File.directory?(pa.p)
@@ -309,7 +311,7 @@ class Pa
309
311
  def empty_dir(*dirs)
310
312
  dirs, o = Util.extract_options(dirs)
311
313
  extra_doc = o[:force] ? "-f " : nil
312
- puts "empty_dir #{extra_doc}#{dirs.join(" ")}" if o[:show_cmd]
314
+ puts _wrap_cmd("empty_dir #{extra_doc}#{dirs.join(" ")}", o[:show_cmd]) if o[:show_cmd]
313
315
 
314
316
  dirs.each {|dir|
315
317
  dir = Pa(dir)
@@ -335,8 +337,9 @@ class Pa
335
337
  # @return [nil]
336
338
  def rm_r(*paths)
337
339
  paths, o = Util.extract_options(paths)
338
- puts "rm -r #{path.join(" ")}" if o[:show_cmd]
339
- Pa.glob(*paths){ |pa|
340
+ puts _wrap_cmd("rm -r #{path.join(' ')}", o[:show_cmd]) if o[:show_cmd]
341
+ paths.each { |path|
342
+ pa = Pa(path)
340
343
  puts "rm -r #{pa.p}" if o[:verbose]
341
344
  File.directory?(pa.p) ? _rmdir(pa) : File.delete(pa.p)
342
345
  }
@@ -350,14 +353,15 @@ class Pa
350
353
  # pa.name == 'old'
351
354
  # end
352
355
  #
353
- # @param [String] *paths support globbing
356
+ # @param [String] *paths
354
357
  # @yield [path]
355
358
  # @yieldparam [Pa] path
356
359
  # @yieldreturn [Boolean] rm_r path if true
357
360
  # @return [nil]
358
361
  def rm_if(*paths, &blk)
359
362
  paths, o = Util.extract_options(paths)
360
- Pa.glob(*paths) do |pa|
363
+ paths.each do |path|
364
+ pa = Pa(path)
361
365
  rm_r pa, o if blk.call(pa)
362
366
  end
363
367
  end
@@ -381,7 +385,7 @@ class Pa
381
385
  # default: preverse mode, not owner.
382
386
  #
383
387
  # @overload cp(src_s, dest, o)
384
- # @param [Array<String>, String] src_s support globbing
388
+ # @param [Array<String>, String] src_s
385
389
  # @param [String,Pa] dest
386
390
  # @param [Hash] o option
387
391
  # @option o [Boolean] :mkdir mkdir(dest) if dest not exists.
@@ -395,7 +399,7 @@ class Pa
395
399
  # @yield [src,dest,o]
396
400
  # @return [nil]
397
401
  def cp(src_s, dest, o={}, &blk)
398
- srcs = Pa.glob(*Util.wrap_array(src_s)).map{|v| v.path}
402
+ srcs = Util.wrap_array(src_s).map{|v| Pa.get(v)}
399
403
  dest = Pa.get(dest)
400
404
  puts "cp #{srcs.join(" ")} #{dest}" if o[:show_cmd]
401
405
 
@@ -435,7 +439,7 @@ class Pa
435
439
  # @option o [Boolean] :fore
436
440
  # @return [nil]
437
441
  def mv(src_s, dest, o={}, &blk)
438
- srcs = Pa.glob(*Util.wrap_array(src_s)).map{|v| get(v)}
442
+ srcs = Util.wrap_array(src_s).map{|v| get(v)}
439
443
  dest = get(dest)
440
444
 
441
445
  extra_doc = o[:force] ? "-f " : nil
@@ -476,6 +480,9 @@ class Pa
476
480
  def _move(src, dest, o)
477
481
  raise Errno::EEXIST, "dest exists -- #{dest}" if File.exists?(dest) and (not o[:force])
478
482
 
483
+ # move same file.
484
+ return if File.absolute_path(src) == File.absolute_path(dest)
485
+
479
486
  # :force. mv "dir", "dira" and 'dira' exists and is a directory.
480
487
  if File.exists?(dest) and File.directory?(dest)
481
488
  ls(src) { |pa|
@@ -534,7 +541,7 @@ class Pa
534
541
  extra_doc << (o[:force] ? "-f " : "")
535
542
  puts "ln #{extra_doc}#{srcs.join(" ")} #{dest}" if o[:show_cmd]
536
543
 
537
- Pa.glob(*srcs) {|src|
544
+ srcs.each {|src|
538
545
  src = get(src)
539
546
  dest = File.join(dest, File.basename(src)) if File.directory?(dest)
540
547
 
@@ -663,6 +670,16 @@ class Pa
663
670
  end
664
671
  EOF
665
672
  end
673
+
674
+ # @private
675
+ def _wrap_cmd(cmd, pretty)
676
+ case pretty
677
+ when "$", "#"
678
+ "#{pretty} cmd"
679
+ else
680
+ cmd
681
+ end
682
+ end
666
683
  end
667
684
  end
668
685
  end
@@ -19,7 +19,6 @@ class Pa
19
19
  :relative_to, :shorten, :delete_ext, :add_ext ]
20
20
 
21
21
  module ClassMethods
22
-
23
22
  # Return current work directory
24
23
  #
25
24
  # @return [String] path
@@ -116,29 +115,31 @@ class Pa
116
115
  #
117
116
  # @example
118
117
  #
119
- # Pa.has_ext?("foo.txt", ".txt") -> true
120
- # Pa.has_ext?("foo", ".txt") -> false
118
+ # Pa.has_ext?("foo.txt", ".epub", ".txt") -> true
119
+ # Pa.has_ext?("foo", ".txt") -> false
121
120
  #
122
- def has_ext?(path, ext)
123
- Pa.ext2(get(path)) == ext
121
+ def has_ext?(path, *exts)
122
+ exts.include? Pa.ext2(get(path))
124
123
  end
125
124
 
126
125
  # Delete the tail.
127
126
  #
128
127
  # @example
129
128
  #
130
- # Pa.delete_ext2("foo.txt", ".txt") -> "foo"
129
+ # Pa.delete_ext2("foo.txt", ".epub", ".txt") -> "foo"
131
130
  # Pa.delete_ext2("foo", ".txt") -> "foo"
132
131
  # Pa.delete_ext2("foo.epub", ".txt") -> "foo.epub"
133
132
  #
134
- def delete_ext2(path, ext)
133
+ def delete_ext2(path, *exts)
135
134
  p = get(path)
136
135
 
137
- if has_ext?(p, ext)
138
- p[0...p.rindex(ext)]
139
- else
140
- p
141
- end
136
+ exts.each {|ext|
137
+ if has_ext?(p, ext)
138
+ return p[0...p.rindex(ext)]
139
+ end
140
+ }
141
+
142
+ p
142
143
  end
143
144
 
144
145
  # Ensure the tail
@@ -208,8 +209,8 @@ class Pa
208
209
 
209
210
  end
210
211
 
211
- DELEGATE_METHODS2 = [ :parent2 ]
212
- DELEGATE_METHODS = [ :parent]
212
+ DELEGATE_METHODS2 = [ :parent2, :delete_ext2, :add_ext2 ]
213
+ DELEGATE_METHODS = [ :parent, :delete_ext, :add_ext]
213
214
 
214
215
  DELEGATE_METHODS2.each do |mth2|
215
216
  class_eval <<-EOF
@@ -1,3 +1,3 @@
1
1
  class Pa
2
- VERSION = "1.3.2"
2
+ VERSION = "1.3.3"
3
3
  end
@@ -81,6 +81,9 @@ describe Pa do
81
81
  expect(Pa.has_ext?("foo.txt", ".txt")).to be_true
82
82
  expect(Pa.has_ext?("foo", ".txt")).to be_false
83
83
  expect(Pa.has_ext?("foo.1txt", ".txt")).to be_false
84
+
85
+ expect(Pa.has_ext?("foo.txt", *%w[.epub .txt])).to be_true
86
+ expect(Pa.has_ext?("foo.txt", *%w[.epub .mobi])).to be_false
84
87
  end
85
88
  end
86
89
 
@@ -89,6 +92,9 @@ describe Pa do
89
92
  expect(Pa.delete_ext2("foo.txt", ".txt")).to eq("foo")
90
93
  expect(Pa.delete_ext2("foo", ".txt")).to eq("foo")
91
94
  expect(Pa.delete_ext2("foo.epub", ".txt")).to eq("foo.epub")
95
+
96
+ expect(Pa.delete_ext2("foo.txt", *%w[.epub .txt])).to eq("foo")
97
+ expect(Pa.delete_ext2("foo.txt", *%w[.epub .mobi])).to eq("foo.txt")
92
98
  end
93
99
  end
94
100
 
@@ -94,6 +94,13 @@ describe Pa do
94
94
  end
95
95
  end
96
96
 
97
+ describe ".head2" do
98
+ it do
99
+ expect(Pa.head2("/bar/foo.txt.tar")).to eq("/bar/foo.txt")
100
+ expect(Pa.head2("/bar/foo")).to eq("/bar/foo")
101
+ end
102
+ end
103
+
97
104
  describe "split2" do
98
105
  it "split a path into two part: dirname and basename" do
99
106
  expect(Pa.split2("/home/b/a.txt")).to eq(["/home/b", "a.txt"])
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.3.2
4
+ version: 1.3.3
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-09-09 00:00:00.000000000 Z
12
+ date: 2012-11-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pd
@@ -35,10 +35,12 @@ executables: []
35
35
  extensions: []
36
36
  extra_rdoc_files: []
37
37
  files:
38
+ - .bundle/config
38
39
  - .gitignore
39
40
  - .rspec
40
41
  - .travis.yml
41
42
  - CHANGELOG.md
43
+ - CONTRIBUTING.md
42
44
  - Gemfile
43
45
  - Gemfile.lock
44
46
  - Guardfile
@@ -86,4 +88,3 @@ signing_key:
86
88
  specification_version: 3
87
89
  summary: a path library for Ruby
88
90
  test_files: []
89
- has_rdoc: