ppl 1.25.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/features/email.feature +10 -0
  3. data/features/ls.feature +15 -0
  4. data/features/post.feature +24 -0
  5. data/features/step_definitions/ppl_steps.rb +17 -0
  6. data/lib/ppl/adapter/vcard/greencard.rb +49 -19
  7. data/lib/ppl/application/bootstrap.rb +13 -3
  8. data/lib/ppl/application/shell.rb +2 -0
  9. data/lib/ppl/command/post.rb +60 -16
  10. data/lib/ppl/entity/contact.rb +10 -4
  11. data/lib/ppl/entity/postal_address.rb +6 -0
  12. data/lib/ppl/error/postal_address_not_found.rb +4 -0
  13. data/lib/ppl/format/address_book/postal_addresses.rb +5 -18
  14. data/lib/ppl/format/contact/full.rb +5 -6
  15. data/lib/ppl/format/contact/postal_addresses.rb +26 -0
  16. data/lib/ppl/format/custom/contact.rb +8 -12
  17. data/lib/ppl/format/postal_address/multi_line.rb +16 -0
  18. data/lib/ppl/format/postal_address/one_line.rb +21 -9
  19. data/lib/ppl/service/postal_address.rb +65 -0
  20. data/lib/ppl.rb +5 -1
  21. data/ppl.gemspec +4 -4
  22. data/spec/ppl/adapter/vcard/greencard_spec.rb +151 -98
  23. data/spec/ppl/application/bootstrap_spec.rb +14 -2
  24. data/spec/ppl/application/shell_spec.rb +8 -0
  25. data/spec/ppl/command/post_spec.rb +83 -61
  26. data/spec/ppl/entity/contact_spec.rb +40 -10
  27. data/spec/ppl/entity/postal_address_spec.rb +6 -0
  28. data/spec/ppl/format/address_book/postal_addresses_spec.rb +6 -14
  29. data/spec/ppl/format/contact/full_spec.rb +1 -1
  30. data/spec/ppl/format/contact/postal_addresses_spec.rb +36 -0
  31. data/spec/ppl/format/postal_address/multi_line_spec.rb +57 -0
  32. data/spec/ppl/format/postal_address/one_line_spec.rb +32 -18
  33. data/spec/ppl/service/postal_address_spec.rb +152 -0
  34. metadata +13 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3438ed54b9c4ab282ccc9a9389511777516fee7a
4
- data.tar.gz: 63d86817042ebf3a768294d394bfc3c77ff60a01
3
+ metadata.gz: ae020954e5c508fd1ea28a2bfb193fbb6393252d
4
+ data.tar.gz: 9d5b69c1e111f93bf92451dfcb4bc448de214393
5
5
  SHA512:
6
- metadata.gz: c789b838676b6d9cd50f0bcd6c0060fdd87287ed3f0dd6088c11ba5a9cd995e0306eb488bc40e2f81fe9c3c7ad91a5206f6cc586cae9f93c09d21f783ff1fec6
7
- data.tar.gz: f48891ad62cc516d9a69a73ccfe84080b086017d8d307683e4934563786d18163f7e2b5c93ee17b2fbefae67c5485598cefbf8846fb001a5756b9f594da22f55
6
+ metadata.gz: 1fae903da342d62e76058b006313c3bad706a4faa509fb3792177bc5eb0e2803c2993f11d491f820503dceea8b1b1bd6905e4183aaeaef8f8d5809bdc03427fe
7
+ data.tar.gz: ad1fd41a18934622b4d78f6f59c79b13513f0242c2f50ae33fb68bc7d50909233fd19ab5fa037594b03270831cfe16b172c7429a9501c9594d28df5f9f7c7bd5
@@ -8,6 +8,16 @@ Feature: ppl email
8
8
  And bob should have 1 email address
9
9
  And the 1st email address should be "* bob@example.org"
10
10
 
11
+ Scenario: Custom output with %a
12
+ Given I am in the same address book as before
13
+ Then running "ppl email bob --format '%a'" should output 1 line
14
+ And the 1st line should be "bob@example.org"
15
+
16
+ Scenario: Custom output with %f
17
+ Given I am in the same address book as before
18
+ Then running "ppl email bob --format '%f'" should output 1 line
19
+ And the 1st line should be "*"
20
+
11
21
  Scenario: Add an email address to a contact
