geo_states 0.1.4 → 0.1.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 23f5adf00ed40e770fefae1b152febf5de3f6cf0e3fd0db046f749040372582a
4
- data.tar.gz: 9bd7ee1dcb2378dc300d8ac94a0aa08fb76bce1d74daa0629865607efe0e27cb
3
+ metadata.gz: 8e48781ab93b649cc825612b4f963b053c0398bc7f658d33fff60857331ecf50
4
+ data.tar.gz: 749026242fb43ca2d91cc26670d8ec1670ec965468eb5e35530db729a1b888fa
5
5
  SHA512:
6
- metadata.gz: f141eeec003f3b685b15ea5f1d56c0c529f167426cba1e989bbee07df14dcd539dc50dc8b2b37ab5ca45328d54ee5a95f9f8f944f1d0c8cc044626f56c8e1096
7
- data.tar.gz: 0ea2455867a0d1e41eb3e82c2c5630aadb5b54fc62043af7c8bbd5ffe948d6f1f15d16ec41839f3ee97ec6e0f7c3351348d314d3f329e0f8d82b34a8472c8ceb
6
+ metadata.gz: 36318233ab28afa96f3174ad0a893c9966e100b78790739758bdd49d69958d7a5937d8a68e446d76e52881d9ed3291286388c59593f9eda34722f81e0b0002d8
7
+ data.tar.gz: 1c63dd25eaba4ff9917b62d4ac9d10523e2da4c6e486ae8f77d39dbfd5bcb43c0898b9022cade367cdc43909b157e33f5daa7239942dc8c34f4b00f82b876400
data/README.md CHANGED
@@ -107,15 +107,17 @@ Example `config/geo_states.yml`:
107
107
 
108
108
  ```yaml
109
109
  MX:
110
- state_aliases:
111
- CDMX: DF
112
- DF: DF
113
- DISTRITO FEDERAL: DF
114
- CIUDAD DE MEXICO: DF
115
- MEXICO CITY: DF
110
+ CDMX: DF
111
+ DF: DF
112
+ DISTRITO FEDERAL: DF
113
+ CIUDAD DE MEXICO: DF
114
+ MEXICO CITY: DF
115
+ _lookup:
116
+ DF: CMX
116
117
  ```
117
118
 
118
- Values map to the `state_code` in the bundled data. For Mexico City the data uses `CMX`; `DF` is a built-in alias that maps to `CMX`. So `CDMX` → `DF` → `CMX` works when you have the config.
119
+ - Aliases map to your preferred `state_code` (e.g. `DF`).
120
+ - `_lookup` maps that preferred value to the actual `state_code` in the bundled data when they differ (e.g. bundled data has `CMX` for Mexico City).
119
121
 
120
122
  See `config/geo_states.example.yml` for a copy-paste template.
121
123
 
@@ -2,13 +2,14 @@
2
2
  # Copy to config/geo_states.yml or geo_states.yml in your project root.
3
3
  # Or set GEO_STATES_CONFIG_PATH to the config file path.
4
4
  #
5
- # Values must map to state_code in bundled data (e.g. CMX for Mexico City).
6
- # DF is supported as built-in alias for Mexico City (CMX).
5
+ # Aliases map to preferred state_code. Use _lookup when preferred isn't in bundled data.
6
+ # Example: Mexico City has CMX in data; user wants DF in output.
7
7
 
8
8
  MX:
9
- state_aliases:
10
- CDMX: DF
11
- DF: DF
12
- DISTRITO FEDERAL: DF
13
- CIUDAD DE MEXICO: DF
14
- MEXICO CITY: DF
9
+ CDMX: DF
10
+ DF: DF
11
+ DISTRITO FEDERAL: DF
12
+ CIUDAD DE MEXICO: DF
13
+ MEXICO CITY: DF
14
+ _lookup:
15
+ DF: CMX
@@ -25,26 +25,50 @@ module GeoStates
25
25
  @state_aliases = nil
26
26
  end
27
27
 
28
- # Resolve state identifier via user config. Returns canonical state_code or nil.
28
+ # Resolve state identifier via user config. Returns alias value or nil.
29
29
  # @param country_iso2 [String] e.g. "MX"
30
30
  # @param state_id [String] e.g. "CDMX", "DF"
