writer 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
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=
2
+ SHA1:
3
+ metadata.gz: ad036d03f4aebf06450cd8089c28c88a9084ca3b
4
+ data.tar.gz: 7fd70f5d6f4a2bbdd7bab8eacf471fcf28721985
5
+ SHA512:
6
+ metadata.gz: eb590424088d7a46cce133dcbfe8973a24872565c1d20a49807281bf7628bbb864c0a37e5a71241ea7d2d95c2fc2bb56aa818c36f22e64c05e2ff59920914430
7
+ data.tar.gz: 91954bd16c4055b43fe69868c121eadeda5977f652f8371c4c55a2ecdb338033a7877a5d00789bc93e41135119b798af0292737ac58ae315d4cda835e53e0e66
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ writer
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.0.0
data/README.md CHANGED
@@ -61,6 +61,20 @@ in the project root
61
61
  template_path: .template
62
62
  ```
63
63
 
64
+ ## Configuration
65
+
66
+ ```yml
67
+ # in config/writer.yml
68
+
69
+ # default config:
70
+ namer: 'Writer::FileNamer'
71
+ creator: 'Writer::FileCreator'
72
+ logger: 'Writer::Logger'
73
+ date_format: '%Y-%m%b-%d'
74
+ log_level: 2 # 0 - err, 1 - warn, 2 - info, 3 - debug
75
+ template_path: ''
76
+ ```
77
+
64
78
  ## Contributing
65
79
 
66
80
  Please provide a spec covering your feature or bug fix, thank you!
data/bin/wr CHANGED
@@ -5,12 +5,4 @@ require 'writer'
5
5
  filename = ARGV[0]
6
6
  content = ARGV[1]
7
7
 
8
- if (load_config = File.open('config/writer.yml') rescue nil)
9
- writer_config = YAML.load(load_config)
10
-
11
- Writer.configure do |config|
12
- config.template_path = writer_config['template_path']
13
- end
14
- end
15
-
16
8
  Writer.write!(filename, content)
data/config/writer.yml ADDED
@@ -0,0 +1 @@
1
+ log_level: 1
@@ -2,28 +2,55 @@ require "active_support/all"
2
2
 
3
3
  module Writer
4
4
  class Configuration
5
- attr_accessor :template_path, :date_format, :namer, :creator
5
+ attr_accessor :template_path, :date_format, :attributes,
6
+ :log_level, :logger, :namer, :creator
6
7
 
7
- def initialize
8
- @namer ||= 'Writer::FileNamer'
9
- @creator ||= 'Writer::FileCreator'
8
+ def initialize(attrs = {})
9
+ attrs = attrs.merge(config)
10
+
11
+ attrs.each do |attr, value|
12
+ self.send("#{attr}=", value)
13
+ end
14
+
15
+ @attributes = attrs
16
+ end
17
+
18
+ def namer=(other)
19
+ @namer = Object.const_get(other)
10
20
  end
11
21
 
12
- def namer
13
- const(@namer)
22
+ def logger=(other)
23
+ @logger = Object.const_get(other)
14
24
  end
15
25
 
16
- def creator
17
- const(@creator)
26
+ def creator=(other)
27
+ @creator = Object.const_get(other)
18
28
  end
19
29
 
20
30
  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
31
+ def config
32
+ conf = default_config
33
+ path = 'config/writer.yml'
34
+
35
+ if File.exist?(path)
36
+ config_file = File.open(path)
37
+ user_config = YAML.load(config_file)
38
+
39
+ conf = conf.merge(user_config)
26
40
  end
41
+
42
+ conf
43
+ end
44
+
45
+ def default_config
46
+ {
47
+ namer: 'Writer::FileNamer',
48
+ creator: 'Writer::FileCreator',
49
+ logger: 'Writer::Logger',
50
+ date_format: '%Y-%m%b-%d',
51
+ log_level: 2, # see Logger for levels
52
+ template_path: ''
53
+ }
27
54
  end
28
55
  end
29
56
  end
@@ -1,24 +1,20 @@
1
- require "writer/file_namer"
2
-
3
1
  module Writer
4
2
  class FileCreator
5
- class << self
6
- def create!(filename, content = nil)
7
- create_file(filename, content)
8
- File.open(filename, 'r')
9
- end
3
+ def create!(filename, content = nil)
4
+ create_file(filename, content)
5
+ File.open(filename, 'r')
6
+ end
10
7
 
11
- private
12
- def create_file(name, content)
13
- File.open(name, 'w') do |f|
14
- f.puts content || template
15
- end
8
+ private
9
+ def create_file(name, content)
10
+ File.open(name, 'w') do |f|
11
+ f.puts content || template
16
12
  end
13
+ end
17
14
 
18
- def template
19
- if Writer.template_path
20
- File.open(Writer.template_path).read
21
- end
15
+ def template
16
+ unless Writer.template_path.blank?
17
+ File.open(Writer.template_path).read
22
18
  end
23
19
  end
24
20
  end
@@ -2,73 +2,73 @@ require "date"
2
2
 
3
3
  module Writer
4
4
  class FileNamer
5
- class << self
6
- def name_for(filename)
7
- filename ||= default_filename
5
+ attr_reader :config
8
6
 
9
- name = fix_standard(filename)
10
- name ||= fix_no_ext(filename)
11
- name ||= prevent_overwrite(filename)
7
+ def initialize
8
+ @config = Writer.config
9
+ end
12
10
 
13
- name
14
- end
11
+ def name_for(filename)
12
+ filename ||= default_filename
15
13
 
16
- def fix_standard(name)
17
- if /.+\..+/ =~ name
18
- parts = name.split('.')
19
- basename = parts[0..-2].join('.')
20
- extension = parts.last
14
+ name = fix_standard(filename)
15
+ name ||= fix_no_ext(filename)
16
+ name ||= prevent_overwrite(filename)
21
17
 
22
- prevent_overwrite(name, basename, extension)
23
- end
24
- end
18
+ name
19
+ end
25
20
 
26
- def fix_no_ext(name)
27
- if /\.$/ =~ name
28
- basename = name.gsub('.', '')
21
+ def fix_standard(name)
22
+ if /.+\..+/ =~ name
23
+ parts = name.split('.')
24
+ basename = parts[0..-2].join('.')
25
+ extension = parts.last
29
26
 
30
- prevent_overwrite(name, basename)
31
- end
27
+ prevent_overwrite(name, basename, extension)
32
28
  end
29
+ end
33
30
 
34
- def prevent_overwrite(name, base = nil, ext = nil)
35
- separator = separator_for(name, base)
36
- to_fix = base || name
37
- count = 1
38
-
39
- while File.exists?(name)
40
- name = append_count(to_fix, separator, count += 1)
41
- name << ".#{ext}" if base
42
- end
31
+ def fix_no_ext(name)
32
+ if /\.$/ =~ name
33
+ basename = name.gsub('.', '')
43
34
 
44
- name
35
+ prevent_overwrite(name, basename)
45
36
  end
37
+ end
46
38
 
47
- def default_filename
48
- date = Date.today
49
- date.strftime("#{date_format}.md")
50
- end
39
+ def prevent_overwrite(name, base = nil, ext = nil)
40
+ separator = separator_for(name, base)
41
+ to_fix = base || name
42
+ count = 1
51
43
 
52
- def separator_for(name, base = nil)
53
- return "." if base || name.include?('.')
54
- "--"
44
+ while File.exists?(name)
45
+ name = append_count(to_fix, separator, count += 1)
46
+ name << ".#{ext}" if base
55
47
  end
56
48
 
57
- def config
58
- @configuration ||= Writer.config
59
- end
49
+ name
50
+ end
60
51
 
61
- private
62
- def append_count(name, separator, count)
63
- basename = name.gsub(/(--|\.)\d*$/, '')
64
- zero_pad = count < 10 ? 0 : nil
52
+ def default_filename
53
+ date = Date.today
54
+ date.strftime("#{date_format}.md")
55
+ end
65
56
 
66
- [basename, separator, zero_pad, count].join
67
- end
57
+ def separator_for(name, base = nil)
58
+ return "." if base || name.include?('.')
59
+ "--"
60
+ end
68
61
 
69
- def date_format
70
- config.date_format || '%Y-%m%b-%d'
71
- end
62
+ private
63
+ def append_count(name, separator, count)
64
+ basename = name.gsub(/(--|\.)\d*$/, '')
65
+ zero_pad = count < 10 ? 0 : nil
66
+
67
+ [basename, separator, zero_pad, count].join
68
+ end
69
+
70
+ def date_format
71
+ config.date_format
72
72
  end
73
73
  end
74
74
  end
@@ -0,0 +1,33 @@
1
+ module Writer
2
+ class Logger
3
+ def info(*args)
4
+ return if log_level < 2
5
+
6
+ out = if args.many?
7
+ o = ["#{args.pop}:"]
8
+ args.each { |a| o << a }
9
+ else
10
+ [args]
11
+ end
12
+
13
+ $stdout.print(out.join(' ') + "\n")
14
+ end
15
+
16
+ class << self
17
+ def config
18
+ @config ||= Writer.config
19
+ end
20
+ end
21
+
22
+ private
23
+ #log_level
24
+ #
25
+ # 0 - error
26
+ # 1 - warn
27
+ # 2 - info
28
+ # 3 - debug
29
+ def log_level
30
+ self.class.config.log_level
31
+ end
32
+ end
33
+ end
@@ -1,3 +1,3 @@
1
1
  module Writer
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/writer.rb CHANGED
@@ -1,34 +1,52 @@
1
- require "writer/configuration"
2
- require "writer/version"
3
-
4
1
  module Writer
2
+ autoload :Version, "writer/version"
3
+ autoload :FileNamer, "writer/file_namer"
4
+ autoload :FileCreator, "writer/file_creator"
5
+ autoload :Logger, "writer/logger"
6
+ autoload :Configuration, "writer/configuration"
7
+
5
8
  class << self
6
9
  def write!(name = nil, content = nil)
7
- name = namer.name_for(name)
8
- creator.create!(name, content)
10
+ name = file_namer.name_for(name)
11
+
12
+ logger.info "Creating #{name}"
13
+
14
+ file_creator.create!(name, content)
9
15
  end
10
16
 
11
- def configure
12
- yield(config)
17
+ def file_namer
18
+ @file_namer ||= namer.new
19
+ end
20
+
21
+ def file_creator
22
+ @file_creator ||= creator.new
13
23
  end
14
24
 
15
- # attempt delegation to config
16
- def method_missing(name, *args)
17
- config.send(name, *args)
25
+ def logger
26
+ @logger ||= config.logger.new
18
27
  end
19
28
 
20
- def config=(other)
21
- @config = other
29
+ def configure
30
+ yield(config)
22
31
  end
23
32
 
24
33
  def config
25
34
  @config ||= Configuration.new
26
35
  end
36
+
37
+ def set_default_config!
38
+ @config = Configuration.new
39
+ end
40
+
41
+ def method_missing(method_name, *args)
42
+ attrs = config.attributes.keys
43
+ return super unless attrs.include?(method_name)
44
+
45
+ config.send(method_name, *args)
46
+ end
27
47
  end
28
48
  end
29
49
 
30
- require "writer/file_creator"
31
-
32
50
  def wr(*args)
33
51
  `wr #{args.join(' ')}`
