-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_east_west_address.rb
59 lines (52 loc) · 1.77 KB
/
01_east_west_address.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require 'pry'
# original
class Address
attr_reader :street, :city, :state, :zipcode, :country
def initialize(**args)
@street = args[:street]
@city = args[:city]
@state = args[:state]
@zipcode = args[:zipcode]
@country = args[:country]
end
end
class Person
attr_reader :name, :address
def initialize(name:, address: nil, addr_args: {})
@name = name
@address = address || Address.new(addr_args)
end
# format correctly depending on the info we have
# BAD CODE!!! (we know too much about Address) - Demeter Violation!!
def display_address
''.tap do |string|
string << "#{name}\n"
string << address.street unless address.street&.empty?
string << "\n" unless string.empty?
string << address.city unless address.city&.empty?
string << ", " if !address.city&.empty? && (!address.state&.empty? || !address.zipcode.empty?)
string << address.state unless address.state&.empty?
string << " " unless address.state&.empty? || address.zipcode&.empty?
string << address.zipcode unless address.state&.empty?
string << "\n" unless address.country&.empty?
string << address.country unless address.country&.empty?
end
end
end
usa_params = {
street: '90 High Street',
city: 'Bristol',
state: 'Rhode Island',
zipcode: '02809',
country: 'USA',
}
# what about ch adresses (& old addresses?)
# what about privacy for product reviews & full adress for shipping
address = Address.new(usa_params)
person = Person.new(name: 'Bill Tihen', address: address)
puts person.display_address
puts "*" * 50
puts "*" * 50
# address = Address.new(usa_params)
person = Person.new(name: 'Bill Tihen', addr_args: usa_params )
puts person.display_address