worldwide 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 70f7f2806122734340656c024d816ec4028060de514b5dc4773529ce8bff4f37
4
- data.tar.gz: 3c5c2237879ab05f1a77505432bca89ad50e9d5667ff1d1951b501c07f43050e
3
+ metadata.gz: 4dca8f095b6020792ecd6186606db9cd6159d03414b735b0c2d4b30f0746b680
4
+ data.tar.gz: 7e40cbbd862960354faf241a3320c03b1a081fa005f42063bf27951ca94f3c1d
5
5
  SHA512:
6
- metadata.gz: 1f8e9fa1416ccc24d3818e609acf7d05da6534cdb9b1e780af496151559bbb5e73540df416d85d554a3083a5add5691df2ab512fa023da3ca43d85251a89c18d
7
- data.tar.gz: d55d1ed819560e45099c6ae1fc9e1cc5f62e0a6b62c4e9b24aa97daa78b7aae0e6cb8636dc0b6086ca5ba7f3a715badc0e7d4c60ddde09df49cb2b2c00677e8c
6
+ metadata.gz: 1c385197a1a3239d1d1251a0cb7b45fe3f04f4d9e75dc3ed02227fe9692184b12dc5754c4c5292bfbd24e4a079df48b2ea63689e63b11511ec1f7017627ac15a
7
+ data.tar.gz: 666f0e760f82cfab270d47328a9600204e7c93c04bd16106b5f6d46cadefcd15f90dc64ea5663f0eeefc2827458aa2b47147468b45da059a57c37dd146893444
data/CHANGELOG.md CHANGED
@@ -28,6 +28,11 @@ Nil.
28
28
 
29
29
  ---
30
30
 