12
22
  Given I am in an address book with a blank contact called bob
13
23
  And I run "ppl email bob bob@example.org"
data/features/ls.feature CHANGED
@@ -8,3 +8,18 @@ Feature: ppl ls
8
8
  Then running "ppl ls" should output 1 line
9
9
  And the 1st line should be "bob: Robert Testing <bob@example.org>"
10
10
 
11
+ Scenario: Custom output with %i
12
+ Given I am in the same address book as before
13
+ Then running "ppl ls --format '%i'" should output 1 line
14
+ And the 1st line should be "bob"
15
+
16
+ Scenario: Custom output with %N
17
+ Given I am in the same address book as before
18
+ Then running "ppl ls --format '%N'" should output 1 line
19
+ And the 1st line should be "Robert Testing"
20
+
21
+ Scenario: Custom output with %e
22
+ Given I am in the same address book as before
23
+ Then running "ppl ls --format '%e'" should output 1 line
24
+ And the 1st line should be "bob@example.org"
25
+
@@ -0,0 +1,24 @@
1
+
2
+ Feature: ppl post
3
+
4
+ Scenario: Add a home postal address to a contact
5
+ Given I am in an address book with a blank contact called bob
6
+ And I run "ppl post bob home --street '1234 Testing Avenue' --postal-code 'AB1 2CD'"
7
+ Then it should succeed
8
+ And bob should have 1 postal address
9
+ And the 1st postal address should be "home 1234 Testing Avenue, AB1 2CD"
10
+
11
+ Scenario: Add a work postal address to a contact
12
+ Given I am in the same address book as before
13
+ And I run "ppl post bob work --street '987 Working Road' --postal-code 'EF3 4GH'"
14
+ Then it should succeed
15
+ And bob should have 2 postal addresses
16
+ And the 2nd postal address should be " work 987 Working Road, EF3 4GH"
17
+
18
+ Scenario: Mark an existing postal address as preferred
19
+ Given I am in the same address book as before
20
+ And I run "ppl post bob work --preferred"
21
+ Then it should succeed
22
+ And bob should have 2 postal addresses
23
+ And "work" should be the preferred postal address
24
+
@@ -46,6 +46,11 @@ Then /^(bob) should have (\d+) phone numbers?$/ do |name, number|
46
46
  @phone_numbers.length.should eq number.to_i
47
47
  end
48
48
 
49
+ Then /^(bob) should have (\d+) postal addresse?s?$/ do |name, number|
50
+ @postal_addresses = ppl("post #{name}").split("\n")
51
+ @postal_addresses.length.should eq number.to_i
52
+ end
53
+
49
54
  Then /^(bob) should have (\d+) URLs?$/ do |name, number|
50
55
  @urls = ppl("url #{name}").split("\n")
51
56
  @urls.length.should eq number.to_i
@@ -84,6 +89,10 @@ Then(/^the (\d+).. phone number should be "([^"]+)"$/) do |nth, phone_number|
84
89
  @phone_numbers[nth.to_i - 1].should eq phone_number
85
90
  end
86
91
 
92
+ Then(/^the (\d+).. postal address should be "([^"]+)"$/) do |nth, address|
93
+ @postal_addresses[nth.to_i - 1].should eq address
94
+ end
95
+
87
96
  Then(/^the (\d+).. URL should be "([^"]+)"$/) do |nth, url|
88
97
  @urls[nth.to_i - 1].should eq url
89
98
  end
@@ -106,6 +115,14 @@ And /^"([^"]+)" should be the preferred phone number$/ do |phone_number|
106
115
  end
107
116
  end
108
117
 
118
+ And /^"([^"]+)" should be the preferred postal address$/ do |postal_address|
119
+ @postal_addresses.each do |line|
120
+ if line.include? "*"
121
+ line.should include "* #{postal_address}"
122
+ end
123
+ end
124
+ end
125
+
109
126
  And /^there should be no favourite email address$/ do
