jma_code 0.0.3 → 0.0.4

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: 6f70649fc434ee1ccde5bcc78c5ea605b5efe485a90dffbb03cd61b21bc3d288
4
- data.tar.gz: 8deba1d4d6591f77cb209d1d59a894abec00c7c1ee3a768f8e6f0495d4f43610
3
+ metadata.gz: bbdf42a7d23265f5ef359b28bf5c6d44fe2effb7d73cd620b3a6f38c1339ebd2
4
+ data.tar.gz: 269adeeb6213bf011b44a35c20872e7cde18600f4e038d8c2d60cbb5380d65a1
5
5
  SHA512:
6
- metadata.gz: 2619f3d4a9975ddcecb7b6ce36ec629212d44d73a4b9be69e79a2d190f6f390cd4f161907b36e8d39c62d4e08cdd43c96e074c7e58e0fdf63331fdde13d52f36
7
- data.tar.gz: 8d75d838146264b051a82339e809ff463d1289f0f423acce1586dba6339af2863b0ed625fb667b338fa829a65111053b76484a6dd3dc020348698d921f737fec
6
+ metadata.gz: 8b24c689d7f69eb67e27b04a460895104e7a18a32afce639dbbc9788a224a20fd7c6f7a2e5ec0bdac16e5c45da4ca2452c3ebe3c4b8b246e89451513424bfdff
7
+ data.tar.gz: acd1941675e1c09ec4d8f8e47707586a53f41d2d95bfd4bd9f4b447252d7f32d0ac46d18ad650b10fbffc5dfcf98db93d09df1f6256a5f35ac3a37472360915d
@@ -92,6 +92,67 @@ module JMACode
92
92
  used_by: used_by_fields.select{|f| row[f] == '1'}.map{|f| f.to_s.sub(/\Aused_by_/, '').to_sym}
93
93
  )
94
94
  end
95
+
96
+ def build_tree(areas=nil, cities=nil)
97
+ areas ||= get
98
+ cities ||= JMACode::AreaInformationCity.get
99
+
100
+ toplevels, areas = areas.partition{|a| a.any_belonging_locals.blank?}
101
+ pref_areas = areas.group_by(&:prefecture_code)
102
+ pref_cities = cities.group_by(&:prefecture_code)
103
+ toplevels.group_by(&:prefecture).map{|pref, pref_toplevels|
104
+ current_areas = pref_areas[pref.code]
105
+ current_cities = pref_cities[pref.code]
106
+
107
+ [
108
+ block_given? ? yield(pref) : pref,
109
+ pref_toplevels.map{|t|
110
+ secondlevels = (current_areas+current_cities).select{|a| a.child_of?(t)}
111
+ secondlevels_children = secondlevels.map{|s|
112
+ thirdlevels = (current_areas+current_cities).select{|a| a.child_of?(s)}
113
+ thirdlevels_children = thirdlevels.map{|th|
114
+ forthlevels = current_cities.select{|c| c.child_of?(th)}
115
+ [
116
+ block_given? ? yield(th) : th,
117
+ forthlevels.map{|f|
118
+ [block_given? ? yield(f) : f, nil]
119
+ }
120
+ ]
121
+ }
122
+ [
123
+ block_given? ? yield(s) : s,
124
+ thirdlevels_children
125
+ ]
126
+ }
127
+ [
128
+ block_given? ? yield(t) : t,
129
+ secondlevels_children
130
+ ]
131
+ }
132
+ ]
133
+
134
+ }
135
+ end
136
+
137
+ def walk_tree(tree, &block)
138
+ tree.map do |area, children|
139
+ a = block.call(area)
140
+ c = if children.is_a?(Array) and children.present?
141
+ walk_tree(children, &block)
142
+ else
143
+ children
144
+ end
145
+ [a, c]
146
+ end
147
+ end
148
+ end
149
+
150
+ def prefecture_code
151
+ @prefecture_code ||= code[0, 2]
152
+ end
153
+
154
+ def prefecture
155
+ @prefecture ||= Prefecture.get.find{|pref| pref.code == prefecture_code}
95
156
  end
96
157
 
97
158
  def area_information_cities
@@ -113,7 +174,20 @@ module JMACode
113
174
  end
114
175
 
115
176
  def any_belonging_locals
116
- [belonging_local_in_weather_alert, belonging_local_in_tornado_alert].compact.uniq(&:code)
177
+ @any_belonging_locals ||= [belonging_local_in_weather_alert, belonging_local_in_tornado_alert].compact.uniq(&:code)
178
+ end
179
+
180
+ def any_ancestry_locals
181
+ @any_ancestry_locals ||= [
182
+ belonging_local_in_weather_alert,
183
+ belonging_local_in_tornado_alert,
184
+ belonging_local_in_weather_alert&.belonging_local_in_weather_alert,
185
+ belonging_local_in_tornado_alert&.belonging_local_in_tornado_alert,
186
+ ].compact.uniq(&:code)
187
+ end
188
+
189
+ def child_of?(area_or_city)
190
+ any_belonging_locals.map(&:code).include?(area_or_city.code)
117
191
  end
118
192
 
119
193
  def to_csv_row
@@ -3,7 +3,7 @@ module JMACode
3
3
  using Blank
4
4
 
5
5
  class AreaInformationCity < Struct.new(
6
- :code, :name, :alt_name, :alt_name_phonetic,
6
+ :code, :long_name, :alt_name, :alt_name_phonetic,
7
7
  :area_forecast_local_code, :used_by,
8
8
  keyword_init: true
9
9
  )
