spwn 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/spwn.rb +8 -5
- data/lib/spwn/version.rb +1 -1
- data/spec/spwn_spec.rb +11 -10
- metadata +1 -1
data/lib/spwn.rb
CHANGED
@@ -35,9 +35,7 @@ class Spwn
|
|
35
35
|
options = args.last.is_a?(Hash) ? args.last : {}
|
36
36
|
return spawn_multiples( :dir, *args, &block ) unless options[:count].nil?
|
37
37
|
name = args.first.is_a?(String) ? args.first : 'dir'
|
38
|
-
|
39
|
-
digit = @dirs.empty? ? '1' : next_digit( @dirs.last )
|
40
|
-
path = File.join(Dir.pwd, "#{name}#{digit}")
|
38
|
+
path = File.join(Dir.pwd, next_dirname(name))
|
41
39
|
Dir.mkdir(path)
|
42
40
|
@dirs << new_dir = Dir.new(path)
|
43
41
|
if block_given?
|
@@ -86,10 +84,15 @@ class Spwn
|
|
86
84
|
items
|
87
85
|
end
|
88
86
|
def self.next_digit(item)
|
89
|
-
File.basename(item).match(/\d+/)
|
87
|
+
match = File.basename(item).match(/\d+/)
|
88
|
+
if match.nil? then '2' else match[0].next end
|
89
|
+
end
|
90
|
+
def self.next_dirname(name)
|
91
|
+
digit = @dirs.empty? ? '' : next_digit( @dirs.last )
|
92
|
+
"#{name}#{digit}"
|
90
93
|
end
|
91
94
|
def self.next_filename(name, ext)
|
92
|
-
digit = @files.empty? ? '
|
95
|
+
digit = @files.empty? ? '' : next_digit( @files.last )
|
93
96
|
"#{name}#{digit}.#{ext}"
|
94
97
|
end
|
95
98
|
def self.split_filename(fullname)
|
data/lib/spwn/version.rb
CHANGED
data/spec/spwn_spec.rb
CHANGED
@@ -14,7 +14,7 @@ describe Spwn do
|
|
14
14
|
Spwn.extensions = old_exts
|
15
15
|
end
|
16
16
|
|
17
|
-
context '
|
17
|
+
context 'file' do
|
18
18
|
it "should be instance of File" do
|
19
19
|
Spwn.file.should be_an_instance_of File
|
20
20
|
end
|
@@ -26,21 +26,21 @@ describe Spwn do
|
|
26
26
|
Spwn.files.should include Spwn.file
|
27
27
|
end
|
28
28
|
it "should have a common file name" do
|
29
|
-
File.basename(Spwn.file).should match /\Afile\d
|
29
|
+
File.basename(Spwn.file).should match /\Afile\d*/
|
30
30
|
end
|
31
31
|
it "should increment the file name" do
|
32
|
-
pattern = /\Afile(\d+)(?:.\w*)?/
|
32
|
+
pattern = /\Afile(\d+)?(?:.\w*)?/
|
33
33
|
Spwn.file
|
34
34
|
expect {
|
35
35
|
3.times { Spwn.file }
|
36
36
|
}.to change {
|
37
37
|
# the digit in the filename for the last spawned file
|
38
|
-
File.basename(Spwn.files.last).match(pattern)[1]
|
39
|
-
}.from(
|
38
|
+
File.basename(Spwn.files.last).match(pattern)[1]
|
39
|
+
}.from(nil).to('4')
|
40
40
|
end
|
41
41
|
it "should allow you to define a name" do
|
42
42
|
file = Spwn.file('candy', ext: 'html')
|
43
|
-
File.basename(file).should == '
|
43
|
+
File.basename(file).should == 'candy.html'
|
44
44
|
end
|
45
45
|
it "should use a random extension" do
|
46
46
|
ext = File.extname(Spwn.file).delete '.'
|
@@ -75,17 +75,18 @@ describe Spwn do
|
|
75
75
|
Spwn.dirs.should include dir
|
76
76
|
end
|
77
77
|
it "should have a common dir name" do
|
78
|
-
File.basename(dir).should match /\Adir\d
|
78
|
+
File.basename(dir).should match /\Adir\d*/
|
79
79
|
end
|
80
80
|
it "should increment the dir name" do
|
81
|
-
pattern = /\Adir(\d+)
|
81
|
+
pattern = /\Adir(\d+)?/
|
82
|
+
puts Spwn.dirs
|
82
83
|
Spwn.dir
|
83
84
|
expect {
|
84
85
|
3.times { Spwn.dir }
|
85
86
|
}.to change {
|
86
87
|
# the digit in the filename for the last spawned file
|
87
|
-
File.basename(Spwn.dirs.last).match(pattern)[1]
|
88
|
-
}.from(
|
88
|
+
File.basename(Spwn.dirs.last).match(pattern)[1]
|
89
|
+
}.from(nil).to('4')
|
89
90
|
end
|
90
91
|
it "should cd into the dir if a block is given" do
|
91
92
|
Spwn.dir do |directory|
|