Class: JMACode::AreaForecastLocalE
- Inherits:
-
Struct
- Object
- Struct
- JMACode::AreaForecastLocalE
- Defined in:
- lib/jma_code/area_forecast_local_e.rb
Constant Summary collapse
- CSV_ROW_SEP =
"\r\n"
- NUM_HEADER_ROWS =
1
- HEADERS =
%i( code name name_phonetic prefecture_code )
Class Attribute Summary collapse
-
.data ⇒ Object
Returns the value of attribute data.
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#name ⇒ Object
Returns the value of attribute name.
-
#name_phonetic ⇒ Object
Returns the value of attribute name_phonetic.
-
#prefecture_code ⇒ Object
Returns the value of attribute prefecture_code.
Class Method Summary collapse
- .build_by_csv_row(row) ⇒ Object
- .build_tree(areas = nil, cities = nil) ⇒ Object
- .get ⇒ Object
- .load(**args) ⇒ Object
- .load_csv(version: "20240618-completed") ⇒ Object
- .walk_tree(tree, &block) ⇒ Object
Instance Method Summary collapse
Class Attribute Details
.data ⇒ Object
Returns the value of attribute data.
18 19 20 |
# File 'lib/jma_code/area_forecast_local_e.rb', line 18 def data @data end |
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code
4 5 6 |
# File 'lib/jma_code/area_forecast_local_e.rb', line 4 def code @code end |
#name ⇒ Object
Returns the value of attribute name
4 5 6 |
# File 'lib/jma_code/area_forecast_local_e.rb', line 4 def name @name end |
#name_phonetic ⇒ Object
Returns the value of attribute name_phonetic
4 5 6 |
# File 'lib/jma_code/area_forecast_local_e.rb', line 4 def name_phonetic @name_phonetic end |
#prefecture_code ⇒ Object
Returns the value of attribute prefecture_code
4 5 6 |
# File 'lib/jma_code/area_forecast_local_e.rb', line 4 def prefecture_code @prefecture_code end |
Class Method Details
.build_by_csv_row(row) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/jma_code/area_forecast_local_e.rb', line 40 def build_by_csv_row(row) new( code: row[:code], name: row[:name], name_phonetic: row[:name_phonetic], prefecture_code: row[:prefecture_code], ) end |
.build_tree(areas = nil, cities = nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/jma_code/area_forecast_local_e.rb', line 49 def build_tree(areas=nil, cities=nil) areas ||= get cities ||= JMACode::AreaInformationCity.get areas.group_by(&:prefecture).map{|pref, pref_areas| [ block_given? ? yield(pref) : pref, pref_areas.map{|a| [ block_given? ? yield(a) : a, a.area_information_cities.map{|c| [ block_given? ? yield(c) : c, nil ] } ] } ] } end |
.get ⇒ Object
36 37 38 |
# File 'lib/jma_code/area_forecast_local_e.rb', line 36 def get @data ||= load end |
.load(**args) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/jma_code/area_forecast_local_e.rb', line 28 def load(**args) load_csv(**args) do |csv| csv.drop(NUM_HEADER_ROWS).map do |row| build_by_csv_row(row) end end end |
.load_csv(version: "20240618-completed") ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/jma_code/area_forecast_local_e.rb', line 20 def load_csv(version: "20240618-completed") path = File.join(File.dirname(__FILE__), "../../data/#{version}_Volcano-Earthquake/AreaForecastLocalE.csv") File.open(path) do |f| csv = CSV.new(f, headers: HEADERS, row_sep: CSV_ROW_SEP) yield(csv) end end |
.walk_tree(tree, &block) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/jma_code/area_forecast_local_e.rb', line 71 def walk_tree(tree, &block) tree.map do |area, children| a = block.call(area) c = if children.is_a?(Array) and children.present? walk_tree(children, &block) else children end [a, c] end end |
Instance Method Details
#area_information_cities ⇒ Object
88 89 90 |
# File 'lib/jma_code/area_forecast_local_e.rb', line 88 def area_information_cities @area_information_cities ||= AreaInformationCity.get.select{|x| x.area_forecast_local_e_code == code} end |
#prefecture ⇒ Object
84 85 86 |
# File 'lib/jma_code/area_forecast_local_e.rb', line 84 def prefecture @prefecture ||= Prefecture.get.find{|pref| pref.code == prefecture_code} end |
#to_csv_row ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/jma_code/area_forecast_local_e.rb', line 92 def to_csv_row HEADERS.map do |k| respond_to?(k) ? public_send(k) : nil end end |