writer 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,5 +1,3 @@
1
- 20*.md
2
- *.gem
3
1
  *.rbc
4
2
  .bundle
5
3
  .config
data/bin/wr CHANGED
@@ -3,6 +3,7 @@
3
3
  require 'yaml'
4
4
  require 'writer'
5
5
  filename = ARGV[0]
6
+ content = ARGV[1]
6
7
 
7
8
  if (load_config = File.open('config/writer.yml') rescue nil)
8
9
  config = YAML.load(load_config)
@@ -12,8 +13,4 @@ if (load_config = File.open('config/writer.yml') rescue nil)
12
13
  end
13
14
  end
14
15
 
15
- if filename
16
- Writer.write!(filename)
17
- else
18
- Writer.write!
19
- end
16
+ Writer.write!(filename, content)
@@ -1,3 +1,3 @@
1
1
  module Writer
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/writer.rb CHANGED
@@ -4,8 +4,10 @@ require "writer/version"
4
4
 
5
5
  module Writer
6
6
  class << self
7
- def write!(filename = todays_date_md)
8
- create_file(filename)
7
+ def write!(name = default_filename, content = nil)
8
+ protect_from_overwrite(name)
9
+ create_file(content)
10
+ File.open(filename, 'r')
9
11
  end
10
12
 
11
13
  def configure
@@ -14,13 +16,33 @@ module Writer
14
16
  end
15
17
 
16
18
  private
17
- def create_file(name)
18
- File.open(name, 'w') do |f|
19
- f.puts template
19
+ def protect_from_overwrite(name)
20
+ count = 1
21
+ while File.exists?(name)
22
+ name = append_count(name, count += 1)
20
23
  end
24
+ @filename = name
21
25
  end
22
26
 
23
- def todays_date_md
27
+ def create_file(content = nil)
28
+ File.open(filename, 'w') do |f|
29
+ f.puts content || template
30
+ end
31
+ end
32
+
33
+ def append_count(name, count)
34
+ if (split = name.split('.')).length > 1
35
+ ext = split.last
36
+ split.delete(ext)
37
+ name = split.join
38
+ end
39
+
40
+ name = name.gsub(/--\d$/,'')
41
+
42
+ [name + "--#{count}", '.', ext].join
43
+ end
44
+
45
+ def default_filename
24
46
  date = Date.today
25
47
  date.strftime('%Y-%m%b-%d.md')
26
48
  end
@@ -34,5 +56,9 @@ module Writer
34
56
  def config
35
57
  @config ||= Configuration.new
36
58
  end
59
+
60
+ def filename
61
+ @filename
62
+ end
37
63
  end
38
64
  end
data/spec/writer_spec.rb CHANGED
@@ -1,10 +1,6 @@
1
1
  require 'writer'
2
2
 
3
3
  describe Writer do
4
- let(:file) do
5
- File.open('2012-01Jan-03.md')
6
- end
7
-
8
4
  before :each do
9
5
  Date.stub(:today) do
10
6
  Date.new(2012, 1, 3)
@@ -12,16 +8,22 @@ describe Writer do
12
8
  end
13
9
 
14
10
  after :all do
15
- File.delete('2012-01Jan-03.md')
16
- File.delete('.template')
17
- File.delete('My custom filename.txt')
11
+ cleanup_files
18
12
  end
19
13
 
20
14
  it "creates today's file, blank" do
21
- Writer.write!
15
+ file = Writer.write!
22
16
  file.read.should == "\n"
23
17
  end
24
18
 
19
+ it "prevents overwriting" do
20
+ Writer.write!
21
+ Writer.write!
22
+
23
+ File.open('2012-01Jan-03--2.md')
24
+ File.open('2012-01Jan-03--3.md')
25
+ end
26
+
25
27
  it "creates the file with your custom name" do
26
28
  filename = "My custom filename.txt"
27
29
  Writer.write!(filename)
@@ -30,16 +32,22 @@ describe Writer do
30
32
 
31
33
  it "uses a template, if it exists" do
32
34
  body = "hello\nworld"
33
-
34
- File.open('.template', 'w') do |f|
35
- f.puts body
36
- end
35
+ Writer.write!('.template', body)
37
36
 
38
37
  Writer.configure do |c|
39
38
  c.template_path = '.template'
40
39
  end
41
40
 
42
- Writer.write!
41
+ file = Writer.write!
43
42
  file.read.should == body + "\n"
44
43
  end
45
44
  end
45
+
46
+ def cleanup_files
47
+ File.delete('2012-01Jan-03.md')
48
+ File.delete('2012-01Jan-03--2.md')
49
+ File.delete('2012-01Jan-03--3.md')
50
+ File.delete('2012-01Jan-03--4.md')
51
+ File.delete('.template')
52
+ File.delete('My custom filename.txt')
53
+ 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.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-07-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70193602201320 !ruby/object:Gem::Requirement
16
+ requirement: &70104983209060 !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: *70193602201320
24
+ version_requirements: *70104983209060
25
25
  description: Easily start a new file to write in.
26
26
  email:
27
27
  - joe@joesak.com