writer 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MDQ3ZTE5YWEwNzVlZTA4M2NmNzUyY2ExYzkwMWIyZWY4MTU5NDA0NQ==
5
+ data.tar.gz: !binary |-
6
+ NDdhNGJhZDYzNTZlZTY2YmNiNmM1MWM3NzcxMDlhMjRiYzU1NzkxYw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MDUyNTE4ZWM4YjliZTg4OWQxMWZkZWU4N2EwNzVlZThhNDU3NWFiOGM4YTg5
10
+ NDg5MWQ2ZWFkMjQwYTczOWMwYzgyYmU5MmI1OGY5OGI3NTRiZTFlNjFmOWZj
11
+ YjgwYTBhYmIyNDQ3YTA0ZDhjOWZiNGIyMmZmZTFhNGQ5ODRhMDc=
12
+ data.tar.gz: !binary |-
13
+ NzU4NWUyOGRjNTExYjM5MmZlMzc5NWVmM2UwNTA1M2IxNDg4YjBhNWZiMWVi
14
+ ODY3ZWNkNzAzZjM4NjA3NzEzNjZkYTI3NjQ3MWE5ZDJhMDllZTA1OGQzNWFk
15
+ MmZiYmZjYTcyMGNhZjY1YzgyNzdmNTQxNzYxYzI5NDEzODRmOTI=
data/Rakefile CHANGED
@@ -1,2 +1,11 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task :default => :spec
7
+
8
+ RSpec::Core::RakeTask.new(:nested_spec) do |t|
9
+ t.rspec_opts = "--format nested"
10
+ end
11
+ task :nested => :nested_spec
data/bin/wr CHANGED
@@ -6,10 +6,10 @@ filename = ARGV[0]
6
6
  content = ARGV[1]
7
7
 
8
8
  if (load_config = File.open('config/writer.yml') rescue nil)
9
- config = YAML.load(load_config)
9
+ writer_config = YAML.load(load_config)
10
10
 
11
- Writer.configure do |c|
12
- c.template_path = config['template_path']
11
+ Writer.configure do |config|
12
+ config.template_path = writer_config['template_path']
13
13
  end
14
14
  end
15
15
 
@@ -1,5 +1,29 @@
1
+ require "active_support/all"
2
+
1
3
  module Writer
2
4
  class Configuration
3
- attr_accessor :template_path
5
+ attr_accessor :template_path, :date_format, :namer, :creator
6
+
7
+ def initialize
8
+ @namer ||= 'Writer::FileNamer'
9
+ @creator ||= 'Writer::FileCreator'
10
+ end
11
+
12
+ def namer
13
+ const(@namer)
14
+ end
15
+
16
+ def creator
17
+ const(@creator)
18
+ end
19
+
20
+ private
21
+ def const(name)
22
+ if (parts = name.to_s.split('_')).size > 1
23
+ parts.collect(&:capitalize).join.constantize
24
+ else
25
+ name.constantize
26
+ end
27
+ end
4
28
  end
5
29
  end
@@ -3,12 +3,9 @@ require "writer/file_namer"
3
3
  module Writer
4
4
  class FileCreator
5
5
  class << self
6
- def create!(filename, content)
7
- name = FileNamer.name_for(filename)
8
- content = content
9
-
10
- create_file(name, content)
11
- File.open(name, 'r')
6
+ def create!(filename, content = nil)
7
+ create_file(filename, content)
8
+ File.open(filename, 'r')
12
9
  end
13
10
 
14
11
  private
@@ -1,10 +1,15 @@
1
+ require "date"
2
+
1
3
  module Writer
2
4
  class FileNamer
3
5
  class << self
4
6
  def name_for(filename)
5
- name = fix_standard(filename)
7
+ filename ||= default_filename
8
+
9
+ name = fix_standard(filename)
6
10
  name ||= fix_no_ext(filename)
7
11
  name ||= prevent_overwrite(filename)
12
+
8
13
  name
9
14
  end
10
15
 
@@ -39,6 +44,20 @@ module Writer
39
44
  name
40
45
  end
41
46
 
