Skip to content

Commit

Permalink
Refactor to extract :converters option
Browse files Browse the repository at this point in the history
Co-authored-by: Sutou Kouhei <[email protected]>
  • Loading branch information
tikkss and kou committed Dec 20, 2023
1 parent 851f110 commit 57fee53
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions lib/datasets/house-of-representative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ def each

open_data do |csv|
csv.each do |row|
row.fields.each_with_index do |field, idx|
row[idx] = JapaneseDateParser.new(field).parse
end
%w(議案提出会派 衆議院審議時賛成会派 衆議院審議時反対会派 議案提出者一覧 議案提出の賛成者).each do |array_column_name|
row[array_column_name] = parse_array(row[array_column_name])
end
record = Record.new(*row.fields)
yield(record)
end
Expand All @@ -82,9 +76,30 @@ def open_data
data_path = cache_dir_path + "gian.csv"
download(data_path, data_url)

japanese_date_converter = lambda do |field, info|
case info.header
when /年月日\z/
JapaneseDateParser.new(field).parse
else
field
end
end
array_converter = lambda do |field, info|
case info.header
when "議案提出会派", "衆議院審議時賛成会派", "衆議院審議時反対会派", "議案提出者一覧", "議案提出の賛成者"
parse_array(field)
else
field
end
end
File.open(data_path) do |data_file|
options = {
col_sep: ",",
headers: true,
converters: [:integer, japanese_date_converter, array_converter],
}
# There are two columns within one column. To split into two columns, `#gsub` is necessary.
yield(CSV.new(data_file.read.gsub("/", ","), col_sep: ",", headers: true, converters: %i(integer)))
yield(CSV.new(data_file.read.gsub("/", ","), **options))
end
end

Expand Down

0 comments on commit 57fee53

Please sign in to comment.