addressing 2.1.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +45 -0
  3. data/README.md +223 -112
  4. data/data/UPSTREAM_VERSION +1 -0
  5. data/data/address_formats.json +2256 -206
  6. data/data/countries.json +1282 -0
  7. data/data/locale.json +222 -0
  8. data/data/subdivision/BR-AC.json +22 -22
  9. data/data/subdivision/BR-AL.json +102 -102
  10. data/data/subdivision/BR-AM.json +62 -62
  11. data/data/subdivision/BR-AP.json +16 -16
  12. data/data/subdivision/BR-BA.json +417 -417
  13. data/data/subdivision/BR-CE.json +184 -184
  14. data/data/subdivision/BR-DF.json +1 -1
  15. data/data/subdivision/BR-ES.json +79 -79
  16. data/data/subdivision/BR-GO.json +246 -246
  17. data/data/subdivision/BR-MA.json +217 -217
  18. data/data/subdivision/BR-MG.json +853 -853
  19. data/data/subdivision/BR-MS.json +78 -78
  20. data/data/subdivision/BR-MT.json +141 -141
  21. data/data/subdivision/BR-PA.json +144 -144
  22. data/data/subdivision/BR-PB.json +223 -223
  23. data/data/subdivision/BR-PE.json +185 -185
  24. data/data/subdivision/BR-PI.json +223 -223
  25. data/data/subdivision/BR-PR.json +400 -400
  26. data/data/subdivision/BR-RJ.json +93 -93
  27. data/data/subdivision/BR-RN.json +166 -166
  28. data/data/subdivision/BR-RO.json +52 -52
  29. data/data/subdivision/BR-RR.json +15 -15
  30. data/data/subdivision/BR-RS.json +497 -497
  31. data/data/subdivision/BR-SC.json +295 -295
  32. data/data/subdivision/BR-SE.json +75 -75
  33. data/data/subdivision/BR-SP.json +645 -645
  34. data/data/subdivision/BR-TO.json +139 -139
  35. data/data/subdivision/CL-AI.json +10 -10
  36. data/data/subdivision/CL-AN.json +9 -9
  37. data/data/subdivision/CL-AP.json +4 -4
  38. data/data/subdivision/CL-AR.json +32 -32
  39. data/data/subdivision/CL-AT.json +9 -9
  40. data/data/subdivision/CL-BI.json +34 -34
  41. data/data/subdivision/CL-CO.json +15 -15
  42. data/data/subdivision/CL-LI.json +33 -33
  43. data/data/subdivision/CL-LL.json +30 -30
  44. data/data/subdivision/CL-LR.json +12 -12
  45. data/data/subdivision/CL-MA.json +11 -11
  46. data/data/subdivision/CL-ML.json +30 -30
  47. data/data/subdivision/CL-NB.json +21 -21
  48. data/data/subdivision/CL-RM.json +52 -52
  49. data/data/subdivision/CL-TA.json +7 -7
  50. data/data/subdivision/CL-VS.json +38 -38
  51. data/data/subdivision/CV.json +9 -9
  52. data/data/subdivision/KY.json +3 -3
  53. data/data/subdivision/TV.json +1 -1
  54. data/lib/addressing/address.rb +7 -4
  55. data/lib/addressing/address_field.rb +2 -1
  56. data/lib/addressing/address_format.rb +58 -88
  57. data/lib/addressing/address_validator.rb +89 -0
  58. data/lib/addressing/administrative_area_type.rb +2 -2
  59. data/lib/addressing/blank.rb +29 -0
  60. data/lib/addressing/country.rb +33 -321
  61. data/lib/addressing/data_source.rb +77 -0
  62. data/lib/addressing/default_formatter.rb +35 -38
  63. data/lib/addressing/dependent_locality_type.rb +2 -2
  64. data/lib/addressing/enum.rb +1 -11
  65. data/lib/addressing/exceptions.rb +0 -4
  66. data/lib/addressing/field_override.rb +5 -1
  67. data/lib/addressing/field_violation.rb +17 -0
  68. data/lib/addressing/lazy_subdivisions.rb +18 -15
  69. data/lib/addressing/locale.rb +27 -288
  70. data/lib/addressing/locality_type.rb +2 -2
  71. data/lib/addressing/model.rb +21 -79
  72. data/lib/addressing/postal_code_type.rb +2 -2
  73. data/lib/addressing/postal_label_formatter.rb +20 -23
  74. data/lib/addressing/subdivision.rb +110 -126
  75. data/lib/addressing/version.rb +1 -1
  76. data/lib/addressing.rb +7 -9
  77. metadata +9 -3
  78. data/data/address_formats.dump +0 -0
