Skip to content

Commit e59daab

Browse files
committed
Reduce duplication between publish and unpublish commands
1 parent b67e0c7 commit e59daab

File tree

6 files changed

+77
-85
lines changed

6 files changed

+77
-85
lines changed

lib/jekyll-compose.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
require "jekyll-compose/version"
22
require "jekyll-compose/arg_parser"
3+
require "jekyll-compose/movement_arg_parser"
34
require "jekyll-compose/file_creator"
5+
require "jekyll-compose/file_mover"
46
require "jekyll-compose/file_info"
57

68
module Jekyll

lib/jekyll-compose/file_mover.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module Jekyll
2+
module Compose
3+
class FileMover
4+
attr_reader :movement
5+
def initialize(movement)
6+
@movement = movement
7+
end
8+
9+
def resource_type
10+
'file'
11+
end
12+
13+
def move
14+
validate_source
15+
ensure_directory_exists
16+
move_file
17+
end
18+
19+
def validate_source
20+
raise ArgumentError.new("There was no #{resource_type} found at '#{movement.from}'.") unless File.exist? movement.from
21+
end
22+
23+
def ensure_directory_exists
24+
dir = File.dirname movement.to
25+
Dir.mkdir(dir) unless Dir.exist?(dir)
26+
end
27+
28+
def move_file
29+
FileUtils.mv(movement.from, movement.to)
30+
puts "#{resource_type.capitalize} #{movement.from} was moved to #{movement.to}"
31+
end
32+
end
33+
end
34+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Jekyll
2+
module Compose
3+
class MovementArgParser
4+
attr_reader :args, :options
5+
def initialize(args, options)
6+
@args = args
7+
@options = options
8+
end
9+
10+
def validate!
11+
raise ArgumentError.new("You must specify a #{resource_type} path.") if args.empty?
12+
end
13+
14+
def path
15+
args.join ' '
16+
end
17+
end
18+
end
19+
end

lib/jekyll/commands/publish.rb

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,17 @@ def self.process(args = [], options = {})
2626

2727
end
2828

29-
class PublishArgParser
30-
attr_reader :args, :options
31-
def initialize(args, options)
32-
@args = args
33-
@options = options
34-
end
35-
36-
def validate!
37-
raise ArgumentError.new('You must specify a draft path.') if args.empty?
29+
class PublishArgParser < Compose::MovementArgParser
30+
def resource_type
31+
"draft"
3832
end
3933

4034
def date
4135
options["date"].nil? ? Date.today : Date.parse(options["date"])
4236
end
4337

44-
def draft_path
45-
args.join ' '
46-
end
47-
48-
def draft_name
49-
File.basename draft_path
38+
def name
39+
File.basename path
5040
end
5141
end
5242

@@ -57,41 +47,18 @@ def initialize(params)
5747
end
5848

5949
def from
60-
params.draft_path
50+
params.path
6151
end
6252

6353
def to
64-
"_posts/#{_date_stamp}-#{params.draft_name}"
65-
end
66-
67-
def _date_stamp
68-
params.date.strftime '%Y-%m-%d'
54+
date_stamp = params.date.strftime '%Y-%m-%d'
55+
"_posts/#{date_stamp}-#{params.name}"
6956
end
7057
end
7158

72-
class DraftMover
73-
attr_reader :movement
74-
def initialize(movement)
75-
@movement = movement
76-
end
77-
78-
def move
79-
validate_source
80-
ensure_directory_exists
81-
move_file
82-
end
83-
84-
def validate_source
85-
raise ArgumentError.new("There was no draft found at '#{movement.from}'.") unless File.exist? movement.from
86-
end
87-
88-
def ensure_directory_exists
89-
Dir.mkdir("_posts") unless Dir.exist?("_posts")
90-
end
91-
92-
def move_file
93-
FileUtils.mv(movement.from, movement.to)
94-
puts "Draft #{movement.from} was published to #{movement.to}"
59+
class DraftMover < Compose::FileMover
60+
def resource_type
61+
'draft'
9562
end
9663
end
9764
end

lib/jekyll/commands/unpublish.rb

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,13 @@ def self.process(args = [], options = {})
2424

2525
end
2626

27-
class UnpublishArgParser
28-
attr_reader :args, :options
29-
def initialize(args, options)
30-
@args = args
31-
@options = options
27+
class UnpublishArgParser < Compose::MovementArgParser
28+
def resource_type
29+
'post'
3230
end
3331

34-
def validate!
35-
raise ArgumentError.new('You must specify a post path.') if args.empty?
36-
end
37-
38-
def post_path
39-
args.join ' '
40-
end
41-
42-
def post_name
43-
File.basename(post_path).sub /\d{4}-\d{2}-\d{2}-/, ''
32+
def name
33+
File.basename(path).sub /\d{4}-\d{2}-\d{2}-/, ''
4434
end
4535
end
4636

@@ -51,37 +41,17 @@ def initialize(params)
5141
end
5242

5343
def from
54-
params.post_path
44+
params.path
5545
end
5646

5747
def to
58-
"_drafts/#{params.post_name}"
48+
"_drafts/#{params.name}"
5949
end
6050
end
6151

62-
class PostMover
63-
attr_reader :movement
64-
def initialize(movement)
65-
@movement = movement
66-
end
67-
68-
def move
69-
validate_source
70-
ensure_directory_exists
71-
move_file
72-
end
73-
74-
def validate_source
75-
raise ArgumentError.new("There was no post found at '#{movement.from}'.") unless File.exist? movement.from
76-
end
77-
78-
def ensure_directory_exists
79-
Dir.mkdir("_drafts") unless Dir.exist?("_drafts")
80-
end
81-
82-
def move_file
83-
FileUtils.mv(movement.from, movement.to)
84-
puts "Post #{movement.from} was moved to #{movement.to}"
52+
class PostMover < Compose::FileMover
53+
def resource_type
54+
'post'
8555
end
8656
end
8757
end

spec/publish_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
it 'writes a helpful message on success' do
4545
expect(draft_path).to exist
4646
output = capture_stdout { described_class.process(args) }
47-
expect(output).to eql("Draft _drafts/#{draft_to_publish} was published to _posts/#{post_filename}\n")
47+
expect(output).to eql("Draft _drafts/#{draft_to_publish} was moved to _posts/#{post_filename}\n")
4848
end
4949

5050
it 'publishes a draft on the specified date' do

0 commit comments

Comments
 (0)