addressing 1.0.0 → 2.0.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 (76) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +36 -0
  3. data/README.md +1 -1
  4. data/data/country/af.json +1 -1
  5. data/data/country/ak.json +258 -0
  6. data/data/country/am.json +18 -18
  7. data/data/country/ba.json +258 -0
  8. data/data/country/bn-IN.json +3 -3
  9. data/data/country/bn.json +3 -3
  10. data/data/country/bs.json +1 -0
  11. data/data/country/ca.json +9 -9
  12. data/data/country/cv.json +258 -0
  13. data/data/country/cy.json +1 -1
  14. data/data/country/en-AU.json +16 -16
  15. data/data/country/en-LT.json +258 -0
  16. data/data/country/en-LV.json +258 -0
  17. data/data/country/en-UA.json +258 -0
  18. data/data/country/fil.json +1 -1
  19. data/data/country/fr-CA.json +1 -1
  20. data/data/country/ga.json +17 -17
  21. data/data/country/gd.json +2 -2
  22. data/data/country/gl.json +20 -20
  23. data/data/country/gu.json +12 -12
  24. data/data/country/hr.json +17 -17
  25. data/data/country/hy.json +2 -2
  26. data/data/country/ig.json +30 -30
  27. data/data/country/is.json +1 -1
  28. data/data/country/it.json +8 -8
  29. data/data/country/kk-Arab.json +258 -0
  30. data/data/country/kok.json +158 -158
  31. data/data/country/ky.json +3 -3
  32. data/data/country/lt.json +1 -1
  33. data/data/country/lv.json +9 -9
  34. data/data/country/mk.json +1 -1
  35. data/data/country/ml.json +1 -1
  36. data/data/country/mn.json +9 -9
  37. data/data/country/mr.json +1 -1
  38. data/data/country/or.json +14 -14
  39. data/data/country/pa.json +7 -7
  40. data/data/country/qu.json +258 -0
  41. data/data/country/rm.json +258 -0
  42. data/data/country/shn.json +258 -0
  43. data/data/country/sl.json +16 -16
  44. data/data/country/so.json +2 -2
  45. data/data/country/sr-Cyrl-BA.json +1 -1
  46. data/data/country/sr-Cyrl-ME.json +1 -1
  47. data/data/country/sr-Cyrl-XK.json +1 -1
  48. data/data/country/sr-Latn-BA.json +1 -1
  49. data/data/country/sr-Latn-ME.json +1 -1
  50. data/data/country/sr-Latn-XK.json +1 -1
  51. data/data/country/sr-Latn.json +1 -1
  52. data/data/country/sr.json +1 -1
  53. data/data/country/sw-CD.json +1 -1
  54. data/data/country/sw.json +1 -1
  55. data/data/country/ta.json +3 -3
  56. data/data/country/te.json +11 -11
  57. data/data/country/vi.json +1 -1
  58. data/data/country/yue-Hans.json +11 -11
  59. data/data/country/yue.json +10 -10
  60. data/data/subdivision/VN.json +38 -154
  61. data/lib/addressing/address.rb +66 -78
  62. data/lib/addressing/address_format.rb +19 -8
  63. data/lib/addressing/country.rb +62 -30
  64. data/lib/addressing/default_formatter.rb +33 -19
  65. data/lib/addressing/enum.rb +4 -5
  66. data/lib/addressing/exceptions.rb +29 -0
  67. data/lib/addressing/field_override.rb +6 -13
  68. data/lib/addressing/locale.rb +25 -12
  69. data/lib/addressing/model.rb +0 -6
  70. data/lib/addressing/postal_label_formatter.rb +1 -1
  71. data/lib/addressing/subdivision.rb +36 -22
  72. data/lib/addressing/version.rb +1 -1
  73. data/lib/addressing.rb +1 -8
  74. metadata +17 -6
  75. /data/data/country/{en-ID.json → en-EE.json} +0 -0
  76. /data/data/country/{en-MV.json → en-GE.json} +0 -0
@@ -1,25 +1,36 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Addressing
4
+ # Represents administrative subdivisions within countries.
5
+ #
6
+ # Subdivisions can be hierarchical with up to three levels:
7
+ # Administrative Area -> Locality -> Dependent Locality
8
+ #
9
+ # @example Get subdivisions for Brazil
10
+ # states = Addressing::Subdivision.all(['BR'])
11
+ # states.each do |code, state|
12
+ # puts "#{code}: #{state.name}"
13
+ # municipalities = state.children
14
+ # end
15
+ #
16
+ # @example Get subdivisions for a Brazilian state
17
+ # municipalities = Addressing::Subdivision.all(['BR', 'CE'])
4
18
  class Subdivision
5
19
  class << self
6
- # Subdivision definitions.
7
- @@definitions = {}
8
-
9
- # Parent subdivisions.
20
+ # Gets a Subdivision instance by ID and parent hierarchy.
10
21
  #
11
- # Used as a cache to speed up instantiating subdivisions with the same
12
- # parent. Contains only parents instead of all instantiated subdivisions
13
- # to minimize duplicating the data in $this->definitions, thus reducing
14
- # memory usage.
15
- @@parents = {}
16
-
22
+ # @param id [String] Subdivision ID
23
+ # @param parents [Array<String>] Parent hierarchy (e.g., ['BR'] or ['BR', 'CE'])
24
+ # @return [Subdivision, nil] Subdivision instance or nil if not found
17
25
  def get(id, parents)
18
26
  definitions = load_definitions(parents)
19
27
  create_subdivision_from_definitions(id, definitions)
20
28
  end
21
29
 
22
30
  # Returns all subdivision instances for the provided parents.
31
+ #
32
+ # @param parents [Array<String>] Parent hierarchy (e.g., ['BR'] or ['BR', 'CE'])
33
+ # @return [Hash<String, Subdivision>] Hash of subdivision ID => Subdivision instance
23
34
  def all(parents)
24
35
  definitions = load_definitions(parents)
25
36
  return {} if definitions.empty?
@@ -57,9 +68,10 @@ module Addressing
57
68
  grandparents = parents.dup
58
69
  parent_id = grandparents.pop
59
70
  parent_group = build_group(grandparents.dup)
71
+ @definitions ||= {}
60
72
 
61
- if @@definitions.dig(parent_group, "subdivisions", parent_id)
62
- definition = @@definitions[parent_group]["subdivisions"][parent_id]
73
+ if @definitions.dig(parent_group, "subdivisions", parent_id)
74
+ definition = @definitions[parent_group]["subdivisions"][parent_id]
63
75
  return !!definition["has_children"]
64
76
  else
65
77
  # The parent definition wasn't loaded previously, fallback to guessing based on depth.
@@ -73,12 +85,13 @@ module Addressing
73
85
 
74
86
  # Loads the subdivision definitions for the provided parents.
75
87
  def load_definitions(parents)
88
+ @definitions ||= {}
76
89
  group = build_group(parents.dup)
77
- if @@definitions.key?(group)
78
- return @@definitions[group]
90
+ if @definitions.key?(group)
91
+ return @definitions[group]
79
92
  end
80
93
 
81
- @@definitions[group] = {}
94
+ @definitions[group] = {}
82
95
 
83
96
  # If there are predefined subdivisions at this level, try to load them.
84
97
  if has_data(parents)
@@ -86,12 +99,12 @@ module Addressing
86
99
 
87
100
  if File.exist?(filename)
88
101
  raw_definition = File.read(filename)
89
- @@definitions[group] = JSON.parse(raw_definition)
90
- @@definitions[group] = process_definitions(@@definitions[group])
102
+ @definitions[group] = JSON.parse(raw_definition)
103
+ @definitions[group] = process_definitions(@definitions[group])
91
104
  end
92
105
  end
93
106
 
94
- @@definitions[group]
107
+ @definitions[group]
95
108
  end
96
109
 
97
110
  # Processes the loaded definitions.
@@ -164,13 +177,14 @@ module Addressing
164
177
  grandparents = parents.dup
165
178
  parent_id = grandparents.pop
166
179
  parent_group = build_group(grandparents.dup)
180
+ @parents ||= {}
167
181
 
168
- if !@@parents.dig(parent_group, parent_id)
169
- @@parents[parent_group] ||= {}
170
- @@parents[parent_group][parent_id] = get(parent_id, grandparents)
182
+ if !@parents.dig(parent_group, parent_id)
183
+ @parents[parent_group] ||= {}
184
+ @parents[parent_group][parent_id] = get(parent_id, grandparents)
171
185
  end
172
186
 
173
- definition["parent"] = @@parents[parent_group][parent_id]
187
+ definition["parent"] = @parents[parent_group][parent_id]
174
188
  end
175
189
 
176
190
  # Prepare children.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Addressing
4
- VERSION = "1.0.0"
4
+ VERSION = "2.0.1"
5
5
  end
