@@ -9,6 +9,9 @@ class RegionsLoader
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 @@ def load_regions
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?
0 commit comments