34
52
  end
data/spec/config_spec.rb CHANGED
@@ -1,15 +1,20 @@
1
1
  require "writer"
2
- class MyCreator; end
3
- class MyNamer; end
2
+ module Creators; class MyCreator; end; end
3
+ module Namers; class MyNamer; end; end;
4
4
 
5
5
  module Writer
6
6
  describe Configuration do
7
7
  after :all do
8
- Writer.config = Configuration.new
8
+ Writer.set_default_config!
9
9
  end
10
10
 
11
- it "defaults template_path to nil" do
12
- Writer.template_path.should be_nil
11
+ it "defaults template_path blank" do
12
+ Writer.template_path.should be_blank
13
+ end
14
+
15
+ it "defaults date_format to american" do
16
+ # and in a format that helps with file sorting
17
+ Writer.date_format.should == '%Y-%m%b-%d'
13
18
  end
14
19
 
15
20
  it "takes a template_path" do
@@ -20,16 +25,24 @@ module Writer
20
25
  Writer.template_path.should == 'hi'
21
26
  end
22
27
 
28
+ it "takes a date_format" do
29
+ Writer.configure do |c|
30
+ c.date_format = 'hello there'
31
+ end
32
+
33
+ Writer.date_format.should == 'hello there'
34
+ end
35
+
23
36
  it "defaults creator to FileCreator" do