@@ -11,24 +11,24 @@ module JMACode
11
11
  NUM_HEADER_ROWS = 3
12
12
  HEADERS = %i(
13
13
  code
14
- name
14
+ long_name
15
15
  name_used_by_weather
16
16
  name_phonetic_used_by_weather
17
17
  area_forecast_local_code
18
- used_by_weather_alert
19
- used_by_tornado_alert
20
- used_by_storm_surge_alert
21
- used_by_high_wave_alert
22
- used_by_landslide_alert
23
- used_by_flood_alert
18
+ used_by_weather
19
+ used_by_tornado
20
+ used_by_storm_surge
21
+ used_by_high_wave
22
+ used_by_landslide
23
+ used_by_flood
24
24
  name_used_by_earthquake
25
25
  name_phonetic_used_by_earthquake
26
26
  name_used_by_volcano
27
27
  name_phonetic_used_by_volcano
28
28
  name_used_by_uv
29
29
  name_phonetic_used_by_uv
30
- name_used_by_rainstorm_alert
31
- name_phonetic_used_by_rainstorm_alert
30
+ name_used_by_rainstorm
31
+ name_phonetic_used_by_rainstorm
32
32
  )
33
33
 
34
34
  class << self
@@ -63,36 +63,62 @@ module JMACode
63
63
  [row[:name_used_by_volcano], row[:name_phonetic_used_by_volcano]]
64
64
  elsif row[:name_used_by_uv].present?
65
65
  [row[:name_used_by_uv], row[:name_phonetic_used_by_uv]]
66
- elsif row[:name_used_by_rainstorm_alert].present?
67
- [row[:name_used_by_rainstorm_alert], row[:name_phonetic_used_by_rainstorm_alert]]
66
+ elsif row[:name_used_by_rainstorm].present?
67
+ [row[:name_used_by_rainstorm], row[:name_phonetic_used_by_rainstorm]]
68
68
  else
69
69
  []
70
70
  end
71
71
 
72
72
  new(
73
73
  code: row[:code],
74
- name: row[:name],
74
+ long_name: row[:long_name],
75
75
  alt_name: alt_name,
76
76
  alt_name_phonetic: alt_name_phonetic,
77
77
  area_forecast_local_code: row[:area_forecast_local_code],
78
78
  used_by: [
79
- row[:used_by_weather_alert] == '1' ? :weather_alert : nil,
80
- row[:used_by_tornado_alert] == '1' ? :tornado_alert : nil,
81
- row[:used_by_storm_surge_alert] == '1' ? :storm_surge_alert : nil,
82
- row[:used_by_high_wave_alert] == '1' ? :high_wave_alert : nil,
83
- row[:used_by_landslide_alert] == '1' ? :landslide_alert : nil,
84
- row[:used_by_flood_alert] == '1' ? :flood_alert : nil,
79
+ row[:used_by_weather] == '1' ? :weather : nil,
80
+ row[:used_by_tornado] == '1' ? :tornado : nil,
81
+ row[:used_by_storm_surge] == '1' ? :storm_surge : nil,
82
+ row[:used_by_high_wave] == '1' ? :high_wave : nil,
83
+ row[:used_by_landslide] == '1' ? :landslide : nil,
84
+ row[:used_by_flood] == '1' ? :flood : nil,
85
85
  row[:name_used_by_earthquake].present? ? :earthquake : nil,
86
86
  row[:name_used_by_volcano].present? ? :volcano : nil,
87
87
  row[:name_used_by_uv].present? ? :uv : nil,
88
- row[:name_used_by_rainstorm_alert].present? ? :rainstorm_alert : nil,
88
+ row[:name_used_by_rainstorm].present? ? :rainstorm : nil,
89
89
  ].compact
90
90
  )
91
91
  end
92
92
  end
93
93
 
94
+ def name
95
+ alt_name.presence || long_name
96
+ end
97
+
98
+ def name_phonetic
99
+ alt_name_phonetic
100
+ end
101
+
102
+ def prefecture_code
103
+ @prefecture_code ||= code[0, 2]
104
+ end
105
+
94
106
  def area_forecast_local
95
107
  @area_forecast_local ||= AreaForecastLocal.get.find{|x| x.code == area_forecast_local_code}
96
108
  end
109
+
110
+ def child_of?(area_or_city)
111
+ area_forecast_local_code == area_or_city.code
112
+ end
113
+
114
+ def to_h
115
+ {
116
+ code: code,
117
+ name: name,
118
+ name_phonetic: name_phonetic,
119
+ area_forecast_local_code: area_forecast_local_code,
120
+ used_by: used_by,
121
+ }
122
+ end
97
123
  end
98
124
  end
@@ -24,7 +24,7 @@ module JMACode
24
24
  [18, '福井県', '福井', 'fukui'],
25
25
  [19, '山梨県', '山梨', 'yamanashi'],
26
26
  [20, '長野県', '長野', 'nagano'],
27
- [20, '岐阜県', '岐阜', 'gifu'],
27
+ [21, '岐阜県', '岐阜', 'gifu'],
28
28
  [22, '静岡県', '静岡', 'shizuoka'],
29
29
  [23, '愛知県', '愛知', 'aichi'],
30
30
  [24, '三重県', '三重', 'mie'],
@@ -1,3 +1,3 @@
1
1
  module JMACode
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jma_code
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - metheglin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-05 00:00:00.000000000 Z
11
+ date: 2024-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake