Initial commit
This commit is contained in:
53
app/models/address.rb
Normal file
53
app/models/address.rb
Normal file
@@ -0,0 +1,53 @@
|
||||
class Address
|
||||
attr_accessor :street1, :street2, :city, :state, :zip, :country
|
||||
|
||||
def initialize(street1, street2, city, state, zip, country)
|
||||
@street1 = street1
|
||||
@street2 = street2
|
||||
@city = city
|
||||
@state = state
|
||||
@zip = zip
|
||||
@country = country
|
||||
end
|
||||
|
||||
def to_s(format: nil)
|
||||
case format
|
||||
when :full
|
||||
join_with_newline(
|
||||
street1,
|
||||
street2,
|
||||
join_with_comma(city, state_and_zip),
|
||||
country
|
||||
)
|
||||
when :condensed
|
||||
join_with_newline(
|
||||
join_with_comma(street1, street2),
|
||||
join_with_comma(city, state_and_zip, country),
|
||||
)
|
||||
else
|
||||
join_with_comma(street1, street2, city, state_and_zip, country)
|
||||
end
|
||||
end
|
||||
|
||||
def present?
|
||||
[street1, street2, city, state, zip, country].any?(&:present?)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def join_without_blanks(*parts, separator: ", ")
|
||||
parts.reject(&:blank?).join(separator)
|
||||
end
|
||||
|
||||
def join_with_comma(*parts)
|
||||
join_without_blanks(*parts)
|
||||
end
|
||||
|
||||
def join_with_newline(*parts)
|
||||
join_without_blanks(*parts, separator: "\n")
|
||||
end
|
||||
|
||||
def state_and_zip
|
||||
join_without_blanks(state, zip, separator: " ")
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user