47
+ def default_filename
48
+ date = Date.today
49
+ date.strftime("#{date_format}.md")
50
+ end
51
+
52
+ def separator_for(name, base = nil)
53
+ return "." if base || name.include?('.')
54
+ "--"
55
+ end
56
+
57
+ def config
58
+ @configuration ||= Writer.config
59
+ end
60
+
42
61
  private
43
62
  def append_count(name, separator, count)
44
63
  basename = name.gsub(/(--|\.)\d*$/, '')
@@ -47,12 +66,8 @@ module Writer
47
66
  [basename, separator, zero_pad, count].join
48
67
  end
49
68
 
50
- def separator_for(name, base = nil)
51
- if base || name.include?('.')
52
- "."
53
- else
54
- "--"
55
- end
69
+ def date_format
70
+ config.date_format || '%Y-%m%b-%d'
56
71
  end
57
72
  end
58
73
  end
@@ -1,3 +1,3 @@
1
1
  module Writer
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
data/lib/writer.rb CHANGED
@@ -1,13 +1,11 @@
1
- require "date"
2
1
  require "writer/configuration"
3
2
  require "writer/version"
4
- require "writer/file_creator"
5
3
 
6
4
  module Writer
7
5
  class << self
8
6
  def write!(name = nil, content = nil)
9
- name ||= default_filename
10
- FileCreator.create!(name, content)
7
+ name = namer.name_for(name)
8
+ creator.create!(name, content)
11
9
  end
12
10
 
13
11
  def configure
@@ -16,15 +14,11 @@ module Writer
16
14
 
17
15
  # attempt delegation to config
18
16
  def method_missing(name, *args)
19
- return config.send(name, *args)
20
- rescue
21
- super
17
+ config.send(name, *args)
22
18
  end
23
19
 
24
- private
25
- def default_filename
26
- date = Date.today
27
- date.strftime('%Y-%m%b-%d.md')
20
+ def config=(other)
21
+ @config = other
28
22
  end
29
23
 
30
24
  def config
@@ -33,6 +27,8 @@ module Writer
33
27
  end
34
28
  end
35
29
 
30
+ require "writer/file_creator"
31
+
36
32
  def wr(*args)
37
33
  `wr #{args.join(' ')}`
38
34
  end
