decoupage_administratif 0.4.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.
@@ -10,7 +10,7 @@ module DecoupageAdministratif
10
10
  # DecoupageAdministratif::Commune.find('72039')
11
11
  # DecoupageAdministratif::Region.find('52')
12
12
  def find(code)
13
- result = find_by(code: code)
13
+ result = all_by_code[code]
14
14
  if result.nil?
15
15
  raise DecoupageAdministratif::NotFoundError.new(
16
16
  "#{name.split('::').last} not found for code #{code}",
@@ -21,6 +21,16 @@ module DecoupageAdministratif
21
21
  result
22
22
  end
23
23
 
24
+ # Memoized index of all records by code
25
+ # @return [Hash]
26
+ def all_by_code
27
+ @all_by_code ||= begin
28
+ result = {}
29
+ all.each { |record| result[record.code] = record }
30
+ result
31
+ end
32
+ end
33
+
24
34
  # @param criteria [Hash] a hash with the attributes to filter by
25
35
  # @return [untyped] the element that matches the criteria
26
36
  # @example
@@ -141,7 +151,7 @@ module DecoupageAdministratif
141
151
  # @param filter_array [Array] the filter array
142
152
  # @return [Boolean] true if string options should be applied
143
153
  def apply_string_options?(item_value, filter_array)
144
- item_value.is_a?(String) && filter_array.all? { |v| v.is_a?(String) }
154
+ item_value.is_a?(String) && filter_array.all?(String)
145
155
  end
146
156
  end
147
157
  end
@@ -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,14 +60,61 @@ 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
63
68
 
69
+ def self.all_by_code
70
+ @all_by_code ||= begin
71
+ hash = {}
72
+ all.each do |commune|
73
+ # prioritize communes actuelles over communes déléguées in the result
74
+ existing_same_code = hash[commune.code]
75
+ hash[commune.code] = commune if existing_same_code.nil? || !existing_same_code.actuelle?
76
+ end
77
+ hash
78
+ end
79
+ end
80
+
64
81
  # @return [Array<Commune>] a collection of all communes _actuelles_ and municipal districts
65
82
  def self.actuelles
66
- @actuelles ||= where(commune_type: %i[commune_actuelle arrondissement_municipal])
83
+ @actuelles ||= all.select(&:actuelle?)
84
+ end
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
+
115
+ # @return [TrueClass, FalseClass]
116
+ def actuelle?
117
+ %i[commune_actuelle arrondissement_municipal].include? commune_type
67
118
  end
68
119
 
69
120
  # @raise [NotFoundError] if no region is found for the code
@@ -70,8 +70,8 @@ module DecoupageAdministratif
70
70
  return if @@departements_cache && @@communes_cache
71
71
 
72
72
  # rubocop:disable Style/ClassVars
73
- @@departements_cache = DecoupageAdministratif::Departement.all.each_with_object({}) { |dept, hash| hash[dept.code] = dept }
74
- @@communes_cache = DecoupageAdministratif::Commune.actuelles.each_with_object({}) { |commune, hash| hash[commune.code] = commune }
73
+ @@departements_cache = DecoupageAdministratif::Departement.all.to_h { |dept| [dept.code, dept] }
74
+ @@communes_cache = DecoupageAdministratif::Commune.actuelles.to_h { |commune| [commune.code, commune] }
75
75
  # rubocop:enable Style/ClassVars
76
76
  end
77
77
 
@@ -76,7 +76,7 @@ module DecoupageAdministratif
76
76
 
77
77
  def check_inclusion?(commune_insee_codes)
78
78
  epci_commune_codes = territory.membres.map { |membre| membre[:code] || membre["code"] }
79
- (epci_commune_codes & commune_insee_codes).any?
79
+ epci_commune_codes.intersect?(commune_insee_codes)
80
80
  end
81
81
 
82
82
  def calculate_insee_codes
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DecoupageAdministratif
4
- VERSION = "0.4.0"
5
- DATA_VERSION = "5.2.0"
4
+ VERSION = "0.6.0"
5
+ DATA_VERSION = "6.0.0"
6
6
  DATA_SOURCE = "@etalab/decoupage-administratif"
7
7
  end
@@ -28,6 +28,9 @@ def download_file(url, destination)
28
28
  end
29
29
  end
30
30
 
31
+ # Version of @etalab/decoupage-administratif the data is fetched from.
32
+ ETALAB_DATASET_VERSION = '6.0.0'
33
+
31
34
  namespace :decoupage_administratif do
32
35
  desc 'Update files'
33
36
  task :update do
@@ -37,7 +40,7 @@ namespace :decoupage_administratif do
37
40
 
38
41
  collection.each do |item|
39
42
  file = File.join(data_dir, "#{item}.json")
40
- url = "https://unpkg.com/@etalab/decoupage-administratif@4.0.0/data/#{item}.json"
43
+ url = "https://unpkg.com/@etalab/decoupage-administratif@#{ETALAB_DATASET_VERSION}/data/#{item}.json"
41
44
 
42
45
  download_file(url, file)
43
46
 
@@ -1,5 +1,7 @@
1
1
  module DecoupageAdministratif
2
2
  module BaseModel
3
+ def all_by_code: -> Hash[String, instance]
4
+
3
5
  def find: (String) -> untyped
4
6
  def find_by: (Hash[Symbol, untyped]) -> untyped
5
7
  def where: (**untyped) -> Array[untyped]
@@ -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,22 @@ 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
+
34
+ def actuelle?: -> bool
35
+
36
+ def all_by_code: -> Hash[String, instance]
37
+
28
38
  def region: () -> Region
29
39
 
30
40
  def departement: () -> Departement
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decoupage_administratif
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lucien Mollard
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2026-02-11 00:00:00.000000000 Z
10
+ date: 1980-01-01 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: rake
@@ -35,6 +34,7 @@ extra_rdoc_files: []
35
34
  files:
36
35
  - ".rspec"
37
36
  - ".rubocop.yml"
37
+ - ".ruby-version"
38
38
  - CHANGELOG.md
39
39
  - LICENSE.txt
40
40
  - README.md
@@ -74,7 +74,6 @@ metadata:
74
74
  homepage_uri: https://github.com/betagouv/decoupage-administratif-gem
75
75
  changelog_uri: https://github.com/betagouv/decoupage-administratif-gem/blob/main/CHANGELOG.md
76
76
  rubygems_mfa_required: 'true'
77
- post_install_message:
78
77
  rdoc_options: []
79
78
  require_paths:
80
79
  - lib
@@ -82,15 +81,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
82
81
  requirements:
83
82
  - - ">="
84
83
  - !ruby/object:Gem::Version
85
- version: 3.0.0
84
+ version: 3.3.0
86
85
  required_rubygems_version: !ruby/object:Gem::Requirement
87
86
  requirements:
88
87
  - - ">="
89
88
  - !ruby/object:Gem::Version
90
89
  version: '0'
91
90
  requirements: []
92
- rubygems_version: 3.5.11
93
- signing_key:
91
+ rubygems_version: 3.6.9
94
92
  specification_version: 4
95
93
  summary: French administrative divisions - Découpage administratif français
96
94
  test_files: []