Skip to content

Commit

Permalink
Add --delete flag to "ppl email" enabling email addresses to be deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
henrycatalinismith committed Dec 27, 2012
1 parent 4c9bc1c commit 0c7ddb7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/ppl.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

module Ppl

Version = "1.5.3"
Version = "1.6.0"

module Adapter
end
Expand Down
17 changes: 16 additions & 1 deletion lib/ppl/command/email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def initialize

def options(parser, options)
parser.banner = "usage: ppl email <contact> [<email-address>]"

parser.on("-d", "--delete", "delete email address") do
options[:delete] = true
end
end

def execute(input, output)
Expand All @@ -29,6 +33,8 @@ def determine_action(input)
:list_email_addresses
elsif input.arguments[1].nil?
:show_email_addresses
elsif input.options[:delete]
:delete_email_address
else
:set_email_address
end
Expand All @@ -51,10 +57,19 @@ def show_email_addresses(input, output)
end
end

def set_email_address(input, output)
def delete_email_address(input, output)
contact = @storage.require_contact(input.arguments[0])
email_address = input.arguments[1].dup
contact.email_addresses.delete(email_address) do
message = sprintf("%s has no such email address %s", contact.id, email_address)
raise Ppl::Error::IncorrectUsage, message
end
@storage.save_contact(contact)
end

def set_email_address(input, output)
contact = @storage.require_contact(input.arguments[0])
email_address = input.arguments[1].dup
if contact.has_email_address?(email_address)
message = sprintf("%s already has email address %s", contact.id, email_address)
raise Ppl::Error::IncorrectUsage, message
Expand Down
Binary file added ppl-1.6.0.gem
Binary file not shown.
4 changes: 2 additions & 2 deletions ppl.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Gem::Specification.new do |spec|

spec.name = "ppl"
spec.version = "1.5.3"
spec.date = "2012-12-26"
spec.version = "1.6.0"
spec.date = "2012-12-27"

spec.required_ruby_version = ">= 1.9.3"

Expand Down
21 changes: 21 additions & 0 deletions spec/ppl/command/email_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@
@command.execute(@input, @output).should eq false
end

it "should delete the given address to the contact if requested" do
@contact.email_addresses.push("jim@example.org")
@contact.email_addresses.push("jim@example.com")
@storage.should_receive(:require_contact).and_return(@contact)
@storage.should_receive(:save_contact) do |contact|
contact.email_addresses.should eq ["jim@example.com"]
end
@input.arguments = ["jim", "jim@example.org"]
@input.options = { :delete => true }
@command.execute(@input, @output)
end

it "should raise an error when deleting a non-existent email address" do
@contact.email_addresses.push("jim@example.org")
@contact.email_addresses.push("jim@example.com")
@storage.should_receive(:require_contact).and_return(@contact)
@input.arguments = ["jim", "jim@example.net"]
@input.options = { :delete => true }
expect{@command.execute(@input, nil)}.to raise_error(Ppl::Error::IncorrectUsage)
end

it "should add the given address to the contact if it's new" do
@storage.should_receive(:require_contact).and_return(@contact)
@storage.should_receive(:save_contact) do |contact|
Expand Down

0 comments on commit 0c7ddb7

Please sign in to comment.