@@ -2,229 +2,6 @@
2
2
 
3
3
  module Addressing
4
4
  class Locale
5
- ALIASES = {
6
- "az-AZ" => "az-Latn-AZ",
7
- "bs-BA" => "bs-Latn-BA",
8
- "ha-GH" => "ha-Latn-GH",
9
- "ha-NE" => "ha-Latn-NE",
10
- "ha-NG" => "ha-Latn-NG",
11
- "in" => "id",
12
- "in-ID" => "id-ID",
13
- "iw" => "he",
14
- "iw-IL" => "he-IL",
15
- "kk-KZ" => "kk-Cyrl-KZ",
16
- "ks-IN" => "ks-Arab-IN",
17
- "ky-KG" => "ky-Cyrl-KG",
18
- "mn-MN" => "mn-Cyrl-MN",
19
- "mo" => "ro-MD",
20
- "ms-BN" => "ms-Latn-BN",
21
- "ms-MY" => "ms-Latn-MY",
22
- "ms-SG" => "ms-Latn-SG",
23
- "no" => "nb",
24
- "no-NO" => "nb-NO",
25
- "no-NO-NY" => "nn-NO",
26
- "pa-IN" => "pa-Guru-IN",
27
- "pa-PK" => "pa-Arab-PK",
28
- "sh" => "sr-Latn",
29
- "sh-BA" => "sr-Latn-BA",
30
- "sh-CS" => "sr-Latn-RS",
31
- "sh-YU" => "sr-Latn-RS",
32
- "shi-MA" => "shi-Tfng-MA",
33
- "sr-BA" => "sr-Cyrl-BA",
34
- "sr-ME" => "sr-Latn-ME",
35
- "sr-RS" => "sr-Cyrl-RS",
36
- "sr-XK" => "sr-Cyrl-XK",
37
- "tl" => "fil",
38
- "tl-PH" => "fil-PH",
39
- "tzm-MA" => "tzm-Latn-MA",
40
- "ug-CN" => "ug-Arab-CN",
41
- "uz-AF" => "uz-Arab-AF",
42
- "uz-UZ" => "uz-Latn-UZ",
43
- "vai-LR" => "vai-Vaii-LR",
44
- "zh-CN" => "zh-Hans-CN",
45
- "zh-HK" => "zh-Hant-HK",
46
- "zh-MO" => "zh-Hant-MO",
47
- "zh-SG" => "zh-Hans-SG",
48
- "zh-TW" => "zh-Hant-TW"
49
- }.freeze
50
-
51
- PARENTS = {
52
- "en-150" => "en-001",
53
- "en-AG" => "en-001",
54
- "en-AI" => "en-001",
55
- "en-AT" => "en-150",
56
- "en-AU" => "en-001",
57
- "en-BB" => "en-001",
58
- "en-BE" => "en-150",
59
- "en-BM" => "en-001",
60
- "en-BS" => "en-001",
61
- "en-BW" => "en-001",
62
- "en-BZ" => "en-001",
63
- "en-CC" => "en-001",
64
- "en-CH" => "en-150",
65
- "en-CK" => "en-001",
66
- "en-CM" => "en-001",
67
- "en-CX" => "en-001",
68
- "en-CY" => "en-001",
69
- "en-CZ" => "en-150",
70
- "en-DE" => "en-150",
71
- "en-DG" => "en-001",
72
- "en-DK" => "en-150",
73
- "en-DM" => "en-001",
74
- "en-ER" => "en-001",
75
- "en-ES" => "en-150",
76
- "en-FI" => "en-150",
77
- "en-FJ" => "en-001",
78
- "en-FK" => "en-001",
79
- "en-FM" => "en-001",
80
- "en-FR" => "en-150",
81
- "en-GB" => "en-001",
82
- "en-GD" => "en-001",
83
- "en-GG" => "en-001",
84
- "en-GH" => "en-001",
85
- "en-GI" => "en-001",
86
- "en-GM" => "en-001",
87
- "en-GS" => "en-001",
88
- "en-GY" => "en-001",
89
- "en-HK" => "en-001",
90
- "en-HU" => "en-150",
91
- "en-ID" => "en-001",
92
- "en-IE" => "en-001",
93
- "en-IL" => "en-001",
94
- "en-IM" => "en-001",
95
- "en-IN" => "en-001",
96
- "en-IO" => "en-001",
97
- "en-IT" => "en-150",
98
- "en-JE" => "en-001",
99
- "en-JM" => "en-001",
100
- "en-KE" => "en-001",
101
- "en-KI" => "en-001",
102
- "en-KN" => "en-001",
103
- "en-KY" => "en-001",
104
- "en-LC" => "en-001",
105
- "en-LR" => "en-001",
106
- "en-LS" => "en-001",
107
- "en-MG" => "en-001",
108
- "en-MO" => "en-001",
109
- "en-MS" => "en-001",
110
- "en-MT" => "en-001",
111
- "en-MU" => "en-001",
112
- "en-MV" => "en-001",
113
- "en-MW" => "en-001",
114
- "en-MY" => "en-001",
115
- "en-NA" => "en-001",
116
- "en-NF" => "en-001",
117
- "en-NG" => "en-001",
118
- "en-NL" => "en-150",
119
- "en-NO" => "en-150",
120
- "en-NR" => "en-001",
121
- "en-NU" => "en-001",
122
- "en-NZ" => "en-001",
123
- "en-PG" => "en-001",
124
- "en-PK" => "en-001",
125
- "en-PL" => "en-150",
126
- "en-PN" => "en-001",
127
- "en-PT" => "en-150",
128
- "en-PW" => "en-001",
129
- "en-RO" => "en-150",
130
- "en-RW" => "en-001",
131
- "en-SB" => "en-001",
132
- "en-SC" => "en-001",
133
- "en-SD" => "en-001",
134
- "en-SE" => "en-150",
135
- "en-SG" => "en-001",
136
- "en-SH" => "en-001",
137
- "en-SI" => "en-150",
138
- "en-SK" => "en-150",
139
- "en-SL" => "en-001",
140
- "en-SS" => "en-001",
141
- "en-SX" => "en-001",
142
- "en-SZ" => "en-001",
143
- "en-TC" => "en-001",
144
- "en-TK" => "en-001",
145
- "en-TO" => "en-001",
146
- "en-TT" => "en-001",
147
- "en-TV" => "en-001",
148
- "en-TZ" => "en-001",
149
- "en-UG" => "en-001",
150
- "en-VC" => "en-001",
151
- "en-VG" => "en-001",
152
- "en-VU" => "en-001",
153
- "en-WS" => "en-001",
154
- "en-ZA" => "en-001",
155
- "en-ZM" => "en-001",
156
- "en-ZW" => "en-001",
157
- "es-AR" => "es-419",
158
- "es-BO" => "es-419",
159
- "es-BR" => "es-419",
160
- "es-BZ" => "es-419",
161
- "es-CL" => "es-419",
162
- "es-CO" => "es-419",
163
- "es-CR" => "es-419",
164
- "es-CU" => "es-419",
165
- "es-DO" => "es-419",
166
- "es-EC" => "es-419",
167
- "es-GT" => "es-419",
168
- "es-HN" => "es-419",
169
- "es-MX" => "es-419",
170
- "es-NI" => "es-419",
171
- "es-PA" => "es-419",
172
- "es-PE" => "es-419",
173
- "es-PR" => "es-419",
174
- "es-PY" => "es-419",
175
- "es-SV" => "es-419",
176
- "es-US" => "es-419",
177
- "es-UY" => "es-419",
178
- "es-VE" => "es-419",
179
- "hi-Latn" => "und",
180
- "ht" => "fr-HT",
181
- "nb" => "no",
182
- "nn" => "no",
183
- "pt-AO" => "pt-PT",
184
- "pt-CH" => "pt-PT",
185
- "pt-CV" => "pt-PT",
186
- "pt-GQ" => "pt-PT",
187
- "pt-GW" => "pt-PT",
188
- "pt-LU" => "pt-PT",
189
- "pt-MO" => "pt-PT",
190
- "pt-MZ" => "pt-PT",
191
- "pt-ST" => "pt-PT",
192
- "pt-TL" => "pt-PT",
193
- "az-Arab" => "und",
194
- "az-Cyrl" => "und",
195
- "bal-Latn" => "und",
196
- "blt-Latn" => "und",
197
- "bs-Cyrl" => "und",
198
- "byn-Latn" => "und",
199
- "en-Dsrt" => "und",
200
- "en-Shaw" => "und",
201
- "iu-Latn" => "und",
202
- "kk-Arab" => "und",
203
- "ks-Deva" => "und",
204
- "ku-Arab" => "und",
205
- "ky-Arab" => "und",
206
- "ky-Latn" => "und",
207
- "ml-Arab" => "und",
208
- "mn-Mong" => "und",
209
- "mni-Mtei" => "und",
210
- "ms-Arab" => "und",
211
- "pa-Arab" => "und",
212
- "sat-Deva" => "und",
213
- "sd-Deva" => "und",
214
- "sd-Khoj" => "und",
215
- "sd-Sind" => "und",
216
- "so-Arab" => "und",
217
- "sr-Latn" => "und",
218
- "sw-Arab" => "und",
219
- "tg-Arab" => "und",
220
- "uz-Arab" => "und",
221
- "uz-Cyrl" => "und",
222
- "yue-Hans" => "und",
223
- "zh-Hant" => "und",
224
- "zh-Hant-MO" => "zh-Hant-HK"
225
- }.freeze
226
-
227
- # Checks whether two locales match.
228
5
  def self.match(first_locale, second_locale)