24
37
  Writer.creator.should == FileCreator
25
38
  end
26
39
 
27
40
  it "takes a creator" do
28
41
  Writer.configure do |c|
29
- c.creator = :my_creator
42
+ c.creator = 'Creators::MyCreator'
30
43
  end
31
44
 
32
- Writer.creator.should == MyCreator
45
+ Writer.creator.should == Creators::MyCreator
33
46
  end
34
47
 
35
48
  it "defaults namer to FileNamer" do
@@ -38,10 +51,10 @@ module Writer
38
51
 
39
52
  it "takes a namer" do
40
53
  Writer.configure do |c|
41
- c.namer = :my_namer
54
+ c.namer = 'Namers::MyNamer'
42
55
  end
43
56
 
44
- Writer.namer.should == MyNamer
57
+ Writer.namer.should == Namers::MyNamer
45
58
  end
46
59
  end
47
60
  end
data/spec/creator_spec.rb CHANGED
@@ -2,7 +2,8 @@ require "writer/file_creator"
2
2
 
3
3
  module Writer
4
4
  describe FileCreator do
5
- let(:file) { stub(:file) }
5
+ let(:file) { stub(:file) }
6
+ let(:creator) { FileCreator.new }
6
7
 
7
8
  before :each do
8
9
  File.stub(:open)
