Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Change the output format of ppl show substantially.

In part this change is motivated by issue #14, in which @storge pointed out that
the ppl show command only displays one of the contact's email addresses. This
gives the impression that only one can be stored per contact, so it needed
changing urgently.

Another reason for this change was that the output of ppl show was on the whole
quite an ugly, disorganised mess. This commit changes the rest of the formatting
somewhat so that hopefully it'll be a little more clear.
  • Loading branch information...
commit c8c54c0de7e82ab6f258fef15f8033cb4f589d76 1 parent 81021fd
@hnrysmth authored
View
4 lib/ppl.rb
@@ -1,7 +1,7 @@
module Ppl
- Version = "1.6.0"
+ Version = "1.7.0"
module Adapter
end
@@ -80,6 +80,8 @@ module Format
require "ppl/format/contact/organization"
require "ppl/format/contact/phone_number"
require "ppl/format/contact/postal_address"
+require "ppl/format/postal_address"
+require "ppl/format/postal_address/one_line"
require "ppl/format/table"
class String
View
48 lib/ppl/format/contact/full.rb
@@ -4,31 +4,28 @@ class Ppl::Format::Contact::Full < Ppl::Format::Contact
attr_writer :postal_address_format
def initialize
- @postal_address_format = Ppl::Format::Contact::PostalAddress.new
+ @postal_address_format = Ppl::Format::PostalAddress::OneLine.new
end
def process(contact)
- lines = []
+ @lines = []
first_line = first_line(contact)
if first_line != ""
- lines.push(first_line)
+ @lines.push(first_line)
end
vitals = vitals(contact)
if vitals != ""
- lines.push("")
- lines.push(vitals)
- lines.push("")
- lines.push("")
+ @lines.push("")
+ @lines.push(vitals)
end
- if !contact.postal_address.nil?
- lines.push("Postal Address:")
- lines.push(@postal_address_format.process(contact))
- end
+ format_email_addresses(contact)
+ format_phone_numbers(contact)
+ format_postal_addresses(contact)
- return lines.join("\n")
+ return @lines.join("\n")
end
private
@@ -49,9 +46,6 @@ def vitals(contact)
if !contact.birthday.nil?
vitals.push(format_vital("Birthday", contact.birthday.strftime("%Y-%m-%d")))
end
- if !contact.phone_number.nil?
- vitals.push(format_vital("Telephone", contact.phone_number))
- end
if !contact.organization.nil?
vitals.push(format_vital("Organization", contact.organization))
end
@@ -62,5 +56,29 @@ def format_vital(name, value)
return sprintf(" %-12s %s", name, value)
end
+ def format_email_addresses(contact)
+ if !contact.email_addresses.empty?
+ @lines.push("")
+ @lines.push("Email Addresses:")
+ contact.email_addresses.each { |email_address| @lines.push(" " + email_address) }
+ end
+ end
+
+ def format_phone_numbers(contact)
+ if !contact.phone_number.nil?
+ @lines.push("")
+ @lines.push("Phone Numbers:")
+ @lines.push(" #{contact.phone_number}")
+ end
+ end
+
+ def format_postal_addresses(contact)
+ if !contact.postal_address.nil?
+ @lines.push("")
+ @lines.push("Postal Address:")
+ @lines.push(" " + @postal_address_format.process(contact.postal_address))
+ end
+ end
+
end
View
9 lib/ppl/format/postal_address.rb
@@ -0,0 +1,9 @@
+
+class Ppl::Format::PostalAddress
+
+ def process(postal_address)
+ raise NotImplementedError
+ end
+
+end
+
View
18 lib/ppl/format/postal_address/one_line.rb
@@ -0,0 +1,18 @@
+
+class Ppl::Format::PostalAddress::OneLine < Ppl::Format::AddressBook
+
+ def process(postal_address)
+ pieces = []
+
+ pieces.push(postal_address.street) unless postal_address.street.nil?
+ pieces.push(postal_address.locality) unless postal_address.locality.nil?
+ pieces.push(postal_address.region) unless postal_address.region.nil?
+ pieces.push(postal_address.country) unless postal_address.country.nil?
+ pieces.push(postal_address.postal_code) unless postal_address.postal_code.nil?
+ pieces.push(postal_address.po_box) unless postal_address.po_box.nil?
+
+ pieces.join(", ")
+ end
+
+end
+
View
4 ppl.gemspec
@@ -2,8 +2,8 @@
Gem::Specification.new do |spec|
spec.name = "ppl"
- spec.version = "1.6.0"
- spec.date = "2012-12-27"
+ spec.version = "1.7.0"
+ spec.date = "2012-12-29"
spec.required_ruby_version = ">= 1.9.3"
View
13 spec/ppl/format/contact/full_spec.rb
@@ -24,7 +24,16 @@
it "should include their email address in brackets" do
@contact.name = "John Doe"
@contact.email_addresses.push "john@example.org"
- @format.process(@contact).should eq "John Doe <john@example.org>"
+ @format.process(@contact).should include "John Doe <john@example.org>"
+ end
+
+ it "should show all their email addresses" do
+ @contact.email_addresses.push "john@example.org"
+ @contact.email_addresses.push "john@example.com"
+ @contact.email_addresses.push "john@example.net"
+ @format.process(@contact).should include "john@example.org"
+ @format.process(@contact).should include "john@example.com"
+ @format.process(@contact).should include "john@example.net"
end
it "should show their birthday if available" do
@@ -44,7 +53,7 @@
it "should show their postal address if available" do
@contact.postal_address = @address
- @postal_address_format.should_receive(:process).with(@contact)
+ @postal_address_format.should_receive(:process).and_return("")
@format.process(@contact)
end
View
44 spec/ppl/format/postal_address/one_line_spec.rb
@@ -0,0 +1,44 @@
+
+describe Ppl::Format::PostalAddress::OneLine do
+
+ before(:each) do
+ @format = Ppl::Format::PostalAddress::OneLine.new
+ @address = Ppl::Entity::PostalAddress.new
+ end
+
+ describe "#process" do
+
+ it "should include the country if available" do
+ @address.country = "UK"
+ @format.process(@address).should include "UK"
+ end
+
+ it "should include the locality if available" do
+ @address.locality = "Liverpool"
+ @format.process(@address).should include "Liverpool"
+ end
+
+ it "should include the street if available" do
+ @address.street = "1 Test Road"
+ @format.process(@address).should include "1 Test Road"
+ end
+
+ it "should include the po box if available" do
+ @address.po_box = "123456"
+ @format.process(@address).should include "123456"
+ end
+
+ it "should include the postal code if available" do
+ @address.postal_code = "L1 9AA"
+ @format.process(@address).should include "L1 9AA"
+ end
+
+ it "should include the region if available" do
+ @address.region = "Merseyside"
+ @format.process(@address).should include "Merseyside"
+ end
+
+ end
+
+end
+
View
18 spec/ppl/format/postal_address_spec.rb
@@ -0,0 +1,18 @@
+
+describe Ppl::Format::PostalAddress do
+
+ before(:each) do
+ @format = Ppl::Format::PostalAddress.new
+ end
+
+ describe "#process" do
+
+ it "should raise a NotImplementedError" do
+ expect{@format.process(nil)}.to raise_error(NotImplementedError)
+ end
+
+ end
+
+end
+
+

1 comment on commit c8c54c0

@storge-xx

#applause

Please sign in to comment.
Something went wrong with that request. Please try again.