229
6
  return false if first_locale.to_s.empty? || second_locale.to_s.empty?
230
7
 
@@ -234,59 +11,39 @@ module Addressing
234
11
  # Checks whether two locales have at least one common candidate.
235
12
  #
236
13
  # For example, "de" and "de-AT" will match because they both have
237
- # "de" in common. This is useful for partial locale matching.
14
+ # "de" in common.
238
15
  def self.match_candidates(first_locale, second_locale)
239
16
  return false if first_locale.to_s.empty? || second_locale.to_s.empty?
240
17
 
241
- first_locale = canonicalize(first_locale)
242
- second_locale = canonicalize(second_locale)
243
- first_locale_candidates = candidates(first_locale)
244
- second_locale_candidates = candidates(second_locale)
245
-
246
- (first_locale_candidates & second_locale_candidates).any?
18
+ (candidates(canonicalize(first_locale)) & candidates(canonicalize(second_locale))).any?
247
19
  end
248
20
 
249
- # Resolves the locale from the available locales.
21
+ # Gets the first candidate of the locale, then of the fallback locale,
22
+ # that is available.
250
23
  #
251
- # Takes all locale candidates for the requested locale
252
- # and fallback locale, searches for them in the available
253
- # locale list. The first found locale is returned.
254
- # If no candidate is found in the list, an error is raised.
24
+ # @raise [UnknownLocaleError] if none is available
255
25
  def self.resolve(available_locales, locale, fallback_locale = nil)
