Class: Worldwide::Address
- Inherits:
-
Object
- Object
- Worldwide::Address
- Defined in:
- lib/worldwide/address.rb
Constant Summary collapse
- COUNTRIES_USING_REVERSE_ADDRESS_ORDER =
["JP"]
Instance Attribute Summary collapse
-
#address1 ⇒ Object
readonly
Returns the value of attribute address1.
-
#address2 ⇒ Object
readonly
Returns the value of attribute address2.
-
#city ⇒ Object
readonly
Returns the value of attribute city.
-
#company ⇒ Object
readonly
Returns the value of attribute company.
-
#country_code ⇒ Object
readonly
Returns the value of attribute country_code.
-
#first_name ⇒ Object
readonly
Returns the value of attribute first_name.
-
#last_name ⇒ Object
readonly
Returns the value of attribute last_name.
-
#phone ⇒ Object
readonly
Returns the value of attribute phone.
-
#province_code ⇒ Object
readonly
Returns the value of attribute province_code.
-
#zip ⇒ Object
readonly
Returns the value of attribute zip.
Instance Method Summary collapse
- #errors ⇒ Object
- #format(additional_lines: [], excluded_fields: []) ⇒ Object
-
#initialize(first_name: nil, last_name: nil, company: nil, address1: nil, address2: nil, zip: nil, city: nil, province_code: nil, country_code: "ZZ", phone: nil) ⇒ Address
constructor
A new instance of Address.
- #normalize(autocorrect_level: 1) ⇒ Object
- #normalize!(autocorrect_level: 1) ⇒ Object
- #single_line(additional_lines: [], excluded_fields: []) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(first_name: nil, last_name: nil, company: nil, address1: nil, address2: nil, zip: nil, city: nil, province_code: nil, country_code: "ZZ", phone: nil) ⇒ Address
Returns a new instance of Address.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/worldwide/address.rb', line 16 def initialize( first_name: nil, last_name: nil, company: nil, address1: nil, address2: nil, zip: nil, city: nil, province_code: nil, country_code: "ZZ", # Unknown region phone: nil ) @first_name = first_name @last_name = last_name @company = company @address1 = address1 @address2 = address2 @zip = zip @city = city @province_code = province_code&.to_s&.upcase @country_code = country_code.to_s.upcase @phone = phone end |
Instance Attribute Details
#address1 ⇒ Object (readonly)
Returns the value of attribute address1.
5 6 7 |
# File 'lib/worldwide/address.rb', line 5 def address1 @address1 end |
#address2 ⇒ Object (readonly)
Returns the value of attribute address2.
5 6 7 |
# File 'lib/worldwide/address.rb', line 5 def address2 @address2 end |
#city ⇒ Object (readonly)
Returns the value of attribute city.
5 6 7 |
# File 'lib/worldwide/address.rb', line 5 def city @city end |
#company ⇒ Object (readonly)
Returns the value of attribute company.
5 6 7 |
# File 'lib/worldwide/address.rb', line 5 def company @company end |
#country_code ⇒ Object (readonly)
Returns the value of attribute country_code.
5 6 7 |
# File 'lib/worldwide/address.rb', line 5 def country_code @country_code end |
#first_name ⇒ Object (readonly)
Returns the value of attribute first_name.
5 6 7 |
# File 'lib/worldwide/address.rb', line 5 def first_name @first_name end |
#last_name ⇒ Object (readonly)
Returns the value of attribute last_name.
5 6 7 |
# File 'lib/worldwide/address.rb', line 5 def last_name @last_name end |
#phone ⇒ Object (readonly)
Returns the value of attribute phone.
5 6 7 |
# File 'lib/worldwide/address.rb', line 5 def phone @phone end |
#province_code ⇒ Object (readonly)
Returns the value of attribute province_code.
5 6 7 |
# File 'lib/worldwide/address.rb', line 5 def province_code @province_code end |
#zip ⇒ Object (readonly)
Returns the value of attribute zip.
5 6 7 |
# File 'lib/worldwide/address.rb', line 5 def zip @zip end |
Instance Method Details
#errors ⇒ Object
40 41 42 |
# File 'lib/worldwide/address.rb', line 40 def errors AddressValidator.errors(self) end |
#format(additional_lines: [], excluded_fields: []) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/worldwide/address.rb', line 44 def format(additional_lines: [], excluded_fields: []) normalized_excluded_fields = normalize_field_names(excluded_fields) lines = build_filled_address_data( additional_lines: additional_lines, excluded_fields: normalized_excluded_fields, ) strip_extra_chars(lines: lines, excluded_fields: normalized_excluded_fields).map do |line| line.join(" ") end end |
#normalize(autocorrect_level: 1) ⇒ Object
57 58 59 |
# File 'lib/worldwide/address.rb', line 57 def normalize(autocorrect_level: 1) dup.normalize!(autocorrect_level: autocorrect_level) end |
#normalize!(autocorrect_level: 1) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/worldwide/address.rb', line 61 def normalize!(autocorrect_level: 1) @country_code = normalize_country_code(autocorrect_level: autocorrect_level) @city = normalize_city @zip = normalize_zip @province_code = normalize_province_code || @province_code @phone = normalize_phone self end |
#single_line(additional_lines: [], excluded_fields: []) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/worldwide/address.rb', line 72 def single_line(additional_lines: [], excluded_fields: []) normalized_excluded_fields = normalize_field_names(excluded_fields) lines = build_address_format_array( additional_lines: additional_lines, excluded_fields: normalized_excluded_fields, ) if "BR" == country_code lines = adjust_single_line_format_for_brazil(lines: lines, excluded_fields: normalized_excluded_fields) end filled = fill_in_lines(lines: lines, address_data: build_formatted_address_data) stripped = strip_extra_chars(lines: filled, excluded_fields: normalized_excluded_fields) # Fallback to showing the country name by itself, in case no other information ended up being displayed if stripped.empty? stripped = [[Worldwide.region(code: country_code).full_name]] end fields = stripped.flatten.filter_map do |field| field unless blank?(field) end locale_str = I18n.locale.to_s.downcase # A handful of countries show their address in reverse order. # When formatting a mailing address for that country, we display in the country's order. # But, when constructing a single-line address like "Tokyo, Japan", we'd rather have things # in the "natural order" for the active locale. # So, in those cases where format_address returns the fields in reverse order, let's # unflip them before we get to producing our output. That way, we'll show "Tokyo, Japan" # for :en, instead of showing "Japan, Tokyo". if COUNTRIES_USING_REVERSE_ADDRESS_ORDER.include?(country_code) && locale_str.start_with?("ja", "zh") fields.reverse! end line = if locale_str.downcase.start_with?("ja") result = if fields.size < 2 || !lines.any? { |line| line.include?("{country}") } fields.reverse.join("") else fields.reverse.insert(1, ":").join("") end if Worldwide::Scripts.identify(text: result).include?(:Latn) result else result.delete(" \u{3000}") end elsif locale_str.start_with?("zh") fields.reverse.join("") else Worldwide::Lists.format(fields, join: :narrow) end line.strip end |