data/lib/addressing.rb CHANGED
@@ -6,6 +6,7 @@ require "digest"
6
6
  require "json"
7
7
 
8
8
  # modules
9
+ require "addressing/exceptions"
9
10
  require "addressing/enum"
10
11
  require "addressing/address"
11
12
  require "addressing/address_field"
@@ -23,14 +24,6 @@ require "addressing/postal_label_formatter"
23
24
  require "addressing/subdivision"
24
25
  require "addressing/version"
25
26
 
26
- module Addressing
27
- class Error < StandardError; end
28
-
29
- class UnknownCountryError < Error; end
30
-
31
- class UnknownLocaleError < Error; end
32
- end
33
-
34
27
  if defined?(ActiveSupport.on_load)
35
28
  ActiveSupport.on_load(:active_record) do
36
29
  require "addressing/model"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: addressing
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robin van der Vleuten
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-21 00:00:00.000000000 Z
11
+ date: 2026-05-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: robinvdvleuten@gmail.com
@@ -22,12 +22,14 @@ files:
22
22
  - data/address_formats.dump
23
23
  - data/address_formats.json
24
24
  - data/country/af.json
25
+ - data/country/ak.json
25
26
  - data/country/am.json
26
27
  - data/country/ar-LY.json
27
28
  - data/country/ar-SA.json
28
29
  - data/country/ar.json
29
30
  - data/country/as.json
30
31
  - data/country/az.json
32
+ - data/country/ba.json
31
33
  - data/country/be.json
32
34
  - data/country/bg.json
33
35
  - data/country/bn-IN.json
@@ -36,6 +38,7 @@ files:
36
38
  - data/country/ca.json
37
39
  - data/country/chr.json
38
40
  - data/country/cs.json
41
+ - data/country/cv.json
39
42
  - data/country/cy.json
40
43
  - data/country/da.json
41
44
  - data/country/de-AT.json
@@ -47,8 +50,11 @@ files:
47
50
  - data/country/en-001.json
48
51
  - data/country/en-AU.json
49
52
  - data/country/en-CA.json
50
- - data/country/en-ID.json
51
- - data/country/en-MV.json
53
+ - data/country/en-EE.json
54
+ - data/country/en-GE.json
55
+ - data/country/en-LT.json
56
+ - data/country/en-LV.json
57
+ - data/country/en-UA.json
52
58
  - data/country/en.json
53
59
  - data/country/es-419.json
54
60
  - data/country/es-AR.json
@@ -96,6 +102,7 @@ files:
96
102
  - data/country/it.json
97
103
  - data/country/ja.json
98
104
  - data/country/ka.json
105
+ - data/country/kk-Arab.json
99
106
  - data/country/kk.json
100
107
  - data/country/km.json
101
108
  - data/country/ko-KP.json
@@ -122,10 +129,13 @@ files:
122
129
  - data/country/ps.json
123
130
  - data/country/pt-PT.json
124
131
  - data/country/pt.json
132
+ - data/country/qu.json
133
+ - data/country/rm.json
125
134
  - data/country/ro-MD.json
126
135
  - data/country/ro.json
127
136
  - data/country/ru-UA.json
128
137
  - data/country/ru.json
138
+ - data/country/shn.json
129
139
  - data/country/si.json
130
140
  - data/country/sk.json
131
141
  - data/country/sl.json
@@ -712,6 +722,7 @@ files:
712
722
  - lib/addressing/default_formatter.rb
713
723
  - lib/addressing/dependent_locality_type.rb
714
724
  - lib/addressing/enum.rb
725
+ - lib/addressing/exceptions.rb
715
726
  - lib/addressing/field_override.rb
716
727
  - lib/addressing/lazy_subdivisions.rb
717
728
  - lib/addressing/locale.rb
@@ -735,14 +746,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
735
746
  requirements:
736
747
  - - ">="
737
748
  - !ruby/object:Gem::Version
738
- version: '3.1'
749
+ version: '3.3'
739
750
  required_rubygems_version: !ruby/object:Gem::Requirement
740
751
  requirements:
741
752
  - - ">="
742
753
  - !ruby/object:Gem::Version
743
754
  version: '0'
744
755
  requirements: []
745
- rubygems_version: 3.5.6
756
+ rubygems_version: 3.5.22
746
757
  signing_key:
747
758
  specification_version: 4
748
759
  summary: Addressing library powered by CLDR and Google's address data
File without changes
File without changes