256
26
  locale = canonicalize(locale)
257
- resolved_locale = nil
258
-
259
- candidates(locale, fallback_locale).each do |candidate|
260
- if available_locales.include?(candidate)
261
- resolved_locale = candidate
262
- break
263
- end
264
- end
27
+ resolved_locale = candidates(locale, fallback_locale).find { |candidate| available_locales.include?(candidate) }
265
28
 
266
- # No locale could be resolved, stop here.
267
29
  raise UnknownLocaleError.new(locale) if resolved_locale.nil?
268
30
 
269
31
  resolved_locale
270
32
  end
271
33
 
272
- # Canonicalizes the given locale.
273
- #
274
34
  # Standardizes separators and capitalization, turning
275
35
  # a locale such as "sr_rs_latn" into "sr-RS-Latn".
276
36
  def self.canonicalize(locale)
277
37
  return locale if locale.to_s.empty?
278
38
 
279
- # Lowercase the locale and replace all dashes with underscores.
280
39
  locale_parts = locale.downcase.tr("_", "-").split("-")
281
40
 
282
41
  locale_parts.map.with_index do |part, index|
283
42
  # The language code should stay lowercase.
284
43
  next part if index == 0
285
44
 
286
- if part.length == 4
287
- # Script code.
288
- next part.capitalize
289
- end
45
+ # A script code.
46
+ next part.capitalize if part.length == 4
290
47
 
