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
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "cgi"
4
+
3
5
  module Addressing
4
6
  class DefaultFormatter
5
7
  DEFAULT_LOCALE = "en"
@@ -7,6 +9,7 @@ module Addressing
7
9
  FORMAT_SPLIT_PATTERN = /(#{FORMAT_PLACEHOLDER_PATTERN})/
8
10
  LEADING_TRAILING_PUNCTUATION_PATTERN = /\A[ \-,]+|[ \-,]+\z/
9
11
  MULTIPLE_SPACES_PATTERN = /\s\s+/
12
+ HTML_TAG_PATTERN = /\A[a-z][a-z0-9-]*\z/i
10
13
 
11
14
  DEFAULT_OPTIONS = {
12
15
  locale: DEFAULT_LOCALE,
@@ -23,14 +26,13 @@ module Addressing
23
26
  end
24
27
 
25
28
  def format(address, options = {})
26
- assert_options(options)
27
-
28
29
  options = @default_options.merge(options)
30
+ assert_options(options)
29
31
  address_format = AddressFormat.get(address.country_code)
30
32
 
31
33
  # Add the country to the bottom or the top of the format string,
32
34
  # depending on whether the format is minor-to-major or major-to-minor.
33
- format_string = if Locale.match_candidates(address_format.locale, address.locale)
35
+ format_string = if address_format.local_format && Locale.match_candidates(address_format.locale, address.locale)
34
36
  "%country\n" + address_format.local_format
35
37
  else
36
38
  address_format.format + "\n%country"
@@ -39,13 +41,12 @@ module Addressing
39
41
  view = build_view(address, address_format, options)
40
42
  view = render_view(view)
41
43
 
42
- replacements = view.map { |key, element| ["%#{key}", element] }.to_h
44
+ replacements = view.transform_keys { |key| "%#{key}" }
43
45
  output = insert_values(format_string, replacements)
44
46
  output = clean_output(output)
45
47
 
46
48
  if options[:html]
47
49
  output = output.gsub("\n", "<br>\n")
48
- # Add the HTML wrapper element.
49
50
  output = render_html_element(value: "\n#{output}\n", html_tag: options[:html_tag], html_attributes: options[:html_attributes])
50
51
  end
51
52
 
@@ -54,34 +55,35 @@ module Addressing
54
55
 
55
56
  protected
56
57
 
57
- # Builds the view for the given address.
58
58
  def build_view(address, address_format, options)
59
- countries = country_list(options[:locale])
60
- values = values(address, address_format).merge({"country" => countries.key?(address.country_code) ? countries[address.country_code] : address.country_code})
59
+ values = values(address, address_format).merge("country" => country_name(address.country_code, options[:locale]))
61
60
  used_fields = address_format.used_fields + ["country"]
62
61
 
63
- used_fields.map do |field|
62
+ used_fields.to_h do |field|
64
63
  [field, {html: options[:html], html_tag: "span", html_attributes: {class: field.tr("_", "-")}, value: values[field]}]
65
- end.to_h
64
+ end
66
65
  end
67
66
 
68
- # Gets the country list for a locale, with caching.
69
67
  def country_list(locale)
70
68
  @country_list_cache[locale] ||= Country.list(locale)
71
69
  end
72
70
 
73
- # Renders the given view.
71
+ # The country code stands in for an unknown country.
72
+ def country_name(country_code, locale)
73
+ country_list(locale).fetch(country_code, country_code)
74
+ end
75
+
74
76
  def render_view(view)
75
- view.map do |key, element|
76
- next [key, ""] if element[:value].empty?
77
+ view.transform_values do |element|
78
+ next "" if element[:value].empty?
77
79
 
78
80
  if element[:html]
79
81
  element[:value] = CGI.escapeHTML(element[:value])
80
- next [key, render_html_element(element)]
82
+ next render_html_element(element)
81
83
  end
82
84
 
83
- [key, element[:value].gsub(/<\/?[^>]*>/, "")]
84
- end.to_h
85
+ element[:value].gsub(/<\/?[^>]*>/, "")
86
+ end
85
87
  end
86
88
 
87
89
  def render_html_element(element)
@@ -133,50 +135,36 @@ module Addressing
133
135
  end.join("\n")
134
136
  end
135
137
 
136
- # Removes empty lines, leading punctuation, excess whitespace.
137
138
  def clean_output(output)
138
139
  output.split("\n").map { |line| line.gsub(LEADING_TRAILING_PUNCTUATION_PATTERN, "").strip.gsub(MULTIPLE_SPACES_PATTERN, " ") }.reject(&:empty?).join("\n")
139
140
  end
140
141
 
141
- # Gets the address values used to build the view.
142
142
  def values(address, address_format)
143
143
  values = extract_address_values(address)
144
144
  resolve_subdivision_values(values, address, address_format)
145
145
  values
146
146
  end
147
147
 
148
- # Extracts all address field values.
149
148
  def extract_address_values(address)
150
- AddressField.all.map { |_, field| [field, address.send(field)] }.to_h
149
+ # A nil field is blank, like an empty string.
150
+ AddressField.all.values.to_h { |field| [field, address.send(field).to_s] }
151
151
  end
152
152
 
153
153
  # Replaces the subdivision values with the codes of any predefined ones.
154
154
  def resolve_subdivision_values(values, address, address_format)
155
- parents = [address.country_code]
156
-
157
- address_format.subdivision_fields.each do |field|
158
- # This level is empty, so there can be no sublevels.
159
- break if values[field].nil? || values[field].empty?
155
+ fields = address_format.subdivision_fields
156
+ chain = Subdivision.chain(address.country_code, values.values_at(*fields))
160
157
 
161
- subdivision = Subdivision.get(values[field], parents)
158
+ fields.zip(chain.subdivisions) do |field, subdivision|
162
159
  break if subdivision.nil?
163
160
 
164
- parents << values[field]
165
-
166
- # Replace the value with the expected code.
167
161
  use_local_name = Locale.match_candidates(address.locale, subdivision.locale)
168
162
  values[field] = use_local_name ? subdivision.local_code : subdivision.code
169
-
170
- # The current subdivision has no children, stop.
171
- break unless subdivision.children?
172
163
  end
173
164
  end
174
165
 
175
- private
176
-
177
- # Validates the provided options.
178
- #
179
- # Ensures the absence of unknown keys, correct data types and values.
166
+ # A subclass with more options adds them to DEFAULT_OPTIONS, extends this
167
+ # and calls super.
180
168
  def assert_options(options)
181
169
  options.each do |option, value|
182
170
  unless self.class::DEFAULT_OPTIONS.key?(option)
@@ -191,6 +179,15 @@ module Addressing
191
179
  if options.key?(:html_attributes) && !options[:html_attributes].is_a?(Hash)
192
180
  raise ArgumentError, "The option `html_attributes` must be a hash."
193
181
  end
182
+
183
+ # The tag is written into the markup as is, so only allow a tag name.
184
+ if options.key?(:html_tag) && !HTML_TAG_PATTERN.match?(options[:html_tag].to_s)
185
+ raise ArgumentError, "The option `html_tag` must be an HTML tag name."
186
+ end
187
+
188
+ if options.key?(:locale) && !options[:locale].is_a?(String)
189
+ raise ArgumentError, "The option `locale` must be a string."
190
+ end
194
191
  end
195
192
  end
196
193
  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 dependent locality types.
5
6
  class DependentLocalityType < Enum
6
7
  DISTRICT = "district"
7
8
  NEIGHBORHOOD = "neighborhood"
@@ -9,7 +10,6 @@ module Addressing
9
10
  SUBURB = "suburb"
10
11
  TOWNLAND = "townland"
11
12
 
12
- # Gets the default value.
13
13
  def self.default
14
14
  SUBURB
15
15
  end
@@ -3,32 +3,22 @@
3
3
  module Addressing
4
4
  class Enum
5
5
  class << self
6
- # Gets all available values.
7
6
  def all
8
- @values ||= {}
9
- if !@values.key?(name)
10
- @values[name] = constants.map { |constant| [constant, const_get(constant)] }.to_h
11
- end
12
-
13
- @values[name]
7
+ @all ||= constants.to_h { |constant| [constant, const_get(constant)] }
14
8
  end
15
9
 
16
- # Gets the key of the provided value.
17
10
  def key(value)
18
11
  all.key(value)
19
12
  end
20
13
 
21
- # Checks whether the provided value is defined.
22
14
  def exists?(value)
23
15
  all.has_value?(value)
24
16
  end
25
17
 
26
- # Asserts that the provided value is defined.
27
18
  def assert_exists(value)
28
19
  raise ArgumentError, "#{value} is not a valid #{name} value." unless exists?(value)
29
20
  end
30
21
 
31
- # Asserts that all provided values are defined.
32
22
  def assert_all_exist(values)
33
23
  values.each { |value| assert_exists(value.to_s) }
34
24
  end
@@ -1,10 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Addressing
4
- # Base error class for all Addressing errors
5
4
  class Error < StandardError; end
6
5
 
7
- # Raised when an unknown country code is provided
8
6
  class UnknownCountryError < Error
9
7
  def initialize(country_code)
10
8
  super("Unknown country code: #{country_code}")
@@ -14,7 +12,6 @@ module Addressing
14
12
  attr_reader :country_code
15
13
  end
16
14
 
17
- # Raised when an unknown locale is provided
18
15
  class UnknownLocaleError < Error
19
16
  def initialize(locale)
20
17
  super("Unknown locale: #{locale}")
@@ -24,6 +21,5 @@ module Addressing
24
21
  attr_reader :locale
25
22
  end
26
23
 
27
- # Raised when an invalid argument is provided
28
24
  class InvalidArgumentError < Error; end
29
25
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "addressing/enum"
4
+
3
5
  module Addressing
4
6
  class FieldOverride < Enum
5
7
  HIDDEN = "hidden"
@@ -11,10 +13,12 @@ module Addressing
11
13
  attr_reader :hidden_fields, :optional_fields, :required_fields
12
14
 
13
15
  def initialize(definition)
16
+ # Fields and overrides are compared as strings, like the constants.
17
+ definition = definition.transform_keys(&:to_s).transform_values(&:to_s)
18
+
14
19
  AddressField.assert_all_exist(definition.keys)
15
20
  FieldOverride.assert_all_exist(definition.values)
16
21
 
17
- # Group fields by their override type
18
22
  grouped = definition.group_by { |_field, override| override }
19
23
  .transform_values { |pairs| pairs.map(&:first) }
20
24
 
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Addressing
4
+ # One field of an address that breaks a rule of the address format for its
5
+ # country, together with the kind of rule it breaks. A blank country code is
6
+ # also a field violation.
7
+ #
8
+ # field is the field as a symbol, such as :postal_code. kind is one of:
9
+ #
10
+ # - :blank, the field must not be blank.
11
+ # - :present, the field must be blank.
12
+ # - :invalid, the value matches no predefined subdivision or does not match
13
+ # the postal code pattern.
14
+ #
15
+ # The kinds match the error types of Rails.
16
+ FieldViolation = Data.define(:field, :kind)
17
+ end
@@ -1,33 +1,36 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "forwardable"
4
+
3
5
  module Addressing
6
+ # The children of a subdivision, loaded on first access.
7
+ #
8
+ # Behaves like the hash that Subdivision.all returns for the same parents.
4
9
  class LazySubdivisions
10
+ extend Forwardable
11
+ include Enumerable
12
+
13
+ def_delegators :subdivisions, :each, :size, :keys, :values, :key?, :include?, :member?, :[], :fetch, :select, :filter, :reject
14
+
5
15
  def initialize(parents)
6
16
  @parents = parents
7
- @subdivisions = {}
8
- @initialized = false
9
17
  end
10
18
 
11
- def [](key)
12
- do_initialize unless @initialized
13
- @subdivisions[key]
19
+ # Checks for children without building them.
20
+ def empty?
21
+ @subdivisions ? @subdivisions.empty? : !Subdivision.any?(@parents)
14
22
  end
15
23
 
16
- def any?
17
- do_initialize unless @initialized
18
- @subdivisions.any?
19
- end
24
+ def any?(*args, &block)
25
+ return subdivisions.any?(*args, &block) if block || args.any?
20
26
 
21
- def empty?
22
- do_initialize unless @initialized
23
- @subdivisions.empty?
27
+ !empty?
24
28
  end
25
29
 
26
30
  private
27
31
 
28
- def do_initialize
29
- @initialized = true
30
- @subdivisions = Subdivision.all(@parents)
32
+ def subdivisions
33
+ @subdivisions ||= Subdivision.all(@parents)
31
34
  end
32
35
  end
33
36
  end