filerenamer 0.0.2 → 0.0.3

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.
@@ -0,0 +1,35 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require "optparse"
5
+
6
+ # FileRenamer 用のオプション解析器。
7
+ # ユーザはこれに任意のオプションを追加できる。
8
+ #
9
+ # 通常の OptionParser と異なり、インスタンス変数 @options に情報を格納する。
10
+ # @options は attr_reader に指定されているので、外部から読み取れる。
11
+ # @options は Hash であるので、ユーザが on で追加するブロックで
12
+ # そのまま鍵と値を追加できる。
13
+ # また、@options に追加せずに自前で何か適当な処理をしても良い。
14
+ module FileRenamer
15
+ class OptionParser < OptionParser
16
+
17
+ attr_reader :options
18
+
19
+ def initialize
20
+ @options = {}
21
+ super
22
+ on("-y", "--yes" , "Yes for all questions."){@options[:yes] = true}
23
+ on("-n", "--no" , "No for all questions."){@options[:no] = true}
24
+ on("-c", "--copy" , "Copy mode." ){@options[:copy] = true}
25
+ on("-m", "--move" , "Move mode.(default)" ){@options[:move] = true}
26
+ on("-h", "--hardlink", "Hardlink mode." ){@options[:hardlink]= true}
27
+ on("-s", "--symlink" , "Symlink mode." ){@options[:symlink] = true}
28
+ on("-q", "--quiet" , "Quiet mode. Forced non-interactive."){
29
+ @options[:quiet] = true
30
+ #このオプションが設定されているときは強制的に --yes として扱われる。
31
+ #non_interactive_mode になる。
32
+ }
33
+ end
34
+ end
35
+ end
File without changes
File without changes
File without changes
data/test/rennum/a ADDED
File without changes
data/test/rennum/b ADDED
File without changes
data/test/rennum/c ADDED
File without changes
data/test/rennum/d ADDED
File without changes
data/test/renpad/1 ADDED
File without changes
data/test/renpad/10 ADDED
File without changes
data/test/renpad/100 ADDED
File without changes
data/test/renpad/100b ADDED
File without changes
data/test/renpad/10b ADDED
File without changes
data/test/renpad/1b ADDED
File without changes
data/test/renpad/a1 ADDED
File without changes
data/test/renpad/a10 ADDED
File without changes
data/test/renpad/a100 ADDED
File without changes
data/test/renpad/a100b ADDED
File without changes
data/test/renpad/a10b ADDED
File without changes
data/test/renpad/a1b ADDED
File without changes
data/test/renreg/a(b) ADDED
File without changes
File without changes
File without changes
data/test/rensub/a0 ADDED
File without changes
data/test/rensub/a1 ADDED
File without changes
data/test/rensub/b0 ADDED
File without changes
data/test/rensub/b1 ADDED
File without changes
@@ -4,22 +4,24 @@
4
4
  require 'helper'
5
5
 
6
6
  require "test/unit"
7
- require "filerenamer/filerenamer.rb"
7
+ require "filerenamer/commander.rb"
8
8
  require "stringio"
9
9
  require "rubygems"
10
10
  gem "capture_stdout"
11
11
  require "capture_stdout"
12
12
 
13
- class FileRenamer
14
- public :check_new_names
15
- public :ask_yes?
16
- public :run
17
- public :make_new_names
18
- public :paths
19
- attr_reader :options
13
+ module FileRenamer
14
+ class Commander
15
+ public :check_new_names
16
+ public :ask_yes?
17
+ public :run
18
+ public :make_new_names
19
+ public :paths
20
+ attr_reader :options
21
+ end
20
22
  end
21
23
 
22
- class TC_FileRenamer < Test::Unit::TestCase
24
+ class TC_Commander < Test::Unit::TestCase
23
25
  EXIST_FILE = "test/filerenamer/dummy.txt"
24
26
 
25
27
  A_0 = "test/filerenamer/a0.txt"
@@ -27,10 +29,10 @@ class TC_FileRenamer < Test::Unit::TestCase
27
29
 
28
30
  def setup
29
31
  options = {}
30
- @fr00 = FileRenamer.new(options)
32
+ @fr00 = FileRenamer::Commander.new(options, [])
31
33
 
32
34
  options = {:copy => true, :yes => true}