291
48
  # Country or variant code.
292
49
  part.upcase
@@ -300,58 +57,40 @@ module Addressing
300
57
  # 2) bs-Cyrl
301
58
  # 3) bs
302
59
  #
303
- # The locale is de-aliased, e.g. the candidates for "sh" are:
304
- # 1) sr-Latn
305
- # 2) sr
60
+ # The locale is de-aliased first: the candidates for "sh" start with
61
+ # "sr-Latn".
306
62
  def self.candidates(locale, fallback_locale = nil)
307
- locale = replace_alias(locale)
308
- candidates = [locale]
309
-
310
- while (parent = parent(locale))
311
- candidates << parent
312
- locale = parent
313
- end
314
-
315
- if fallback_locale
316
- candidates << fallback_locale
317
-
318
- while (parent = parent(fallback_locale))
319
- candidates << parent
320
- fallback_locale = parent
321
- end
322
- end
323
-
63
+ candidates = lineage(replace_alias(locale))
64
+ candidates += lineage(fallback_locale) if fallback_locale
324
65
  candidates.uniq
325
66
  end
326
67
 
327
- # Gets the parent for the given locale.
328
- def self.parent(locale)
329
- parent = nil
68
+ # Gets the given locale followed by its parents.
69
+ def self.lineage(locale)
70
+ lineage = [locale]
71
+ lineage << locale while (locale = parent(locale))
72
+ lineage
73
+ end
74
+ private_class_method :lineage
330
75
 
331
- if PARENTS.key?(locale)
332
- parent = PARENTS[locale]
333
- elsif locale.include?("-")
334
- locale_parts = locale.split("-")
335
- locale_parts.pop
336
- parent = locale_parts.join("-")
337
- end
76
+ def self.parent(locale)
77
+ parent = definitions["parents"].fetch(locale) { locale.include?("-") ? locale.rpartition("-").first : nil }
338
78
 
339
79
  # The library doesn't have data for the empty `und` locale, it
340
80
  # is more user friendly to use the configured fallback instead.
341
- if parent == "und"
342
- parent = nil
343
- end
344
-
345
- parent
81
+ (parent == "und") ? nil : parent
346
82
  end
347
83
 
348
84
  # Replaces a locale alias with the real locale.
349
85
  #
350
86
  # For example, "zh-CN" is replaced with "zh-Hans-CN".
351
87
  def self.replace_alias(locale)
352
- return locale if locale.to_s.empty? || !ALIASES.key?(locale)
88
+ definitions["aliases"].fetch(locale, locale)
89
+ end
353
90
 
354
- ALIASES[locale]
91
+ def self.definitions
92
+ Addressing.data_source.fetch("locale")
355
93
  end
94
+ private_class_method :definitions
356
95
  end
357
96
  end
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "addressing/enum"
4
+
3
5
  module Addressing
4
- # Enumerates available locality types.
5
6
  class LocalityType < Enum
6
7
  CITY = "city"
7
8
  DISTRICT = "district"
@@ -9,7 +10,6 @@ module Addressing
9
10
  SUBURB = "suburb"
10
11
  TOWN_CITY = "town_city"
