nrename 0.0.1 → 0.0.2

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.
@@ -84,3 +84,36 @@ Feature: Renaming files
84
84
  And the following files should exist inside directory "bar":
85
85
  | 0033.txt |
86
86
  | 1234.txt |
87
+
88
+ Scenario: Renaming files with bare numbers
89
+ Given a directory named "dir"
90
+ And the following empty files inside directory "dir":
91
+ | bb1.txt |
92
+ | bb10.txt |
93
+ | cc0023.txt |
94
+ When I cd to "dir"
95
+ When I run `nrename -XN`
96
+ Then the exit status should be 0
97
+ And the following files should exist:
98
+ | 01.txt |
99
+ | 10.txt |
100
+ | 23.txt |
101
+ And the following files should not exist:
102
+ | bb1.txt |
103
+ | bb1o.txt |
104
+ | cc0023.txt |
105
+
106
+
107
+ Scenario: Using custom regular expression
108
+ Given a directory named "dir"
109
+ And the following empty files inside directory "dir":
110
+ | 003_1.txt |
111
+ | 003_10.txt |
112
+ | 003_0023.txt |
113
+ When I cd to "dir"
114
+ When I run `nrename -X --regexp '^003_(\d+)\.txt'`
115
+ Then the exit status should be 0
116
+ And the following files should exist:
117
+ | 003_01.txt |
118
+ | 003_10.txt |
119
+ | 003_23.txt |
@@ -1,4 +1,5 @@
1
1
  require 'aruba/cucumber'
2
+ require 'fileutils'
2
3
 
3
4
  ENV['PATH'] = "#{File.expand_path __FILE__, '../../../bin'}#{File::PATH_SEPARATOR}#{ENV['PATH']}"
4
5
 
@@ -17,3 +18,7 @@ Then(/^the following files should exist inside directory "([^"]*)":$/) do |direc
17
18
  check_file_presence ["#{directory}/#{file_row[0]}"], true
18
19
  end
19
20
  end
21
+
22
+ After do
23
+ FileUtils.rm_rf File.expand_path('../../../tmp', __FILE__)
24
+ end
@@ -6,15 +6,12 @@ module Nrename
6
6
  def initialize(dir)
7
7
  @dir = Pathname.new dir
8
8
 
9
- unless @dir.absolute?
10
- fail ArgumentError, 'directory path should be absolute.'
11
- end
12
-
13
9
  opts = Nrename.options
14
10
 
15
11
  @verbose = opts.verbose
16
12
  @execute = opts.execute
17
13
  @pattern = opts.pattern
14
+ @numbers_only = opts.numbers_only
18
15
  end
19
16
 
20
17
  def numbered_files
@@ -45,7 +42,13 @@ module Nrename
45
42
  def normalized_name_for(path)
46
43
  dirname, filename = path.split
47
44
  new_filename = filename.to_s
48
- new_filename[@pattern, 1] = adjusted_number_string_for(path)
45
+ if @numbers_only
46
+ name = adjusted_number_string_for path
47
+ extname = filename.extname
48
+ new_filename = "#{name}#{extname}"
49
+ else
50
+ new_filename[@pattern, 1] = adjusted_number_string_for(path)
51
+ end
49
52
  dirname + new_filename
50
53
  end
51
54
 
@@ -5,11 +5,12 @@ module Nrename
5
5
  module Options
6
6
  def self.parse(args)
7
7
  default_options = {
8
- :dirs => [],
9
- :execute => false,
10
- :pattern => /(\d+)/,
11
- :recursive => false,
12
- :verbose => true
8
+ :numbers_only => false,
9
+ :dirs => [],
10
+ :execute => false,
11
+ :pattern => /(\d+)/,
12
+ :recursive => false,
13
+ :verbose => true
13
14
  }
14
15
 
15
16
  options = OpenStruct.new default_options
@@ -37,6 +38,16 @@ module Nrename
37
38
  options.recursive = rec
38
39
  end
39
40
 
41
+ opts.on '-N', '--numbers-only',
42
+ 'Leave only numbers in file name' do |n|
43
+ options.numbers_only = n
44
+ end
45
+
46
+ opts.on '--regexp REGEXP', Regexp,
47
+ 'Use REGEXP to match filenames' do |regexp|
48
+ options.pattern = regexp
49
+ end
50
+
40
51
  opts.on '-v', '--[no-]verbose', 'Run verbosely' do |v|
41
52
  options.verbose = v
42
53
  end
@@ -1,3 +1,3 @@
1
1
  module Nrename
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -85,7 +85,7 @@ describe Nrename::Directory do
85
85
 
86
86
  describe '#normalize' do
87
87
  before do
88
- Nrename.options.stub execute: true
88
+ Nrename.options.stub execute: true, verbose: false
89
89
  end
90
90
 
91
91
  it 'renames files so than they can be sorted properly' do
@@ -93,9 +93,6 @@ describe Nrename::Directory do
93
93
  touch %w[1 2 3 10 11 004 005 006]
94
94
  end
95
95
 
96
- # turn off verbose output:
97
- Nrename.options.stub verbose: false
98
-
99
96
  dir = Nrename::Directory.new test_dir
100
97
  dir.normalize
101
98
 
@@ -108,7 +105,16 @@ describe Nrename::Directory do
108
105
  describe '#normalized_name_for' do
109
106
  it 'returns normalized name for file' do
110
107
  dir = Nrename::Directory.new test_dir
111
- file = Pathname.new(test_dir) + '1.txt'
108
+ file = Pathname.new(test_dir) + 'b1.txt'
109
+ dir.stub num_field_length: 4
110
+ new_name = dir.normalized_name_for(file).basename.to_s
111
+ new_name.should be == 'b0001.txt'
112
+ end
113
+
114
+ it 'returns bare number if numbers_only options is provided' do
115
+ Nrename.options.stub numbers_only: true
116
+ dir = Nrename::Directory.new test_dir
117
+ file = Pathname.new(test_dir) + 'b1.txt'
112
118
  dir.stub num_field_length: 4
113
119
  new_name = dir.normalized_name_for(file).basename.to_s
114
120
  new_name.should be == '0001.txt'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nrename
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &81769620 !ruby/object:Gem::Requirement
16
+ requirement: &74275200 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *81769620
24
+ version_requirements: *74275200
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: aruba
27
- requirement: &81769410 !ruby/object:Gem::Requirement
27
+ requirement: &74274990 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *81769410
35
+ version_requirements: *74274990
36
36
  description: ''
37
37
  email:
38
38
  - vderyagin@gmail.com