writer 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,10 +1,15 @@
1
1
  # Writer
2
2
 
3
- TODO: Write a gem description
3
+ This gem adds a command line utility that writes a new file for you.
4
+
5
+ ## Doesn't touch do that?
6
+
7
+ Yes, but with this gem you can simply type `wr` and it defaults to
8
+ a markdown file named after today's date.
4
9
 
5
10
  ## Installation
6
11
 
7
- Add this line to your application's Gemfile:
12
+ Create a bundler Gemfile and add this:
8
13
 
9
14
  gem 'writer'
10
15
 
@@ -18,10 +23,36 @@ Or install it yourself as:
18
23
 
19
24
  ## Usage
20
25
 
21
- TODO: Write usage instructions here
26
+ ```
27
+ $ wr
28
+ #=> Creates a MarkDown file named after today's date
29
+ ## e.g. `2012-01Jan-03.md`
30
+
31
+ $ wr hello
32
+ #=> Creates a file named `hello`
33
+
34
+ $ wr hello world
35
+ #=> Creates a file named `hello` with the content `world`
36
+ ```
37
+
38
+ ## Use a template
39
+
40
+ The Writer will copy a template file's contents into your new file
41
+ if you configure the template file's path.
42
+
43
+ Add `config/writer.yml` to the root directory of your project or journal
44
+
45
+ Create a file named whatever you want, the example uses `.template`
46
+ in the project root
47
+
48
+ ```yml
49
+ template_path: .template
50
+ ```
22
51
 
23
52
  ## Contributing
24
53
 
54
+ Please provide a spec covering your feature or bug fix, thank you!
55
+
25
56
  1. Fork it
26
57
  2. Create your feature branch (`git checkout -b my-new-feature`)
27
58
  3. Commit your changes (`git commit -am 'Added some feature'`)
@@ -0,0 +1,34 @@
1
+ require "writer/overwrite_prevention"
2
+
3
+ module Writer
4
+ class FileCreator
5
+ class << self
6
+ attr_accessor :name, :content
7
+
8
+ def create!(name, content)
9
+ @name = OverwritePrevention.adjust(name)
10
+ @content = content
11
+
12
+ create_file
13
+ return file
14
+ end
15
+
16
+ private
17
+ def create_file
18
+ File.open(name, 'w') do |f|
19
+ f.puts content || template
20
+ end
21
+ end
22
+
23
+ def template
24
+ if Writer.template_path
25
+ File.open(Writer.template_path).read
26
+ end
27
+ end
28
+
29
+ def file
30
+ File.open(name, 'r')
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,25 @@
1
+ module Writer
2
+ class OverwritePrevention
3
+ def self.adjust(name)
4
+ count = 1
5
+
6
+ while File.exists?(name)
7
+ name = append_count(name, count +=1)
8
+ end
9
+
10
+ name
11
+ end
12
+
13
+ def self.append_count(name, count)
14
+ if (split = name.split('.')).length > 1
15
+ ext = split.last
16
+ split.delete(ext)
17
+ name = split.join('.')
18
+ end
19
+
20
+ name = name.gsub(/--\d$/,'')
21
+
22
+ [name + "--#{count}", '.', ext].join
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module Writer
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/writer.rb CHANGED
@@ -1,65 +1,34 @@
1
1
  require "date"
2
2
  require "writer/configuration"
3
3
  require "writer/version"
4
+ require "writer/file_creator"
4
5
 
5
6
  module Writer
6
7
  class << self
7
8
  def write!(name = nil, content = nil)
8
- name = default_filename if name.nil?
9
- protect_from_overwrite(name)
10
- create_file(content)
11
- File.open(filename, 'r')
9
+ name ||= default_filename
10
+ FileCreator.create!(name, content)
12
11
  end
13
12
 
14
13
  def configure
15
- @config = Configuration.new
16
- yield(@config)
14
+ yield(config)
17
15
  end
18
16
 
19
- private
20
- def protect_from_overwrite(name)
21
- count = 1
22
- while File.exists?(name)
23
- name = append_count(name, count += 1)
24
- end
25
- @filename = name
26
- end
27
-
28
- def create_file(content = nil)
29
- File.open(filename, 'w') do |f|
30
- f.puts content || template
31
- end
32
- end
33
-
34
- def append_count(name, count)
35
- if (split = name.split('.')).length > 1
36
- ext = split.last
37
- split.delete(ext)
38
- name = split.join
39
- end
40
-
41
- name = name.gsub(/--\d$/,'')
42
-
43
- [name + "--#{count}", '.', ext].join
17
+ # attempt delegation to config
18
+ def method_missing(name, *args)
19
+ return config.send(name, *args)
20
+ rescue
21
+ super
44
22
  end
45
23
 
24
+ private
46
25
  def default_filename
47
26
  date = Date.today
48
27
  date.strftime('%Y-%m%b-%d.md')
49
28
  end
50
29
 
51
- def template
52
- if config.template_path
53
- File.open(config.template_path).read
54
- end
55
- end
56
-
57
30
  def config
58
31
  @config ||= Configuration.new
59
32
  end
60
-
61
- def filename
62
- @filename
63
- end
64
33
  end
65
34
  end
data/spec/writer_spec.rb CHANGED
@@ -29,6 +29,12 @@ describe Writer do
29
29
  File.open('2012-01Jan-03--3.md')
30
30
  end
31
31
 
32
+ it "preserves periods" do
33
+ Writer.write!('some.jquery.file')
34
+ file = Writer.write!('some.jquery.file')
35
+ File.basename(file).should == 'some.jquery--2.file'
36
+ end
37
+
32
38
  it "creates the file with your custom name" do
33
39
  filename = "My custom filename.txt"
34
40
  Writer.write!(filename)
@@ -54,6 +60,8 @@ def cleanup_files
54
60
  File.delete('2012-01Jan-03--3.md')
55
61
  File.delete('2012-01Jan-03--4.md')
56
62
  File.delete('2012-01Jan-03--5.md')
63
+ File.delete('some.jquery.file')
64
+ File.delete('some.jquery--2.file')
57
65
  File.delete('.template')
58
66
  File.delete('My custom filename.txt')
59
67
  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.3
4
+ version: 0.0.4
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-14 00:00:00.000000000 Z
12
+ date: 2012-07-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70224064989180 !ruby/object:Gem::Requirement
16
+ requirement: &70185578605260 !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: *70224064989180
24
+ version_requirements: *70185578605260
25
25
  description: Easily start a new file to write in.
26
26
  email:
27
27
  - joe@joesak.com
@@ -40,6 +40,8 @@ files:
40
40
  - bin/wr
41
41
  - lib/writer.rb
42
42
  - lib/writer/configuration.rb
43
+ - lib/writer/file_creator.rb
44
+ - lib/writer/overwrite_prevention.rb
43
45
  - lib/writer/version.rb
44
46
  - spec/.gitkeep
45
47
  - spec/writer_spec.rb