decoupage_administratif 0.5.0 → 0.6.0

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: 6e20eab2bf66bb533a1ec049858e0c0191eb82d03010c9f50fd518bb839a7a30
4
- data.tar.gz: 6ff84f46bf003fa476825b10e688e964f9b42df0f3954ef9a3ae648f99942450
3
+ metadata.gz: 7c1f04c996f56a0efc901c6010341d5c14418c0e76fcdedcab4300de819cb14f
4
+ data.tar.gz: aa4634401f09659fbdc49264e148abd3ad1e21d6408c48e414101522267ef658
5
5
  SHA512:
6
- metadata.gz: 8d5ff05eec823b9cc156d006b75900e95c9c2bccc303872534906f75fee34d404d9e6d41ab3850d0be3b59b7b7334be4462c6c1e8f3b14e7fd37ba9d0e228611
7
- data.tar.gz: 16df16c8519aae8074bfe81fba4f54a4a432dfde277c63f4014fc6c0cfd29d556f99ba16d57717280dacf5fb8b2bed99a29ad6b4ed03f8feebd6d068c5959b1f
6
+ metadata.gz: a2e6c7d57bc6cf4f5542a70c001c68d7507e7fd747e28142b1fde7262a5920c0568e232a0ada3ca33d34bb28426648f19acb6108e054005d68510ebc83eed9b9
7
+ data.tar.gz: 1335fa9dfecde7a8b2b6c7daf5eaf44aa52d2e3f6859b51fbbd16964782492f2766973f8176a14d5dfe8660466f9ff40c5d41a4c76ae52bd0d4d81c32a8e17c6
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.6.0] - 2026-11-16
4
+
5
+ ### Added
6
+ - **Commune**: `anciens_codes` attribute exposing the former INSEE codes of the communes merged into a commune, read from the `anciensCodes` field of the source data
7
+ - **Commune**: `Commune.find_actuelle(code)` returns the commune actuelle for a current or former INSEE code (e.g. `Commune.find_actuelle('72287')` returns Laigné-Saint-Gervais, `72155`), and raises `NotFoundError` when nothing matches. `Commune.find` is unchanged and still rejects obsolete codes
8
+
3
9
  ## [0.5.0] - 2026-09-28
4
10
 
5
11
  ### Added
data/README.md CHANGED
@@ -67,6 +67,12 @@ DecoupageAdministratif::Region.all
67
67
  commune = DecoupageAdministratif::Commune.find('75056')
68
68
  puts commune.nom # => "Paris"
69
69
 
70
+ # Retrouver la commune actuelle à partir d'un code INSEE actuel ou ancien
71
+ # (find lève NotFoundError pour un code obsolète, find_actuelle renvoie la commune qui l'a absorbé)
72
+ commune = DecoupageAdministratif::Commune.find_actuelle('72287')
73
+ puts commune.nom # => "Laigné-Saint-Gervais"
74
+ commune.anciens_codes # => ["72287"]
75
+
70
76
  # Lister toutes les communes actuelles (communes actuelles + arrondissements municipaux)
71
77
  DecoupageAdministratif::Commune.actuelles
72
78
 
@@ -234,6 +240,12 @@ DecoupageAdministratif::Region.all
234
240
  commune = DecoupageAdministratif::Commune.find('75056')
235
241
  puts commune.nom # => "Paris"
236
242
 
243
+ # Find the current municipality from a current or former INSEE code
244
+ # (find raises NotFoundError for an obsolete code, find_actuelle returns the municipality that absorbed it)
245
+ commune = DecoupageAdministratif::Commune.find_actuelle('72287')
246
+ puts commune.nom # => "Laigné-Saint-Gervais"
247
+ commune.anciens_codes # => ["72287"]
248
+
237
249
  # List all current municipalities (current communes + municipal districts)
238
250
  DecoupageAdministratif::Commune.actuelles
239
251
 
@@ -24,7 +24,9 @@ module DecoupageAdministratif
24
24
  # @note Default value is :commune_actuelle
25
25
  # @!attribute [r] codes_postaux
26
26
  # @return [Array<String>] Postal codes of the commune
27
- attr_reader :code, :nom, :zone, :region_code, :departement_code, :commune_type, :codes_postaux
27
+ # @!attribute [r] anciens_codes
28
+ # @return [Array<String>] Former INSEE codes of the communes merged into this one
29
+ attr_reader :code, :nom, :zone, :region_code, :departement_code, :commune_type, :codes_postaux, :anciens_codes
28
30
 
29
31
  # rubocop:disable Metrics/ParameterLists
30
32
  # @param code [String] the INSEE code of the commune
@@ -34,8 +36,9 @@ module DecoupageAdministratif
34
36
  # @param departement_code [String] the INSEE code of the department