33
- @fr01 = FileRenamer.new(options)
35
+ @fr01 = FileRenamer::Commander.new(options, [])
34
36
  end
35
37
 
36
38
  def test_initialize
@@ -39,24 +41,24 @@ class TC_FileRenamer < Test::Unit::TestCase
39
41
  :yes => true,
40
42
  :no => true,
41
43
  }
42
- assert_raise(FileRenamerOptionError){ FileRenamer.new(options) }
44
+ assert_raise(FileRenamer::Commander::OptionError){ FileRenamer::Commander.new(options, []) }
43
45
 
44
46
  # ファイル操作モードがどれか1つのみ true なら OK。
45
47
  options = { :copy => true, }
46
- assert_nothing_raised{ FileRenamer.new(options) }
48
+ assert_nothing_raised{ FileRenamer::Commander.new(options, []) }
47
49
 
48
50
  options = { :move => true, }
49
- assert_nothing_raised{ FileRenamer.new(options) }
51
+ assert_nothing_raised{ FileRenamer::Commander.new(options, []) }
50
52
 
51
53
  options = { :hardlink => true, }
52
- assert_nothing_raised{ FileRenamer.new(options) }
54
+ assert_nothing_raised{ FileRenamer::Commander.new(options, []) }
53
55
 
54
56
  options = { :symlink => true, }
55
- assert_nothing_raised{ FileRenamer.new(options) }
57
+ assert_nothing_raised{ FileRenamer::Commander.new(options, []) }
56
58
 
57
59
  # ファイル操作モードが空でも OK で、その場合は :move が true として扱われる。
58
60
  options = { }
59
- fr00 = FileRenamer.new(options)
61
+ fr00 = FileRenamer::Commander.new(options, [])
60
62
  assert_equal( {:move => true}, fr00.options )
61
63
 
62
64
  # ファイル操作モードで矛盾する2つ以上が true なら例外。
@@ -64,33 +66,33 @@ class TC_FileRenamer < Test::Unit::TestCase
64
66
  :move => true,
65
67
  :copy => true,
66
68
  }
67
- assert_raise(FileRenamerOptionError){ FileRenamer.new(options) }
69
+ assert_raise(FileRenamer::Commander::OptionError){ FileRenamer::Commander.new(options, []) }
68
70
 
69
71
  options = {
70
72
  :move => true,
71
73
  :hardlink => true,
72
74
  }
73
- assert_raise(FileRenamerOptionError){ FileRenamer.new(options) }
75
+ assert_raise(FileRenamer::Commander::OptionError){ FileRenamer::Commander.new(options, []) }
74
76
 
75
77
  options = {
76
78
  :move => true,
77
79
  :symlink => true,
78
80
  }
79
- assert_raise(FileRenamerOptionError){ FileRenamer.new(options) }
81
+ assert_raise(FileRenamer::Commander::OptionError){ FileRenamer::Commander.new(options, []) }
80
82
 
81
83
  # :quiet が立てられれば、自動的に :yes も true になる。
82
84
  options = {:quiet => true}
83
- fr00 = FileRenamer.new(options)
85
+ fr00 = FileRenamer::Commander.new(options, [])
84
86
  assert_equal( {:quiet => true, :yes => true, :move => true}, fr00.options )
85
87
 
86
88
  # :quiet が立てられれば、:yes が明示的に立っていても問題なし。
87
89
  options = {:quiet => true, :yes => true}
88
- fr00 = FileRenamer.new(options)
90
+ fr00 = FileRenamer::Commander.new(options, [])
89
91
  assert_equal( {:quiet => true, :yes => true, :move => true}, fr00.options )
90
92
 
91
93
  # :quiet が立てられれば、:no が立っていれば例外。
92
94
  options = {:quiet => true, :no => true}
93
- assert_raise(FileRenamerOptionError){ FileRenamer.new(options) }
95
+ assert_raise(FileRenamer::Commander::OptionError){ FileRenamer::Commander.new(options, []) }
94
96
  end
95
97
 
96
98
  def test_execute
@@ -99,8 +101,8 @@ class TC_FileRenamer < Test::Unit::TestCase
99
101
  FileUtils.rm(A_1) if FileTest.exist?(A_1)
100
102
  #
101
103
  options = {:copy => true, :yes => true}