110
127
  @email_addresses.each do |line|
111
128
  line.should_not include "*"
@@ -1,5 +1,6 @@
1
1
 
2
2
  require "greencard/vcard"
3
+ require "digest/sha1"
3
4
 
4
5
  class Ppl::Adapter::Vcard::GreenCard
5
6
 
@@ -10,6 +11,7 @@ class Ppl::Adapter::Vcard::GreenCard
10
11
  :country => :country,
11
12
  :region => :region,
12
13
  :locality => :locality,
14
+ :preferred => :preferred,
13
15
  }
14
16
 
15
17
  def encode(contact)
@@ -20,7 +22,7 @@ class Ppl::Adapter::Vcard::GreenCard
20
22
  encode_phone_numbers(contact, maker)
21
23
  encode_nicknames(contact, maker)
22
24
  encode_organizations(contact, maker)
23
- encode_postal_address(contact, maker)
25
+ encode_postal_addresses(contact, maker)
24
26
  encode_urls(contact, maker)
25
27
  end
26
28
  vcard.to_s
@@ -32,7 +34,7 @@ class Ppl::Adapter::Vcard::GreenCard
32
34
  decode_birthday(vcard, contact)
33
35
  decode_email_addresses(vcard, contact)
34
36
  decode_phone_numbers(vcard, contact)
35
- decode_postal_address(vcard, contact)
37
+ decode_postal_addresses(vcard, contact)
36
38
  decode_nicknames(vcard, contact)
37
39
  decode_organizations(vcard, contact)
38
40
  decode_name(vcard, contact)
@@ -79,15 +81,20 @@ class Ppl::Adapter::Vcard::GreenCard
79
81
  end
80
82
  end
81
83
 
82
- def encode_postal_address(contact, vcard_maker)
83
- if !contact.postal_address.nil?
84
- vcard_maker.add_addr do |address|
85
- @@postal_address_property_map.each_pair do |vpim_name, ppl_name|
86
- value = contact.postal_address.send(ppl_name)
87
- if !value.nil?
88
- address.send("#{vpim_name.to_s}=", value)
89
- end
90
- end
84
+ def encode_postal_addresses(contact, vcard_maker)
85
+ contact.postal_addresses.each do |postal_address|
86
+ vcard_maker.add_addr do |vcard_address|
87
+ encode_postal_address(postal_address, vcard_address)
88
+ end
89
+ end
90
+ end
91
+
92
+ def encode_postal_address(ppl_address, vcard_address)
93
+ vcard_address.location = ppl_address.id
94
+ @@postal_address_property_map.each_pair do |vpim_name, ppl_name|
95
+ value = ppl_address.send(ppl_name)
96
+ if !value.nil?
97
+ vcard_address.send("#{vpim_name.to_s}=", value)
91
98
  end
92
99
  end
93
100
  end
@@ -126,15 +133,38 @@ class Ppl::Adapter::Vcard::GreenCard
126
133
  end
127
134
  end
128
135
 
129
- def decode_postal_address(vcard, contact)
130
- if !vcard.address.nil?
131
- contact.postal_address = Ppl::Entity::PostalAddress.new
132
- @@postal_address_property_map.each_pair do |vpim_name, ppl_name|
133
- value = vcard.address.send(vpim_name)
134
- method = "#{ppl_name.to_s}="
135
- contact.postal_address.send(method, value)
136
- end
136
+ def decode_postal_addresses(vcard, contact)
137
+ vcard.addresses.each do |vcard_address|
138
+ postal_address = decode_postal_address(vcard_address)
139
+ contact.postal_addresses << postal_address
140
+ end
141
+ end
142
+
143
+ def decode_postal_address(vcard_address)
144
+ postal_address = Ppl::Entity::PostalAddress.new
145
+ postal_address.id = determine_postal_address_id(vcard_address)
146
+ @@postal_address_property_map.each_pair do |vpim_name, ppl_name|
147
+ value = vcard_address.send(vpim_name)
148
+ method = "#{ppl_name.to_s}="
149
+ postal_address.send(method, value)
150
+ end
151
+ postal_address
152
+ end
153
+
154
+ def determine_postal_address_id(vcard_address)
155
+ id = (vcard_address.location | vcard_address.nonstandard).join
156
+ if id == ""
157
+ id = Digest::SHA1.hexdigest([
158
+ vcard_address.country,
159
+ vcard_address.delivery,
160
+ vcard_address.locality,
161
+ vcard_address.pobox,
162
+ vcard_address.postalcode,
163
+ vcard_address.region,
164
+ vcard_address.street,
165
+ ].join)[0..6]
137
166
  end