35
37
  # @param commune_type [Symbol] the type of the commune (default: :commune_actuelle)
36
38
  # @param codes_postaux [Array<String>] the postal codes of the commune (default: [])
39
+ # @param anciens_codes [Array<String>] the former INSEE codes merged into the commune (default: [])
37
40
  def initialize(code:, nom:, zone:, region_code:, departement_code:, commune_type: :commune_actuelle,
38
- codes_postaux: [])
41
+ codes_postaux: [], anciens_codes: [])
39
42
  @code = code
40
43
  @nom = nom
41
44
  @zone = zone
@@ -43,6 +46,7 @@ module DecoupageAdministratif
43
46
  @departement_code = departement_code
44
47
  @commune_type = commune_type
45
48
  @codes_postaux = codes_postaux
49
+ @anciens_codes = anciens_codes
46
50
  end
47
51
  # rubocop:enable Metrics/ParameterLists
48
52
 
@@ -56,7 +60,8 @@ module DecoupageAdministratif
56
60
  region_code: commune_data["region"],
57
61
  departement_code: commune_data["departement"],
58
62
  commune_type: commune_data["type"]&.gsub("-", "_")&.to_sym,
59
- codes_postaux: commune_data["codesPostaux"] || []
63
+ codes_postaux: commune_data["codesPostaux"] || [],
64
+ anciens_codes: commune_data["anciensCodes"] || []
60
65
  )
61
66
  end
62
67
  end
@@ -78,6 +83,35 @@ module DecoupageAdministratif
78
83
  @actuelles ||= all.select(&:actuelle?)
79
84
  end
80
85
 
86
+ # Find the commune actuelle for a current or former INSEE code.
87
+ # Unlike {find}, it accepts the code of a commune that has since been merged into another one.
88
+ # @param code [String] a current or former INSEE code
89
+ # @return [Commune] the commune actuelle (or municipal district) with this code, otherwise the one that absorbed it
90
+ # @raise [NotFoundError] if no commune actuelle matches the code
91
+ # @example
92
+ # DecoupageAdministratif::Commune.find_actuelle('72287').nom # => "Laigné-Saint-Gervais"
93
+ def self.find_actuelle(code)
94
+ commune = actuelles_by_any_code[code]
95
+ raise NotFoundError.new("Commune not found for code #{code}", code: code) if commune.nil?
96
+
97
+ commune
98
+ end
99
+
100
+ # Communes actuelles indexed by their current INSEE code and by each of their former codes.
101
+ # When a code is both the current code of a commune and a former code of another one,
102
+ # the commune whose current code it is wins.
103
+ # @return [Hash{String => Commune}]
104
+ def self.actuelles_by_any_code
105
+ @actuelles_by_any_code ||= begin
106
+ by_ancien_code = actuelles.flat_map do |commune|
107
+ commune.anciens_codes.map { |ancien_code| [ancien_code, commune] }
108
+ end.to_h
109
+ by_code = actuelles.to_h { |commune| [commune.code, commune] }
110
+ by_ancien_code.merge(by_code)
111
+ end
112
+ end
113
+ private_class_method :actuelles_by_any_code
114
+
81
115
  # @return [TrueClass, FalseClass]
82
116
  def actuelle?
83
117
  %i[commune_actuelle arrondissement_municipal].include? commune_type
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DecoupageAdministratif
4
- VERSION = "0.5.0"
4
+ VERSION = "0.6.0"
5
5
  DATA_VERSION = "6.0.0"
6
6
  DATA_SOURCE = "@etalab/decoupage-administratif"
7
7
  end
@@ -10,6 +10,7 @@ module DecoupageAdministratif
10
10
  attr_reader departement_code: String
11
11
  attr_reader commune_type: Symbol
12
12
  attr_reader codes_postaux: Array[String]
13
+ attr_reader anciens_codes: Array[String]
13
14
 
14
15
  def initialize: (
15
16
  code: String,
@@ -18,13 +19,18 @@ module DecoupageAdministratif
18
19
  region_code: String,
19
20
  departement_code: String,
20
21
  ?commune_type: Symbol,
21
- ?codes_postaux: Array[String]
22
+ ?codes_postaux: Array[String],
23
+ ?anciens_codes: Array[String]
22
24
  ) -> void
23
25
 
24
26
  def self.all: () -> Array[Commune]
25
27
 
26
28
  def self.actuelles: () -> Array[Commune]
27
29
 
30
+ def self.find_actuelle: (String code) -> Commune
31
+
32
+ private def self.actuelles_by_any_code: () -> Hash[String, Commune]
33
+
28
34
  def actuelle?: -> bool
29
35
 
30
36
  def all_by_code: -> Hash[String, instance]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decoupage_administratif
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lucien Mollard