102
- fr01 = FileRenamer.new(options)
103
- str = capture_stdout{fr01.execute([A_0]){ A_1 }}
104
+ fr01 = FileRenamer::Commander.new(options, [A_0])
105
+ str = capture_stdout{fr01.execute{ A_1 }}
104
106
  assert_equal(true, FileTest.exist?(A_0))
105
107
  assert_equal(true, FileTest.exist?(A_1))
106
108
  FileUtils.rm(A_1)
@@ -121,8 +123,8 @@ class TC_FileRenamer < Test::Unit::TestCase
121
123
  FileUtils.rm(A_1) if FileTest.exist?(A_1)
122
124
  #
123
125
  options = {:copy => true, :no => true}
124
- fr01 = FileRenamer.new(options)
125
- str = capture_stdout{ fr01.execute([A_0]){ A_1 }}
126
+ fr01 = FileRenamer::Commander.new(options, [A_0])
127
+ str = capture_stdout{ fr01.execute{ A_1 }}
126
128
  assert_equal(true , FileTest.exist?(A_0))
127
129
  assert_equal(false, FileTest.exist?(A_1))
128
130
  #
@@ -140,8 +142,8 @@ class TC_FileRenamer < Test::Unit::TestCase
140
142
  io = StringIO.new
141
143
  #
142
144
  options = {:copy => true, :quiet => true}
143
- fr01 = FileRenamer.new(options)
144
- str = capture_stdout{fr01.execute([A_0]){ A_1 }}
145
+ fr01 = FileRenamer::Commander.new(options, [A_0])
146
+ str = capture_stdout{fr01.execute{ A_1 }}
145
147
  assert_equal(true, FileTest.exist?(A_0))
146
148
  assert_equal(true, FileTest.exist?(A_1))
147
149
  FileUtils.rm(A_1)
@@ -156,8 +158,8 @@ class TC_FileRenamer < Test::Unit::TestCase
156
158
  output = StringIO.new
157
159
  #
158
160
  options = {:copy => true}
159
- fr01 = FileRenamer.new(options)
160
- str = capture_stdout{fr01.execute([A_0]){ A_1 }}
161
+ fr01 = FileRenamer::Commander.new(options, [A_0])
162
+ str = capture_stdout{fr01.execute{ A_1 }}
161
163
  assert_equal(true, FileTest.exist?(A_0))
162
164
  assert_equal(true, FileTest.exist?(A_1))
163
165
  FileUtils.rm(A_1)
@@ -184,8 +186,8 @@ class TC_FileRenamer < Test::Unit::TestCase
184
186
  output = StringIO.new
185
187
  #
186
188
  options = {:copy => true}
187
- fr01 = FileRenamer.new(options)
188
- str = capture_stdout{fr01.execute([A_1]){ A_0 }}
189
+ fr01 = FileRenamer::Commander.new(options, [A_1])
190
+ str = capture_stdout{fr01.execute{ A_0 }}
189
191
  assert_equal(true , FileTest.exist?(A_0))
190
192
  assert_equal(false, FileTest.exist?(A_1))
191
193
  #
@@ -207,7 +209,7 @@ class TC_FileRenamer < Test::Unit::TestCase
207
209
  #FileUtils.rm(A_1) if FileTest.exist?(A_1)
208
210
  ##
209
211
  #options = {:copy => true, :no => true}
210
- #fr01 = FileRenamer.new(options)
212
+ #fr01 = FileRenamer::Commander.new(options)
211
213
  #str = capture_stdout{ fr01.execute([]){|name| name + ".00"}}
212
214
  ##
213
215
  #correct =
@@ -225,12 +227,15 @@ class TC_FileRenamer < Test::Unit::TestCase
225
227
  end
226
228
 
227
229
  def test_make_new_names
