Class: JMACode::AreaForecastLocalE

Inherits:
Struct
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.dataObject

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

#codeObject

Returns the value of attribute code

Returns:

  • (Object)

    the current value of code



4
5
6
# File 'lib/jma_code/area_forecast_local_e.rb', line 4

def code
  @code
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



4
5
6
# File 'lib/jma_code/area_forecast_local_e.rb', line 4

def name
  @name
end

#name_phoneticObject

Returns the value of attribute name_phonetic

Returns:

  • (Object)

    the current value of name_phonetic



4
5
6
# File 'lib/jma_code/area_forecast_local_e.rb', line 4

def name_phonetic
  @name_phonetic
end

#prefecture_codeObject

Returns the value of attribute prefecture_code

Returns:

  • (Object)

    the current value of 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

.getObject



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_citiesObject



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

#prefectureObject



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_rowObject



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