167
+ id
138
168
  end
139
169
 
140
170
  def decode_phone_numbers(vcard, contact)
@@ -114,8 +114,10 @@ class Ppl::Application::Bootstrap
114
114
  register :command_post do
115
115
  post = Ppl::Command::Post.new
116
116
  post.storage = storage_adapter
117
- post.list_format = format_address_book_postal_addresses
118
- post.show_format = format_contact_postal_addresses
117
+ post.address_book_format = format_address_book_postal_addresses
118
+ post.contact_format = format_contact_postal_addresses
119
+ post.postal_address_format = format_postal_address_multi_line
120
+ post.address_service = postal_address_service
119
121
  post
120
122
  end
121
123
 
@@ -318,7 +320,7 @@ class Ppl::Application::Bootstrap
318
320
  end
319
321
 
320
322
  register :format_contact_postal_addresses do
321
- Ppl::Format::Contact::PostalAddress.new
323
+ Ppl::Format::Contact::PostalAddresses.new
322
324
  end
323
325
 
324
326
  register :format_contact_urls do
@@ -326,6 +328,10 @@ class Ppl::Application::Bootstrap
326
328
  Ppl::Format::Contact::Urls.new(colors)
327
329
  end
328
330
 
331
+ register :format_postal_address_multi_line do
332
+ Ppl::Format::PostalAddress::MultiLine.new
333
+ end
334
+
329
335
  register :input do
330
336
  Ppl::Application::Input.new(ARGV.dup)
331
337
  end
@@ -377,5 +383,9 @@ class Ppl::Application::Bootstrap
377
383
  phone_service
378
384
  end
379
385
 
386
+ register :postal_address_service do
387
+ Ppl::Service::PostalAddress.new
388
+ end
389
+
380
390
  end
381
391
 
@@ -15,6 +15,8 @@ class Ppl::Application::Shell
15
15
  output.error("ppl: No completion function available for '#{$!}'")
16
16
  rescue Ppl::Error::ContactNotFound
17
17
  output.error("ppl: Contact '#{$!}' not found")
18
+ rescue Ppl::Error::PostalAddressNotFound
19
+ output.error("ppl: Postal address '#{$!}' not found")
18
20
  rescue OptionParser::InvalidOption, OptionParser::MissingArgument, Ppl::Error::IncorrectUsage
19
21
  output.error($!)
20
22
  output.error(@optparse.to_s)
@@ -4,8 +4,10 @@ class Ppl::Command::Post < Ppl::Application::Command
4
4
  name "post"
5
5
  description "List, show or change postal addresses"
6
6
 
7
- attr_writer :show_format
8
- attr_writer :list_format
7
+ attr_writer :address_service
8
+ attr_writer :contact_format
9
+ attr_writer :address_book_format
10
+ attr_writer :postal_address_format
9
11
 
10
12
  def options(parser, options)
11
13
  parser.banner = "usage: ppl post <contact> [address]"
@@ -29,6 +31,20 @@ class Ppl::Command::Post < Ppl::Application::Command
29
31
  options[:country] = country
30
32
  end
31
33
 
34
+ parser.on("-d", "--delete") do
35
+ options[:delete] = true
36
+ end
37
+ parser.on("-m", "--move <new-id>") do |new_id|
38
+ options[:new_id] = new_id
39
+ end
40
+
41
+ parser.on("-p", "--preferred", "mark address as preferred") do
42
+ options[:preferred] = true
43
+ end
44
+ parser.on("-P", "--not-preferred", "mark address as not preferred") do
45
+ options[:preferred] = false
46
+ end
47
+
32
48
  end
