Class: JLG::Governments

Inherits:
Object
  • Object
show all
Includes:
DATA
Defined in:
lib/japanese_local_governments/governments.rb

Constant Summary

Constants included from DATA

DATA::GOV_DATA, DATA::GOV_DATA_NAME_INDEX, DATA::HEADER

Class Method Summary collapse

Class Method Details

.append_code(inputfile, outputfile = nil, pref: 'pref', name: 'name') ⇒ Object

都道府県名、自治体名を持つCSVファイルを読み込み 自治体コードを付加する

Parameters:

  • inputfile (String)

    自治体コードを付加したいCSVファイル

  • outputfile (String) (defaults to: nil)

    出力ファイル名。

  • pref (String) (defaults to: 'pref')

    都道府県カラムの名前

  • name (String) (defaults to: 'name')

    自治体名カラムの名前



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/japanese_local_governments/governments.rb', line 41

def self.append_code(inputfile, outputfile=nil, pref:'pref', name: 'name')
  if outputfile.nil?
    date = Date.today.strftime('%Y%m%d')
    outputfile = './' + File.basename(inputfile,'.*') + "_#{date}.csv"
  end
  CSV.open(inputfile,headers: true,return_headers:true) do|csv|
    CSV.open(outputfile,"wb") do |out|
      csv.each do |row|
        if row.header_row?
          out<<row.headers.to_a.insert(0,'code')
        else
          code = code_of(row[pref],row[name])
          out<<row.to_h.values.insert(0,code)
        end
      end
    end
  end
end

.code_of(pref, name = pref) ⇒ String

都道府県名、自治体名からコードを得る

Parameters:

  • pref (String)

    都道府県名

  • name (String) (defaults to: pref)

    自治体名

Returns:

  • (String)

    自治体コード。6桁。ゼロパディングあり。



24
25
26
27
28
# File 'lib/japanese_local_governments/governments.rb', line 24

def self.code_of(pref, name=pref)
  GOV_DATA[GOV_DATA_NAME_INDEX[pref][name]][:code]
rescue
  nil
end

.data_of(code) ⇒ Object

コードから自治体データを得る

Parameters:

  • code (String)

    自治体コード。6桁。ゼロパディングあり。



32
33
34
# File 'lib/japanese_local_governments/governments.rb', line 32

def self.data_of(code)
  GOV_DATA[code]
end

.list(filename = nil) ⇒ Object

地方自治体のリストを出力する

Parameters:

  • filename (String) (defaults to: nil)

    出力ファイルのパス



11
12
13
14
15
16
17
18
# File 'lib/japanese_local_governments/governments.rb', line 11

def self.list(filename=nil)
  JLG.list(filename){|out|
    out.puts HEADER.join(',')
    GOV_DATA.values.each do |data|
      out.puts data.values.join(',')
    end
  }
end