11
12
 
12
- # Gets the default value.
13
13
  def self.default
14
14
  CITY
15
15
  end
@@ -2,94 +2,36 @@
2
2
 
3
3
  module Addressing
4
4
  module Model
5
+ FIELD_VIOLATION_MESSAGES = {
6
+ blank: "should not be blank",
7
+ present: "should be blank",
8
+ invalid: "should be valid"
9
+ }.freeze
10
+ private_constant :FIELD_VIOLATION_MESSAGES
11
+
5
12
  def validates_address_format(
6
- fields: [:country_code, :administrative_area, :locality, :dependent_locality, :postal_code, :sorting_code, :address_line1, :address_line2, :organization, :given_name, :additional_name, :family_name, :locale], field_overrides: nil, verify_postal_code: true, **options
13
+ fields: Address::FIELDS, field_overrides: nil, verify_postal_code: true, **options
7
14
  )
8
15
  fields = Array(fields)
9
- field_overrides ||= FieldOverrides.new({})
10
-
11
- options[:if] ||= -> { fields.any? { |f| changes.key?(f.to_s) } } unless options[:unless]
12
-
13
- class_eval do
14
- validate :verify_address_format, **options
15
-
16
- define_method :verify_address_format do
17
- values = fields.each_with_object({}) { |f, v| v[f] = send(f) if respond_to?(f) }
18
- address = Address.new(**values)
19
-
20
- return unless address.country_code.present?
21
-
22
- address_format = AddressFormat.get(address.country_code)
23
-
24
- # Validate the presence of required fields.
25
- AddressFormatHelper.required_fields(address_format, field_overrides).each do |required_field|
26
- next unless address.send(required_field).blank?
27
-
28
- errors.add(required_field, "should not be blank")
29
- end
30
-
31
- used_fields = address_format.used_fields - field_overrides.hidden_fields
32
-
33
- # Validate the absence of unused fields.
34
- unused_fields = AddressField.all.values - used_fields
35
- unused_fields.each do |unused_field|
36
- next if address.send(unused_field).blank?
37
-
38
- errors.add(unused_field, "should be blank")
39
- end
40
-
41
- # Validate subdivisions.
42
- subdivisions = verify_subdivisions(address, address_format, field_overrides)
43
-
44
- # Validate postal code.
45
- verify_postal_code(address.postal_code, subdivisions, address_format) if used_fields.include?(AddressField::POSTAL_CODE) && verify_postal_code
46
- end
47
-
48
- define_method :verify_subdivisions do |address, address_format, field_overrides|
49
- parents = [address_format.country_code]
50
- subdivisions = []
51
-
52
- address_format.subdivision_fields.each do |field|
53
- value = address.send(field)
54
- # The field is empty or validation is disabled.
55
- break if value.blank? || field_overrides.hidden_fields.include?(field)
56
-
57
- subdivision = Subdivision.get(value, parents)
58
- if subdivision.nil?
59
- errors.add(field, "should be valid")
60
- break
61
- end
62
-
63
- parents << value
64
- subdivisions << subdivision
65
- # No predefined subdivisions below this level, stop here.
66
- break unless subdivision.children?
67
- end
68
-
69
- subdivisions
70
- end
16
+ # The validator has the default field overrides.
17
+ validator_options = {field_overrides: field_overrides}.compact.merge(verify_postal_code: verify_postal_code)
71
18
 
72
- define_method :verify_postal_code do |postal_code, subdivisions, address_format|
73
- # Nothing to validate.
74
- return if postal_code.blank?
19
+ # Without change tracking, as on a plain ActiveModel class, validate every time.
20
+ options[:if] ||= -> { !respond_to?(:changes) || fields.any? { |f| changes.key?(f.to_s) } } unless options[:unless]
75
21
 
76
- pattern = subdivisions.inject(address_format.postal_code_pattern) do |pattern, subdivision|
77
- subdivision_pattern = subdivision.postal_code_pattern
78
- next pattern if subdivision_pattern.blank?
22
+ validate :validate_address_format, **options
79
23
 