31
- # @return [String, nil] resolved state_code (e.g. "CMX") or nil
31
+ # @return [String, nil] resolved value (e.g. "DF") or nil
32
32
  def resolve_alias(country_iso2, state_id)
33
33
  return nil if country_iso2.nil? || state_id.nil?
34
34
 
35
35
  iso2 = country_iso2.to_s.upcase
36
36
  id = state_id.to_s.strip
37
37
 
38
- aliases = state_aliases[iso2]
38
+ map = alias_map_for(iso2)
39
+ return nil unless map
40
+
41
+ val = map[id] || map[id.upcase]
42
+ val.to_s if val
43
+ end
44
+
45
+ # Resolve preferred/alias to actual state_code for lookup when it's not in bundled data.
46
+ # E.g. _lookup: { DF: CMX } — DF (user preference) maps to CMX (in data).
47
+ # @param country_iso2 [String] e.g. "MX"
48
+ # @param preferred [String] e.g. "DF"
49
+ # @return [String, nil] lookup key for bundled data, or nil
50
+ def resolve_lookup(country_iso2, preferred)
51
+ return nil if country_iso2.nil? || preferred.nil?
52
+
53
+ aliases = state_aliases[country_iso2.to_s.upcase]
54
+ return nil unless aliases.is_a?(Hash)
55
+
56
+ lookup = aliases['_lookup'] || aliases['lookup']
57
+ return nil unless lookup.is_a?(Hash)
58
+
59
+ val = lookup[preferred] || lookup[preferred.to_s.upcase]
60
+ val.to_s if val
61
+ end
62
+
63
+ # Alias map for a country (excludes _lookup / lookup).
64
+ def alias_map_for(country_iso2)
65
+ aliases = state_aliases[country_iso2.to_s.upcase]
39
66
  return nil unless aliases.is_a?(Hash)
40
67
 
41
- # Support both flat and nested structure:
42
- # MX: { CDMX: CMX } or MX: { state_aliases: { CDMX: CMX } }
43
68
  map = aliases['state_aliases'] || aliases
44
69
  return nil unless map.is_a?(Hash)
45
70
 
46
- val = map[id] || map[id.upcase]
47
- val.to_s if val
71
+ map.except('_lookup', 'lookup')
48
72
  end
49
73
 
50
74
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GeoStates
4
- VERSION = '0.1.4'
4
+ VERSION = '0.1.5'
5
5
  end
data/lib/geo_states.rb CHANGED
@@ -90,7 +90,8 @@ module GeoStates
90
90
  state
91
91
  end
92
92
 
93
- # Resolve state identifier: user config first, then built-in aliases (e.g. DF -> CMX for Mexico).
93
+ # Resolve state identifier: user config aliases. Use _lookup in config to map
94
+ # preferred values (e.g. DF) to actual state_code in bundled data (e.g. CMX).
94
95
  # Returns [lookup_key, preferred_state_code, from_alias].
95
96
  # @param iso2 [String] country ISO2
96
97
  # @param state_id [String] user input
@@ -100,16 +101,11 @@ module GeoStates
100
101
  return [nil, nil, false] if id.empty?
101
102
 
102
103
  from_config = ConfigLoader.resolve_alias(iso2, id)
103
- canonical = from_config || id
104
-
105
- # Built-in: Mexico DF -> CMX (bundled data uses CMX; user config may map CDMX => DF)
106
- if iso2.to_s.upcase == 'MX' && canonical.to_s.upcase == 'DF'
107
- return ['CMX', 'DF', true]
108
- end
109
-
110
- # When from config, use that as preferred and mark as from_alias
104
+ preferred = from_config || id
111
105
  from_alias = !from_config.nil?
112
- preferred = from_config || canonical
113
- [canonical, preferred, from_alias]
106
+
107
+ # When preferred isn't in bundled data, check _lookup (e.g. DF => CMX for MX)
108
+ lookup_key = ConfigLoader.resolve_lookup(iso2, preferred) || preferred
109
+ [lookup_key, preferred, from_alias]
114
110
  end
115
111
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geo_states
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alberto Osnaya (@elosnaya)
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-02-20 00:00:00.000000000 Z
11
+ date: 2026-02-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Lists countries and their states (or provinces, regions) in a consistent
14
14
  format. Data is bundled with the gem.