33
49
 
34
50
  def execute(input, output)
@@ -40,24 +56,35 @@ class Ppl::Command::Post < Ppl::Application::Command
40
56
  private
41
57
 
42
58
  def determine_action(input)
43
- if input.arguments[0].nil?
44
- :list_postal_addresses
59
+ if input.arguments.length < 1
60
+ :list_address_book_postal_addresses
61
+ elsif input.arguments.length < 2
62
+ :show_contact_postal_addresses
63
+ elsif input.options[:delete]
64
+ :delete_postal_address
45
65
  elsif input.options.empty?
46
66
  :show_postal_address
67
+ elsif existing_address?(input)
68
+ :update_postal_address
47
69
  else
48
- :set_postal_address
70
+ :create_postal_address
49
71
  end
50
72
  end
51
73
 
52
- def list_postal_addresses(input, output)
74
+ def existing_address?(input)
75
+ @contact = @storage.require_contact(input.arguments[0])
76
+ !@contact.postal_addresses.find { |p| p.id == input.arguments[1] }.nil?
77
+ end
78
+
79
+ def list_address_book_postal_addresses(input, output)
53
80
  address_book = @storage.load_address_book
54
- address_list = @list_format.process(address_book)
81
+ address_list = @address_book_format.process(address_book)
55
82
  output.line(address_list)
56
83
  end
57
84
 
58
- def show_postal_address(input, output)
85
+ def show_contact_postal_addresses(input, output)
59
86
  contact = @storage.require_contact(input.arguments[0])
60
- address = @show_format.process(contact)
87
+ address = @contact_format.process(contact)
61
88
  if address != ""
62
89
  output.line(address)
63
90
  true
@@ -66,17 +93,34 @@ class Ppl::Command::Post < Ppl::Application::Command
66
93
  end
67
94
  end
68
95
 
69
- def set_postal_address(input, output)
96
+ def show_postal_address(input, output)
70
97
  contact = @storage.require_contact(input.arguments[0])
71
-
72
- [:country, :locality, :region, :po_box, :postal_code, :street].each do |property|
73
- next if input.options[property].nil?
74
- contact.set_postal_address do |address|
75
- address.send("#{property.to_s}=", input.options[property])
76
- end
98
+ address = contact.postal_addresses.find { |pa| pa.id == input.arguments[1] }
99
+ if address.nil?
100
+ raise Ppl::Error::PostalAddressNotFound, input.arguments[1]
77
101
  end
102
+ display = @postal_address_format.process(address)
103
+ output.line(display)
104
+ true
105
+ end
78
106
 
107
+ def delete_postal_address(input, output)
108
+ contact = @storage.require_contact(input.arguments[0])
109
+ @address_service.remove(contact, input.arguments[1])
79
110
  @storage.save_contact(contact)
111
+ true
112
+ end
113
+
114
+ def update_postal_address(input, output)
115
+ @address_service.update(@contact, input.arguments[1], input.options)
116
+ @storage.save_contact(@contact)
117
+ true
118
+ end
119
+
120
+ def create_postal_address(input, output)
121
+ @address_service.add(@contact, input.arguments[1], input.options)
122
+ @storage.save_contact(@contact)
123
+ true
80
124
  end
81
125
 
82
126
  end
@@ -9,6 +9,7 @@ class Ppl::Entity::Contact
9
9
  attr_accessor :phone_numbers
10
10
  attr_accessor :organizations
11
11
  attr_accessor :postal_address
12
+ attr_accessor :postal_addresses
12
13
  attr_accessor :urls
13
14
 
14
15
  def initialize
@@ -16,6 +17,7 @@ class Ppl::Entity::Contact
16
17
  @nicknames = []
17
18
  @organizations = []
18
19
  @phone_numbers = []
20
+ @postal_addresses = []
19
21
  @urls = []
20
22
  end
21
23
 
@@ -26,10 +28,6 @@ class Ppl::Entity::Contact
26
28
  yield @postal_address
27
29
  end
28
30
 
29
- def has_email_address?(email_address)
30
- @email_addresses.include? email_address
31
- end
32
-
33
31
  def age(on_date)