80
- subdivision_pattern
81
- end
24
+ # A method rather than a block, so that a later call, in a subclass for
25
+ # example, replaces the configuration of an earlier one.
26
+ define_method :validate_address_format do
27
+ values = fields.each_with_object({}) { |f, v| v[f] = send(f) if respond_to?(f) }
28
+ address = Address.new(**values)
82
29
 
83
- if pattern
84
- # The pattern must match the provided value completely.
85
- match = postal_code.match(Regexp.new(pattern.gsub("\\\\", "\\").to_s, "i"))
86
- if match.nil? || match[0] != postal_code
87
- errors.add(AddressField::POSTAL_CODE, "should be valid")
88
- nil
89
- end
90
- end
30
+ AddressValidator.validate(address, **validator_options).each do |violation|
31
+ errors.add(violation.field, violation.kind, message: FIELD_VIOLATION_MESSAGES.fetch(violation.kind))
91
32
  end
92
33
  end
34
+ private :validate_address_format
93
35
  end
94
36
  end
95
37
  end
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "addressing/enum"
4
+
3
5
  module Addressing
4
- # Enumerates available postal code types.
5
6
  class PostalCodeType < Enum
6
7
  EIR = "eircode"
7
8
  PIN = "pin"
8
9
  POSTAL = "postal"
9
10
  ZIP = "zip"
10
11
 
11
- # Gets the default value.
12
12
  def self.default
13
13
  POSTAL
14
14
  end
@@ -1,35 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "addressing/default_formatter"
4
+
3
5
  module Addressing
4
6
  class PostalLabelFormatter < DefaultFormatter
5
- DEFAULT_OPTIONS = {
6
- locale: DEFAULT_LOCALE,
7
- html: false,
8
- html_tag: "p",
9
- html_attributes: {translate: "no"},
10
- origin_country: ""
11
- }
7
+ DEFAULT_OPTIONS = DefaultFormatter::DEFAULT_OPTIONS.merge(html: false, origin_country: "")
12
8
 
13
9
  protected
14
10
 
15
- def build_view(address, address_format, options)
16
- raise ArgumentError, "The origin_country option cannot be empty." if options[:origin_country].empty?
11
+ # The origin country has no default, so format raises until it is given,
12
+ # to the constructor or to format.
13
+ def assert_options(options)
14
+ super
15
+
16
+ return unless options.key?(:origin_country)
17
+
18
+ origin_country = options[:origin_country]
19
+ raise ArgumentError, "The option `origin_country` must be a non-empty string." unless origin_country.is_a?(String) && !origin_country.empty?
20
+ end
17
21
 
22
+ def build_view(address, address_format, options)
18
23
  view = super
19
- view = view.map do |key, element|
20
- # Uppercase fields where required by the format.
24
+ view.each do |key, element|
21
25
  element[:value] = element[:value].upcase if address_format.uppercase_fields.include?(key)
22
- [key, element]
23
- end.to_h
24
-
25
- # Handle international mailing.
26
- if address.country_code != options[:origin_country]
27
- # Prefix the postal code.
28
- field = AddressField::POSTAL_CODE
26
+ end
29
27
 
30
- if view.key?(field)
31
- view[field][:value] = [address_format.postal_code_prefix, view[field][:value]].join
32
- end
28
+ if address.country_code != options[:origin_country].upcase
29
+ postal_code = view[AddressField::POSTAL_CODE]
30
+ postal_code[:value] = "#{address_format.postal_code_prefix}#{postal_code[:value]}" if postal_code && !postal_code[:value].empty?
33
31
 
34
32
  # Universal Postal Union says: "The name of the country of
35
33
  # destination shall be written preferably in the language of the
@@ -37,8 +35,7 @@ module Addressing
37
35
  # transit, it is desirable for the name of the country of
38
36
  # destination to be added in an internationally known language.
39
37
  country = view["country"][:value]
40
- english_countries = Country.list("en")
41
- english_country = english_countries[address.country_code]
38
+ english_country = country_name(address.country_code, "en")
42
39
 
43
40
  if country != english_country
44
41
  country += " - #{english_country}"