writer 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +12 -0
- data/lib/writer/file_creator.rb +6 -12
- data/lib/writer/overwrite_prevention.rb +49 -13
- data/lib/writer/version.rb +1 -1
- data/spec/writer_spec.rb +36 -6
- metadata +3 -3
data/README.md
CHANGED
@@ -35,6 +35,18 @@ $ wr hello world
|
|
35
35
|
#=> Creates a file named `hello` with the content `world`
|
36
36
|
```
|
37
37
|
|
38
|
+
## Now supports the Ruby interactive shell!
|
39
|
+
|
40
|
+
```
|
41
|
+
$ irb
|
42
|
+
> require 'writer'
|
43
|
+
#=> true
|
44
|
+
> wr 'hello.txt', 'world'
|
45
|
+
#=>""
|
46
|
+
> File.open('hello.txt').read
|
47
|
+
#=> "world\n"
|
48
|
+
```
|
49
|
+
|
38
50
|
## Use a template
|
39
51
|
|
40
52
|
The Writer will copy a template file's contents into your new file
|
data/lib/writer/file_creator.rb
CHANGED
@@ -3,18 +3,16 @@ require "writer/overwrite_prevention"
|
|
3
3
|
module Writer
|
4
4
|
class FileCreator
|
5
5
|
class << self
|
6
|
-
|
6
|
+
def create!(filename, content)
|
7
|
+
name = OverwritePrevention.adjust(filename)
|
8
|
+
content = content
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
@content = content
|
11
|
-
|
12
|
-
create_file
|
13
|
-
return file
|
10
|
+
create_file(name, content)
|
11
|
+
File.open(name, 'r')
|
14
12
|
end
|
15
13
|
|
16
14
|
private
|
17
|
-
def create_file
|
15
|
+
def create_file(name, content)
|
18
16
|
File.open(name, 'w') do |f|
|
19
17
|
f.puts content || template
|
20
18
|
end
|
@@ -25,10 +23,6 @@ module Writer
|
|
25
23
|
File.open(Writer.template_path).read
|
26
24
|
end
|
27
25
|
end
|
28
|
-
|
29
|
-
def file
|
30
|
-
File.open(name, 'r')
|
31
|
-
end
|
32
26
|
end
|
33
27
|
end
|
34
28
|
end
|
@@ -1,25 +1,61 @@
|
|
1
1
|
module Writer
|
2
2
|
class OverwritePrevention
|
3
|
-
|
4
|
-
|
3
|
+
class << self
|
4
|
+
def adjust(filename)
|
5
|
+
name = fix_standard(filename)
|
6
|
+
name ||= fix_no_extension(filename)
|
7
|
+
name ||= fix_no_base(filename)
|
8
|
+
name ||= fix_no_dots(filename)
|
9
|
+
name
|
10
|
+
end
|
11
|
+
|
12
|
+
def fix_standard(name)
|
13
|
+
if /.+\..+/ =~ name
|
14
|
+
parts = name.split('.')
|
15
|
+
basename = parts[0..-2].join('.')
|
16
|
+
extension = parts.last
|
5
17
|
|
6
|
-
|
7
|
-
|
18
|
+
fix_name(name, basename, extension)
|
19
|
+
end
|
8
20
|
end
|
9
21
|
|
10
|
-
name
|
11
|
-
|
22
|
+
def fix_no_extension(name)
|
23
|
+
if /\.$/ =~ name
|
24
|
+
basename = name.gsub('.', '')
|
12
25
|
|
13
|
-
|
14
|
-
|
15
|
-
ext = split.last
|
16
|
-
split.delete(ext)
|
17
|
-
name = split.join('.')
|
26
|
+
fix_name(name, basename)
|
27
|
+
end
|
18
28
|
end
|
19
29
|
|
20
|
-
|
30
|
+
def fix_no_base(name)
|
31
|
+
if /^\./ =~ name
|
32
|
+
fix_name(name)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def fix_no_dots(name)
|
37
|
+
unless name.include?('.')
|
38
|
+
fix_name(name)
|
39
|
+
end
|
40
|
+
end
|
21
41
|
|
22
|
-
|
42
|
+
private
|
43
|
+
def fix_name(name, base = nil, ext = nil)
|
44
|
+
to_fix = base || name
|
45
|
+
count = 1
|
46
|
+
|
47
|
+
while File.exists?(name)
|
48
|
+
name = append_count(to_fix, count += 1)
|
49
|
+
name << ".#{ext}" if base
|
50
|
+
end
|
51
|
+
|
52
|
+
name
|
53
|
+
end
|
54
|
+
|
55
|
+
def append_count(name, count)
|
56
|
+
name = name.gsub(/--\d*$/, '')
|
57
|
+
[name, "--", count].join
|
58
|
+
end
|
23
59
|
end
|
24
60
|
end
|
25
61
|
end
|
data/lib/writer/version.rb
CHANGED
data/spec/writer_spec.rb
CHANGED
@@ -21,20 +21,44 @@ describe Writer do
|
|
21
21
|
file.read.should == "\n"
|
22
22
|
end
|
23
23
|
|
24
|
-
it "
|
25
|
-
Writer.write!
|
26
|
-
|
24
|
+
it "plays well with no dots" do
|
25
|
+
file = Writer.write!('nodots')
|
26
|
+
File.basename(file).should == 'nodots'
|
27
27
|
|
28
|
-
|
29
|
-
File.
|
28
|
+
file = Writer.write!('nodots')
|
29
|
+
File.basename(file).should == 'nodots--2'
|
30
30
|
end
|
31
31
|
|
32
|
-
it "
|
32
|
+
it "plays well with a dot at the end" do
|
33
|
+
file = Writer.write!('dot.')
|
34
|
+
File.basename(file).should == 'dot.'
|
35
|
+
|
36
|
+
file = Writer.write!('dot.')
|
37
|
+
File.basename(file).should == 'dot--2.'
|
38
|
+
end
|
39
|
+
|
40
|
+
it "plays well with a dot at the beginning" do
|
41
|
+
file = Writer.write!('.dot')
|
42
|
+
File.basename(file).should == '.dot'
|
43
|
+
|
44
|
+
file = Writer.write!('.dot')
|
45
|
+
File.basename(file).should == '.dot--2'
|
46
|
+
end
|
47
|
+
|
48
|
+
it "plays well with multiple dots" do
|
33
49
|
Writer.write!('some.jquery.file')
|
34
50
|
file = Writer.write!('some.jquery.file')
|
35
51
|
File.basename(file).should == 'some.jquery--2.file'
|
36
52
|
end
|
37
53
|
|
54
|
+
it "prevents overwriting" do
|
55
|
+
Writer.write!
|
56
|
+
Writer.write!
|
57
|
+
|
58
|
+
File.open('2012-01Jan-03--2.md')
|
59
|
+
File.open('2012-01Jan-03--3.md')
|
60
|
+
end
|
61
|
+
|
38
62
|
it "creates the file with your custom name" do
|
39
63
|
filename = "My custom filename.txt"
|
40
64
|
Writer.write!(filename)
|
@@ -60,8 +84,14 @@ def cleanup_files
|
|
60
84
|
File.delete('2012-01Jan-03--3.md')
|
61
85
|
File.delete('2012-01Jan-03--4.md')
|
62
86
|
File.delete('2012-01Jan-03--5.md')
|
87
|
+
File.delete('nodots')
|
88
|
+
File.delete('nodots--2')
|
89
|
+
File.delete('dot.')
|
90
|
+
File.delete('dot--2.')
|
63
91
|
File.delete('some.jquery.file')
|
64
92
|
File.delete('some.jquery--2.file')
|
65
93
|
File.delete('.template')
|
66
94
|
File.delete('My custom filename.txt')
|
95
|
+
File.delete('.dot')
|
96
|
+
File.delete('.dot--2')
|
67
97
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: writer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-07-15 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70289521172480 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70289521172480
|
25
25
|
description: Easily start a new file to write in.
|
26
26
|
email:
|
27
27
|
- joe@joesak.com
|