34
32
  if @birthday.nil?
35
33
  nil
@@ -39,5 +37,13 @@ class Ppl::Entity::Contact
39
37
  end
40
38
  end
41
39
 
40
+ def preferred_email_address
41
+ @email_addresses.find { |e| e.preferred }
42
+ end
43
+
44
+ def preferred_phone_number
45
+ @phone_numbers.find { |p| p.preferred }
46
+ end
47
+
42
48
  end
43
49
 
@@ -1,6 +1,8 @@
1
1
 
2
2
  class Ppl::Entity::PostalAddress
3
3
 
4
+ attr_accessor :id
5
+ attr_accessor :preferred
4
6
  attr_accessor :country
5
7
  attr_accessor :locality
6
8
  attr_accessor :street
@@ -8,5 +10,9 @@ class Ppl::Entity::PostalAddress
8
10
  attr_accessor :postal_code
9
11
  attr_accessor :region
10
12
 
13
+ def initialize
14
+ @preferred = false
15
+ end
16
+
11
17
  end
12
18
 
@@ -0,0 +1,4 @@
1
+
2
+ class Ppl::Error::PostalAddressNotFound < StandardError
3
+ end
4
+
@@ -4,7 +4,7 @@ class Ppl::Format::AddressBook::PostalAddresses < Ppl::Format::AddressBook
4
4
  attr_writer :table
5
5
 
6
6
  def initialize(colors={})
7
- @table = Ppl::Format::Table.new([:id, :postal_address], colors)
7
+ @table = Ppl::Format::Table.new([:id, :address_ids], colors)
8
8
  end
9
9
 
10
10
  def process(address_book)
@@ -12,30 +12,17 @@ class Ppl::Format::AddressBook::PostalAddresses < Ppl::Format::AddressBook
12
12
  @table.to_s
13
13
  end
14
14
 
15
-
16
15
  private
17
16
 
18
17
  def add_row(contact)
19
- postal_address = nil
20
- if !contact.postal_address.nil?
21
- postal_address = format_postal_address(contact.postal_address)
22
- end
23
18
  @table.add_row({
24
- :id => sprintf("%s:", contact.id),
25
- :postal_address => postal_address,
19
+ :id => sprintf("%s:", contact.id),
20
+ :address_ids => format_address_ids(contact.postal_addresses),
26
21
  })
27
22
  end
28
23
 
29
- def format_postal_address(postal_address)
30
- pieces = [
31
- postal_address.street,
32
- postal_address.locality,
33
- postal_address.region,
34
- postal_address.country,
35
- postal_address.postal_code,
36
- postal_address.po_box,
37
- ].select { |property| property != "" && !property.nil? }
38
- pieces.join(", ")
24
+ def format_address_ids(postal_addresses)
25
+ postal_addresses.map { |pa| pa.id }.join(", ")
39
26
  end
40
27
 
41
28
  end
@@ -8,7 +8,7 @@ class Ppl::Format::Contact::Full < Ppl::Format::Contact
8
8
  def initialize
9
9
  @email_address_format = Ppl::Format::Contact::EmailAddresses.new
10
10
  @phone_number_format = Ppl::Format::Contact::PhoneNumber.new
11
- @postal_address_format = Ppl::Format::PostalAddress::OneLine.new
11
+ @postal_address_format = Ppl::Format::Contact::PostalAddresses.new
12
12
  end
13
13
 
14
14
  def process(contact)
@@ -90,11 +90,10 @@ class Ppl::Format::Contact::Full < Ppl::Format::Contact
90
90
  end
91
91
 
92
92
  def format_postal_addresses(contact)
93
- if !contact.postal_address.nil?
94
- push_list(
95
- "Postal Address",
96
- @postal_address_format.process(contact.postal_address)
97
- )
93
+ unless contact.postal_addresses.empty?
94
+ @lines << ""
95
+ @lines << "Postal Addresses"
96
+ @lines << @postal_address_format.process(contact)
98
97
  end
99
98
  end
100
99
 