@@ -17,23 +18,23 @@ module Writer
17
18
  context "with content" do
18
19
  it "writes the content" do
19
20
  file.should_receive(:puts).with('hi')
20
- FileCreator.create!('filename', 'hi')
21
+ creator.create!('filename', 'hi')
21
22
  end
22
23
  end
23
24
 
24
25
  context "without content" do
25
26
  it "leaves a blank line" do
26
- FileCreator.stub(:template) { nil }
27
+ creator.stub(:template) { nil }
27
28
  file.should_receive(:puts).with(nil)
28
- FileCreator.create!('filename')
29
+ creator.create!('filename')
29
30
  end
30
31
 
31
32
  it "uses a template, if it exists" do
32
33
  template = stub(:read => "hello\nworld")
33
- FileCreator.stub(:template) { template.read }
34
+ creator.stub(:template) { template.read }
34
35
 
35
36
  file.should_receive(:puts).with("hello\nworld")
36
- FileCreator.create!('filename')
37
+ creator.create!('filename')
37
38
  end
38
39
  end
39
40
  end
data/spec/namer_spec.rb CHANGED
@@ -2,6 +2,8 @@ require "writer"
2
2
 
3
3
  module Writer
4
4
  describe FileNamer do
5
+ let(:file_namer) { FileNamer.new }
6
+
5
7
  before :each do
6
8
  File.stub(:exists?)
7
9
  .with(/^\D+[^(\-\-)\d]$/)
@@ -16,7 +18,7 @@ module Writer
16
18
  Date.stub(:today) do
17
19
  Date.new(2012, 1, 3)
18
20
  end
19
- name = FileNamer.name_for(nil)
21
+ name = file_namer.name_for(nil)
20
22
  name.should == "2012-01Jan-03.md"
21
23
  end
22
24
 
@@ -29,32 +31,32 @@ module Writer
29
31
  Date.new(2013, 8, 12)
30
32
  end
31
33
 
32
- name = FileNamer.name_for(nil)
34
+ name = file_namer.name_for(nil)
33
35
  name.should == "12-08Aug-2013.md"
34
36
  end
35
37
 
36
38
  it "prevents overwriting" do
37
- name = FileNamer.name_for('hi')
39
+ name = file_namer.name_for('hi')
38
40
  name.should == 'hi--02'
39
41
  end
40
42
 
41
43
  it "plays well with standard filenames" do
42
- name = FileNamer.name_for('std.rb')
44
+ name = file_namer.name_for('std.rb')
43
45
  name.should == 'std.02.rb'
44
46
  end
45
47
 
46
48
  it "plays well with a dot at the end" do
47
- name = FileNamer.name_for('dot.')
49
+ name = file_namer.name_for('dot.')
48
50
  name.should == 'dot.02.'
49
51
  end
50
52
 
51
53
  it "plays well with a dot at the beginning" do
52
- name = FileNamer.name_for('.dot')
54
+ name = file_namer.name_for('.dot')
53
55
  name.should == '.dot.02'
54
56
  end
55
57
 
56
58
  it "plays well with multiple dots" do
57
- name = FileNamer.name_for('some.jquery.file')
59
+ name = file_namer.name_for('some.jquery.file')
58
60
  name.should == 'some.jquery.02.file'
59
61
  end
60
62
  end
data/spec/writer_spec.rb CHANGED
@@ -24,7 +24,7 @@ describe Writer do
24
24
  end
25
25
 
26
26
  def delete_files
27
- Dir.glob("20*.md") do |filename|
27
+ Dir.glob("[^README]*.md") do |filename|
28
28
  File.delete(filename)
29
29
  end
30
30
  end
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: writer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Sak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-13 00:00:00.000000000 Z
11
+ date: 2013-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ! '>='
31
+ - - '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ! '>='
38
+ - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: Easily start a new file to write in.
@@ -48,16 +48,19 @@ extra_rdoc_files: []
48
48
  files:
49
49
  - .gitignore
50
50
  - .rspec
51
- - .rvmrc
51
+ - .ruby-gemset
52
+ - .ruby-version
52
53
  - Gemfile
53
54
  - LICENSE
54
55
  - README.md
55
56
  - Rakefile
56
57
  - bin/wr
58
+ - config/writer.yml
57
59
  - lib/writer.rb
58
60
  - lib/writer/configuration.rb
59
61
  - lib/writer/file_creator.rb
60
62
  - lib/writer/file_namer.rb
63
+ - lib/writer/logger.rb
61
64
  - lib/writer/version.rb
62
65
  - spec/.gitkeep
63
66
  - spec/config_spec.rb
@@ -74,17 +77,17 @@ require_paths:
74
77
  - lib
75
78
  required_ruby_version: !ruby/object:Gem::Requirement
76
79
  requirements:
77
- - - ! '>='
80
+ - - '>='
78
81
  - !ruby/object:Gem::Version
79
82
  version: '0'
80
83
  required_rubygems_version: !ruby/object:Gem::Requirement
81
84
  requirements:
82
- - - ! '>='
85
+ - - '>='
83
86
  - !ruby/object:Gem::Version
84
87
  version: '0'
85
88
  requirements: []
86
89
  rubyforge_project:
87
- rubygems_version: 2.0.5
90
+ rubygems_version: 2.0.6
88
91
  signing_key:
89
92
  specification_version: 4
90
93
  summary: Start a new file and write in it.
data/.rvmrc DELETED
@@ -1 +0,0 @@
1
- rvm use --create 1.9.3@writer