@@ -0,0 +1,47 @@
1
+ require "writer"
2
+ class MyCreator; end
3
+ class MyNamer; end
4
+
5
+ module Writer
6
+ describe Configuration do
7
+ after :all do
8
+ Writer.config = Configuration.new
9
+ end
10
+
11
+ it "defaults template_path to nil" do
12
+ Writer.template_path.should be_nil
13
+ end
14
+
15
+ it "takes a template_path" do
16
+ Writer.configure do |c|
17
+ c.template_path = 'hi'
18
+ end
19
+
20
+ Writer.template_path.should == 'hi'
21
+ end
22
+
23
+ it "defaults creator to FileCreator" do
24
+ Writer.creator.should == FileCreator
25
+ end
26
+
27
+ it "takes a creator" do
28
+ Writer.configure do |c|
29
+ c.creator = :my_creator
30
+ end
31
+
32
+ Writer.creator.should == MyCreator
33
+ end
34
+
35
+ it "defaults namer to FileNamer" do
36
+ Writer.namer.should == FileNamer
37
+ end
38
+
39
+ it "takes a namer" do
40
+ Writer.configure do |c|
41
+ c.namer = :my_namer
42
+ end
43
+
44
+ Writer.namer.should == MyNamer
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,40 @@
1
+ require "writer/file_creator"
2
+
3
+ module Writer
4
+ describe FileCreator do
5
+ let(:file) { stub(:file) }
6
+
7
+ before :each do
8
+ File.stub(:open)
9
+ .with('filename', 'w')
10
+ .and_yield(file)
11
+
12
+ File.stub(:open)
13
+ .with('filename', 'r')
14
+ .and_return(true)
15
+ end
16
+
17
+ context "with content" do
18
+ it "writes the content" do
19
+ file.should_receive(:puts).with('hi')
20
+ FileCreator.create!('filename', 'hi')
21
+ end
22
+ end
23
+
24
+ context "without content" do
25
+ it "leaves a blank line" do
26
+ FileCreator.stub(:template) { nil }
27
+ file.should_receive(:puts).with(nil)
28
+ FileCreator.create!('filename')
29
+ end
30
+
31
+ it "uses a template, if it exists" do
32
+ template = stub(:read => "hello\nworld")
33
+ FileCreator.stub(:template) { template.read }
34
+
35
+ file.should_receive(:puts).with("hello\nworld")
36
+ FileCreator.create!('filename')
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,61 @@
1
+ require "writer"
2
+
3
+ module Writer
4
+ describe FileNamer do
5
+ before :each do
6
+ File.stub(:exists?)
7
+ .with(/^\D+[^(\-\-)\d]$/)
8
+ .and_return(true)
9
+
10
+ File.stub(:exists?)
11
+ .with(/^\S*\d+.*$/)
12
+ .and_return(false)
13
+ end
14
+
15
+ it "defaults to today's date" do
16
+ Date.stub(:today) do
17
+ Date.new(2012, 1, 3)
18
+ end
19
+ name = FileNamer.name_for(nil)
20
+ name.should == "2012-01Jan-03.md"
21
+ end
22
+
23
+ it "uses a configured date format" do
24
+ Writer.configure do |config|
25
+ config.date_format = '%e-%m%b-%Y'
26
+ end
27
+
28
+ Date.stub(:today) do
29
+ Date.new(2013, 8, 12)
30
+ end
31
+
32
+ name = FileNamer.name_for(nil)
33
+ name.should == "12-08Aug-2013.md"
34
+ end
35
+
36
+ it "prevents overwriting" do
37
+ name = FileNamer.name_for('hi')
38
+ name.should == 'hi--02'
39
+ end
40
+
41
+ it "plays well with standard filenames" do
42
+ name = FileNamer.name_for('std.rb')
43
+ name.should == 'std.02.rb'
44
+ end
45
+
46
+ it "plays well with a dot at the end" do
47
+ name = FileNamer.name_for('dot.')
48
+ name.should == 'dot.02.'
49
+ end
50
+
51
+ it "plays well with a dot at the beginning" do
52
+ name = FileNamer.name_for('.dot')
53
+ name.should == '.dot.02'
54
+ end
55
+
56
+ it "plays well with multiple dots" do
57
+ name = FileNamer.name_for('some.jquery.file')
58
+ name.should == 'some.jquery.02.file'
59
+ end
60
+ end
61
+ end
data/spec/writer_spec.rb CHANGED
@@ -1,14 +1,8 @@
1
1
  require 'writer'
2
2
 
3
3
  describe Writer do
4
- before :each do
5
- Date.stub(:today) do
6
- Date.new(2012, 1, 3)
7
- end
8
- end
9
-
10
4
  after :all do
11
- cleanup_files
5
+ delete_files
12
6
  end
13
7
 
14
8
  it "creates today's file, blank" do
@@ -21,77 +15,16 @@ describe Writer do
21
15
  file.read.should == "\n"
22
16
  end
23
17
 
24
- it "prevents overwriting" do
25
- 50.times do
26
- Writer.write!
27
- end
28
-
29
- File.open('2012-01Jan-03.02.md')
30
- File.open('2012-01Jan-03.52.md')
31
- end
32
-
33
- it "plays well with no dots" do
34
- file = Writer.write!('nodots')
35
- File.basename(file).should == 'nodots'
36
-
37
- file = Writer.write!('nodots')
38
- File.basename(file).should == 'nodots--02'
39
- end
40
-
41
- it "plays well with a dot at the end" do
42
- file = Writer.write!('dot.')
43
- File.basename(file).should == 'dot.'
44
-
45
- file = Writer.write!('dot.')
46
- File.basename(file).should == 'dot.02.'
47
- end
48
-
49
- it "plays well with a dot at the beginning" do
50
- file = Writer.write!('.dot')
51
- File.basename(file).should == '.dot'
52
-
53
- file = Writer.write!('.dot')
54
- File.basename(file).should == '.dot.02'
55
- end
56
-
57
- it "plays well with multiple dots" do
58
- Writer.write!('some.jquery.file')
59
- file = Writer.write!('some.jquery.file')
60
- File.basename(file).should == 'some.jquery.02.file'
61
- end
62
-
63
- it "creates the file with your custom name" do
18
+ it "uses your custom name" do
64
19
  filename = "My custom filename.txt"
65
- Writer.write!(filename)
66
- File.open(filename)
67
- end
68
-
69
- it "uses a template, if it exists" do
70
- body = "hello\nworld"
71
- Writer.write!('.template', body)
72
-
73
- Writer.configure do |c|
74
- c.template_path = '.template'
75
- end
76
20
 
77
- file = Writer.write!
78
- file.read.should == body + "\n"
21
+ Writer.write!(filename)
22
+ File.delete(filename)
79
23
  end
80
24
  end
81
25
 
82
- def cleanup_files
83
- File.delete('2012-01Jan-03.md')
84
- 52.times do |n|
85
- File.delete("2012-01Jan-03.#{'0' if n < 8}#{n + 2}.md")
26
+ def delete_files
27
+ Dir.glob("20*.md") do |filename|
28
+ File.delete(filename)
86
29
  end
87
- File.delete('nodots')
88
- File.delete('dot.')
89
- File.delete('dot.02.')
90
- File.delete('some.jquery.file')
91
- File.delete('some.jquery.02.file')
92
- File.delete('.template')
93
- File.delete('My custom filename.txt')
94
- File.delete('.dot')
95
- File.delete('.dot.02')
96
- File.delete('nodots--2')
97
30
  end
data/writer.gemspec CHANGED
@@ -15,5 +15,7 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Writer::VERSION
17
17
 
18
+ gem.add_dependency 'activesupport'
19
+
18
20
  gem.add_development_dependency 'rspec'
19
21
  end
metadata CHANGED
@@ -1,27 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: writer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
5
- prerelease:
4
+ version: 0.3.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Joe Sak
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-07-15 00:00:00.000000000 Z
11
+ date: 2013-08-13 00:00:00.000000000 Z
13
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
14
27
  - !ruby/object:Gem::Dependency
15
28
  name: rspec
16
- requirement: &70124302849860 !ruby/object:Gem::Requirement
17
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
18
30
  requirements:
19
31
  - - ! '>='
20
32
  - !ruby/object:Gem::Version
21
33
  version: '0'
22
34
  type: :development
23
35
  prerelease: false
24
- version_requirements: *70124302849860
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
25
41
  description: Easily start a new file to write in.
26
42
  email:
27
43
  - joe@joesak.com
@@ -44,32 +60,37 @@ files:
44
60
  - lib/writer/file_namer.rb
45
61
  - lib/writer/version.rb
46
62
  - spec/.gitkeep
63
+ - spec/config_spec.rb
64
+ - spec/creator_spec.rb
65
+ - spec/namer_spec.rb
47
66
  - spec/writer_spec.rb
48
67
  - writer.gemspec
49
68
  homepage: ''
50
69
  licenses: []
70
+ metadata: {}
51
71
  post_install_message:
52
72
  rdoc_options: []
53
73
  require_paths:
54
74
  - lib
55
75
  required_ruby_version: !ruby/object:Gem::Requirement
56
- none: false
57
76
  requirements:
58
77
  - - ! '>='
59
78
  - !ruby/object:Gem::Version
60
79
  version: '0'
61
80
  required_rubygems_version: !ruby/object:Gem::Requirement
62
- none: false
63
81
  requirements:
64
82
  - - ! '>='
65
83
  - !ruby/object:Gem::Version
66
84
  version: '0'
67
85
  requirements: []
68
86
  rubyforge_project:
69
- rubygems_version: 1.8.15
87
+ rubygems_version: 2.0.5
70
88
  signing_key:
71
- specification_version: 3
89
+ specification_version: 4
72
90
  summary: Start a new file and write in it.
73
91
  test_files:
74
92
  - spec/.gitkeep
93
+ - spec/config_spec.rb
94
+ - spec/creator_spec.rb
95
+ - spec/namer_spec.rb
75
96
  - spec/writer_spec.rb