230
+ options = {}
231
+ #pp [A_0, A_1]; exit
232
+ fr00 = FileRenamer::Commander.new(options, [A_0, A_1])
228
233
  assert_equal(
229
234
  {
230
235
  A_0 => A_0 + "00",
231
236
  A_1 => A_1 + "00",
232
237
  },
233
- @fr00.make_new_names([A_0, A_1]){ |old|
238
+ fr00.make_new_names{ |old|
234
239
  old.sub(/$/, "00")
235
240
  }
236
241
  )
@@ -312,47 +317,6 @@ class TC_FileRenamer < Test::Unit::TestCase
312
317
  end
313
318
 
314
319
  def test_run
315
- # # ディレクトリ管理不要の場合
316
- # FileUtils.rm(A_1) if FileTest.exist?(A_1)
317
- # #
318
- # @fr01.run(A_0, A_1)
319
- # io = capture_stdout{ @fr01.run(A_0, A_1) }
320
- # io.rewind
321
- # assert_equal([" cp -r #{A_0} #{A_1}\n"], io.readlines)
322
- # #
323
- # assert_equal(true, FileTest.exist?(A_0))
324
- # assert_equal(true, FileTest.exist?(A_1))
325
- # #
326
- # # あとかたづけ
327
- # FileUtils.rm(A_1)
328
- #
329
- # # ディレクトリ生成が必要な場合
330
- # FileUtils.rm("tmp/#{A_1}") if FileTest.exist?("tmp/#{A_1}")
331
- # FileUtils.rmdir("tmp/test/FileRenamer") if FileTest.exist?("tmp/test/FileRenamer")
332
- # FileUtils.rmdir("tmp/test") if FileTest.exist?("tmp/test")
333
- # FileUtils.rmdir("tmp") if FileTest.exist?("tmp")
334
- # #
335
- # io = capture_stdout{ @fr01.run(A_0, "tmp/"+A_1) }
336
- # io.rewind
337
- # t = io.readlines
338
- # assert_equal(" make directory: tmp\n", t[0])
339
- # assert_equal(" make directory: tmp/test\n", t[1])
340
- # assert_equal(" make directory: tmp/test/FileRenamer\n", t[2])
341
- # assert_equal(" cp -r #{A_0} tmp/#{A_1}\n", t[3])
342
- # assert_equal(4, t.size)
343
- # #
344
- # assert_equal(true, FileTest.exist?(A_0))
345
- # assert_equal(true, FileTest.exist?("tmp/#{A_1}"))
346
- # assert_equal(true, FileTest.exist?("tmp/test/FileRenamer"))
347
- # assert_equal(true, FileTest.exist?("tmp/test" ))
348
- # assert_equal(true, FileTest.exist?("tmp" ))
349
- # #
350
- # # あとかたづけ
351
- # FileUtils.rm("tmp/#{A_1}")
352
- # FileUtils.rmdir("tmp/test/FileRenamer")
353
- # FileUtils.rmdir("tmp/test" )
354
- # FileUtils.rmdir("tmp" )
355
-
356
320
  # ディレクトリ消去が必要な場合
357
321
  FileUtils.rm("tmp1/dir/a1.txt") if FileTest.exist?("tmp1/dir/a1.txt")
358
322
  FileUtils.rm("tmp2/dir/a1.txt") if FileTest.exist?("tmp2/dir/a1.txt")
@@ -0,0 +1,53 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require "test/unit"
5
+ require "filerenamer/optionparser.rb"
6
+
7
+ class TC_OptionParser < Test::Unit::TestCase
8
+ #def setup
9
+ # @frop00 = FileRenamerOptionParser.new
10
+ #end
11
+
12
+ def test_sanity_check
13
+ frop01 = FileRenamer::OptionParser.new
14
+ ary = %w(-y --copy)
15
+ frop01.parse(ary)
16
+ assert_equal( { :yes => true, :copy => true }, frop01.options)
17
+
18
+ # このクラスではオプションの整合性チェックは行わない。
19
+ frop01 = FileRenamer::OptionParser.new
20
+ ary = %w(-y -n)
21
+ frop01.parse(ary)
22
+ assert_equal( { :yes => true, :no => true }, frop01.options)
23
+
24
+ frop01 = FileRenamer::OptionParser.new
25
+ ary = %w(-y -n -c -m -h -s -q)
26
+ frop01.parse(ary)
27
+ corrects = {
28
+ :yes => true,
29
+ :no => true,
30
+ :copy => true,
31
+ :move => true,
32
+ :hardlink => true,
33
+ :symlink => true,
34
+ :quiet => true
35
+ }
36
+ assert_equal(corrects, frop01.options)
37
+
38
+ frop01 = FileRenamer::OptionParser.new
39
+ ary = %w(--yes --no --copy --move --hardlink --symlink --quiet)
40
+ frop01.parse(ary)
41
+ corrects = {
42
+ :yes => true,
43
+ :no => true,
44
+ :copy => true,
45
+ :move => true,
46
+ :hardlink => true,
47
+ :symlink => true,
48
+ :quiet => true
49
+ }
50
+ assert_equal(corrects, frop01.options)
51
+ end
52
+ end
53
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filerenamer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-04 00:00:00.000000000 Z
12
+ date: 2012-08-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdoc
16
- requirement: &81806920 !ruby/object:Gem::Requirement
16
+ requirement: &75168420 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.12'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *81806920
24
+ version_requirements: *75168420
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &81806550 !ruby/object:Gem::Requirement
27
+ requirement: &75167750 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.1.4
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *81806550
35
+ version_requirements: *75167750
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: jeweler
38
- requirement: &81805950 !ruby/object:Gem::Requirement
38
+ requirement: &75167430 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.8.3
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *81805950
46
+ version_requirements: *75167430
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: simplecov
49
- requirement: &81805270 !ruby/object:Gem::Requirement
49
+ requirement: &75167080 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *81805270
57
+ version_requirements: *75167080
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: builtinextension
60
- requirement: &81804560 !ruby/object:Gem::Requirement
60
+ requirement: &75166720 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 0.0.5
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *81804560
68
+ version_requirements: *75166720
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: capture_stdout
71
- requirement: &81804050 !ruby/object:Gem::Requirement
71
+ requirement: &75166430 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,13 +76,14 @@ dependencies:
76
76
  version: '0.0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *81804050
79
+ version_requirements: *75166430
80
80
  description: This library provide common dealing to rename many files with safe method.
81
81
  Automatically mkdir if need and rmdir when empty.
82
82
  email: ippei94da@gmail.com
83
83
  executables:
84
84
  - classify1stchar
85
85
  - rennum
86
+ - renpad
86
87
  - renpar
87
88
  - renreg
88
89
  - rensub
@@ -100,22 +101,49 @@ files:
100
101
  - VERSION
101
102
  - bin/classify1stchar
102
103
  - bin/rennum
104
+ - bin/renpad
103
105
  - bin/renpar
104
106
  - bin/renreg
105
107
  - bin/rensub
106
108
  - filerenamer.gemspec
107
109
  - lib/filerenamer.rb
108
- - lib/filerenamer/filerenamer.rb
109
- - lib/filerenamer/filerenameroptionparser.rb
110
+ - lib/filerenamer/commander.rb
111
+ - lib/filerenamer/optionparser.rb
112
+ - test/classify1stchar/123
113
+ - test/classify1stchar/abc
114
+ - test/classify1stchar/def
110
115
  - test/filerenamer/a0.txt
111
116
  - test/filerenamer/dummy.txt
112
117
  - test/helper.rb
118
+ - test/rennum/a
119
+ - test/rennum/b
120
+ - test/rennum/c
121
+ - test/rennum/d
122
+ - test/renpad/1
123
+ - test/renpad/10
124
+ - test/renpad/100
125
+ - test/renpad/100b
126
+ - test/renpad/10b
127
+ - test/renpad/1b
128
+ - test/renpad/a1
129
+ - test/renpad/a10
130
+ - test/renpad/a100
131
+ - test/renpad/a100b
132
+ - test/renpad/a10b
133
+ - test/renpad/a1b
113
134
  - test/renpar/((ab)(cd(ef))gh(ij)kl(mn).txt
114
135
  - test/renpar/(ab)(cd(ef)))gh(ij)kl(mn).txt
115
136
  - test/renpar/(ab)(cd(ef))gh(ij)kl(mn).txt
116
137
  - test/renpar/(ab)(cd).txt
117
- - test/test_filerenamer.rb
118
- - test/test_filerenameroptionparser.rb
138
+ - test/renreg/a(b)
139
+ - test/renreg/1
140
+ - test/renreg/a
141
+ - test/rensub/a0
142
+ - test/rensub/a1
143
+ - test/rensub/b0
144
+ - test/rensub/b1
145
+ - test/test_commander.rb
146
+ - test/test_optionparser.rb
119
147
  homepage: http://github.com/ippei94da/filerenamer
120
148
  licenses:
121
149
  - MIT
@@ -131,7 +159,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
131
159
  version: '0'
132
160
  segments:
133
161
  - 0
134
- hash: 453747699
162
+ hash: 810196591
135
163
  required_rubygems_version: !ruby/object:Gem::Requirement
136
164
  none: false
137
165
  requirements: