Skip to content

Commit

Permalink
Removing code duplication in CLI class.
Browse files Browse the repository at this point in the history
A little metaprogramming never hurt anybody, right?
  • Loading branch information
mmcclimon committed Feb 2, 2014
1 parent 9baa3f7 commit 85bd38b
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions lib/mr_poole/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,40 @@ def execute(action)
end

def handle_post
options = do_creation_options
options.title ||= @params.first

@helper.post_usage unless options.title
fn = @commands.post(options)
puts "#{@src_dir}/#{fn}"
do_create('post')
end

def handle_draft
options = do_creation_options
options.title ||= @params.first

@helper.draft_usage unless options.title
fn = @commands.draft(options)
puts "#{@src_dir}/#{fn}"
do_create('draft')
end

def handle_publish
options = do_move_options(:publish)

path = @params.first
@helper.publish_usage unless path
fn = @commands.publish(path, options)
puts "#{@src_dir}/#{fn}"
do_move('publish')
end

def handle_unpublish
options = do_move_options(:unpublish)
do_move('unpublish')
end

private

# action is a string, either 'post' or 'draft'
def do_create(action)
options = do_creation_options
options.title ||= @params.first

@helper.send("#{action}_usage") unless options.title
fn = @commands.send(action, options)
puts "#{@src_dir}/#{fn}"
end

# action is a string, either 'publish' or 'unpublish'
def do_move(action)
options = do_move_options(action)
path = @params.first
@helper.unpublish_usage unless path
fn = @commands.unpublish(path, options)

@helper.send("#{action}_usage") unless path
fn = @commands.send(action, path, options)
puts "#{@src_dir}/#{fn}"
end

Expand Down Expand Up @@ -91,11 +93,11 @@ def do_creation_options
options
end

# pass a symbol, either :publish or :unpublish
# pass a string, either publish or unpublish
def do_move_options(type)
options = OpenStruct.new
opt_parser = OptionParser.new do |opts|
if type == :publish
if type == 'publish'
opts.on('-d', '--keep-draft', "Keep draft post") do |d|
options.keep_draft = d
end
Expand Down

0 comments on commit 85bd38b

Please sign in to comment.