@@ -0,0 +1,26 @@
1
+
2
+ class Ppl::Format::Contact::PostalAddresses < Ppl::Format::Contact
3
+
4
+ attr_writer :table
5
+ attr_writer :postal_address_format
6
+
7
+ def initialize(colors={})
8
+ @table = Ppl::Format::Table.new([:star, :address_id, :address_text], colors)
9
+ @postal_address_format = Ppl::Format::PostalAddress::OneLine.new
10
+ end
11
+
12
+ def process(contact)
13
+ contact.postal_addresses.each do |postal_address|
14
+ format_postal_address(postal_address)
15
+ end
16
+ @table.to_s
17
+ end
18
+
19
+ private
20
+
21
+ def format_postal_address(postal_address)
22
+ @postal_address_format.process(postal_address, @table)
23
+ end
24
+
25
+ end
26
+
@@ -28,12 +28,10 @@ class Ppl::Format::Custom::Contact < Ppl::Format::Custom
28
28
  end
29
29
 
30
30
  format :e do |contact|
31
- preferred = contact.email_addresses.find { |e| e.preferred }
32
- first = contact.email_addresses.first
33
- if !preferred.nil?
34
- preferred.address
35
- elsif !first.nil?
36
- first.address
31
+ if !contact.preferred_email_address.nil?
32
+ contact.preferred_email_address.address
33
+ elsif !contact.email_addresses.first.nil?
34
+ contact.email_addresses.first.address
37
35
  end
38
36
  end
39
37
 
@@ -42,12 +40,10 @@ class Ppl::Format::Custom::Contact < Ppl::Format::Custom
42
40
  end
43
41
 
44
42
  format :p do |contact|
45
- preferred = contact.phone_numbers.find { |p| p.preferred }
46
- first = contact.phone_numbers.first
47
- if !preferred.nil?
48
- preferred.number
49
- elsif !first.nil?
50
- first.number
43
+ if !contact.preferred_phone_number.nil?
44
+ contact.preferred_phone_number.number
45
+ elsif !contact.phone_numbers.first.nil?
46
+ contact.phone_numbers.first.number
51
47
  end
52
48
  end
53
49
 
@@ -0,0 +1,16 @@
1
+
2
+ class Ppl::Format::PostalAddress::MultiLine < Ppl::Format::AddressBook
3
+
4
+ def process(postal_address)
5
+ lines = []
6
+ lines << postal_address.street
7
+ lines << postal_address.locality
8
+ lines << postal_address.region
9
+ lines << postal_address.country
10
+ lines << postal_address.po_box
11
+ lines << postal_address.postal_code
12
+ lines.compact.reject(&:empty?).join "\n"
13
+ end
14
+
15
+ end
16
+
@@ -1,17 +1,29 @@
1
1
 
2
2
  class Ppl::Format::PostalAddress::OneLine < Ppl::Format::AddressBook
3
3
 
4
- def process(postal_address)
5
- pieces = []
4
+ def process(postal_address, table)
5
+ table.add_row({
6
+ :star => format_star(postal_address),
7
+ :address_id => postal_address.id,
8
+ :address_text => format_address_text(postal_address),
9
+ })
10
+ end
11
+
12
+ private
6
13
 
7
- pieces.push(postal_address.street) unless postal_address.street.nil?
8
- pieces.push(postal_address.locality) unless postal_address.locality.nil?
9
- pieces.push(postal_address.region) unless postal_address.region.nil?
10
- pieces.push(postal_address.country) unless postal_address.country.nil?
11
- pieces.push(postal_address.postal_code) unless postal_address.postal_code.nil?
12
- pieces.push(postal_address.po_box) unless postal_address.po_box.nil?
14
+ def format_star(postal_address)
15
+ postal_address.preferred ? "*" : " "
16
+ end
13
17
 
14
- pieces.join(", ")
18
+ def format_address_text(postal_address)
19
+ [
20
+ postal_address.street,
21
+ postal_address.locality,
22
+ postal_address.region,
23
+ postal_address.country,
24
+ postal_address.postal_code,
25
+ postal_address.po_box,
26
+ ].compact.reject(&:empty?).join(", ")
15
27
  end
16
28
 
17
29
  end