31
+ [0.4.0] - 2023-11-08
32
+ - Add region name alternates [#32](https://github.com/Shopify/worldwide/pull/32)
33
+ - Cache `Region#parent_name` [#33](https://github.com/Shopify/worldwide/pull/33)
34
+ - Use hash tables to look up regions by code [#36](https://github.com/Shopify/worldwide/pull/36)
35
+
31
36
  [0.3.0] - 2023-11-03
32
37
  - Add code alternates for Japan [#23](https://github.com/Shopify/worldwide/pull/23)
33
38
  - Add code alternates for Puerto Rico [#24](https://github.com/Shopify/worldwide/pull/24)
data/Gemfile.lock CHANGED
@@ -13,7 +13,7 @@ GIT
13
13
  PATH
14
14
  remote: .
15
15
  specs:
16
- worldwide (0.3.0)
16
+ worldwide (0.4.0)
17
17
  activesupport (~> 7.0)
18
18
  i18n (~> 1.12.0)
19
19
  phonelib (~> 0.8)
@@ -7,6 +7,8 @@ unit_system: imperial
7
7
  tax_name: Federal Tax
8
8
  group: North American Countries
9
9
  group_name: North America
10
+ name_alternates:
11
+ - United States of America
10
12
  zip_label: Zip code
11
13
  zip_regex: "(?-mix:\\A\\d{5}(-\\d{4})?\\z)|(?i-mx:\\AFPO\\s*A[A-Z]\\s*\\d{5}-\\d{4}\\z)"
12
14
  zip_example: '90210'
@@ -110,6 +110,10 @@ module Worldwide
110
110
  # This name is the name that was traditionally returned by "country_db".
111
111
  attr_reader :legacy_name
112
112
 
113
+ # Other names that may be used to refer to this region.
114
+ # E.g., "Czech Republic" is also known as "Czechia".
115
+ attr_accessor :name_alternates
116
+
113
117
  # iso_code values of regions (subdivisions) within the same country that border this region.
114
118
  # E.g., for CA-ON, the neighbouring zones are CA-MB, CA-NU and CA-QC.
115
119
  attr_accessor :neighbours
@@ -438,7 +442,7 @@ module Worldwide
438
442
  end
439
443
 
440
444
  def parent_country
441
- parents.find(&:country?)
445
+ @parent_country ||= parents.find(&:country?)
442
446
  end
443
447
 
444
448
  # Checks whether the given value is acceptable according to the regular expression defined for the country.
@@ -16,7 +16,7 @@ module Worldwide
16
16
  end
17
17
 
18
18
  def initialize
19
- @regions = RegionsLoader.new.load_regions
19
+ @regions, @regions_by_cldr_code, @regions_by_iso_code = RegionsLoader.new.load_regions
20
20
  end
21
21
 
22
22
  def all
@@ -29,17 +29,9 @@ module Worldwide
29
29
  end
30
30
 
31
31
  result = if cldr
32
- search_code = cldr.to_s.upcase
33
-
34
- @regions.find do |r|
35
- r.send(:answers_to_cldr_code, search_code)
36
- end
32
+ @regions_by_cldr_code[cldr.to_s.upcase]
37
33
  elsif code
38
- search_code = code.to_s.upcase
39
-
40
- @regions.find do |r|
41
- r.send(:answers_to_iso_code, search_code) || r.alpha_three == search_code || r.numeric_three == search_code
42
- end
34
+ @regions_by_iso_code[code.to_s.upcase]
43
35
  else # search by name
44
36
  search_name = name.upcase
45
37
 
@@ -9,6 +9,9 @@ module Worldwide
9
9
  WORLD_CODE = "001" # UN code for the whole world
10
10
 
11
11
  def load_regions
12
+ @regions_by_cldr_code = {}
13
+ @regions_by_iso_code = {}
14
+
12
15
  # Load country/region definitions out of the db/data/regions/??.yml files
13
16
  @regions = Dir["#{Worldwide::Paths::REGIONS_ROOT}/*.yml"].map do |filename|
14
17
  load_territory(filename)
@@ -25,11 +28,39 @@ module Worldwide
25
28
 
26
29
  construct_unknown
27
30
 
28
- @regions.freeze
31
+ @regions.each do |region|
32
+ construct_lookup_info(region)
33
+ end
34
+
35
+ [@regions.freeze, @regions_by_cldr_code, @regions_by_iso_code]
29
36
  end
30
37
 
31
38
  private
32
39
 
40
+ def construct_lookup_info(region)
41
+ pc = region.send(:parent_country)
42
+
43
+ # Remember CLDR code(s) for later use during lookup
44
+
45
+ search_code = region.cldr_code.to_s.upcase
46
+ @regions_by_cldr_code[search_code] = region if Util.present?(search_code)
47
+
48
+ @regions_by_cldr_code["#{pc.cldr_code.upcase}#{search_code}"] = region if Util.present?(pc&.cldr_code)
49
+
50
+ # Remember ISO 3166 code(s) for later use during lookup
51
+
52
+ iso_code = region.iso_code
53
+ @regions_by_iso_code[iso_code] = region if Util.present?(iso_code)
54
+
55
+ @regions_by_iso_code["#{pc.iso_code}-#{iso_code}"] = region if Util.present?(pc)
56
+
57
+ alpha_three = region.alpha_three
58
+ @regions_by_iso_code[alpha_three] = region if Util.present?(alpha_three)
59
+
60
+ numeric_three = region.numeric_three
61
+ @regions_by_iso_code[numeric_three] = region if Util.present?(numeric_three)
62
+ end
63
+
33
64
  def apply_hierarchy(parent:, code:, children:)
34
65
  current_region = find_region(code: code)
35
66
  if current_region.nil?
@@ -82,10 +113,12 @@ module Worldwide
82
113
  region.zip_requirement = spec["zip_requirement"]
83
114
  region.zip_regex = spec["zip_regex"]
84
115
  region.zips_crossing_provinces = spec["zips_crossing_provinces"]
116
+ region.name_alternates = spec["name_alternates"] || []
85
117
  end
86
118
 
87
119
  def apply_zone_attributes(region, zone)
88
120
  region.code_alternates = zone["code_alternates"] || []
121
+ region.name_alternates = zone["name_alternates"] || []
89
122
  region.example_city = zone["example_city"]
90
123
  region.neighbours = zone["neighboring_zones"]
91
124
  region.zip_prefixes = zone["zip_prefixes"] || []
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Worldwide
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: worldwide
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shopify
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-11-03 00:00:00.000000000 Z
11
+ date: 2023-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport