holidays/create_defs.rb
Alex Dunae 1f11004bd7 Added merging functions in the definition builder
Added DEFINED_REGIONS output to the definition builder
2007-11-21 01:03:20 +00:00

70 lines
1.9 KiB
Ruby

require 'fastercsv'
files = ['data/ca.csv', 'data/christian.csv', 'data/us.csv']
regions = {}
rules_by_month = {}
files.each do |file|
FasterCSV.foreach(file, {:headers => :first_row, :return_headers => false, :force_quotes => true}) do |row|
month = row['month'].to_i
rules_by_month[month] = [] unless rules_by_month[month]
rule = {}
row.each do |key, val|
rule[key] = val
end
regions[rule['regions'].to_sym] = true
rule['regions'] = [rule['regions']]
# TODO: convert weeks to symbols
# added data checking
# check if this rule already exists from another region
existed = false
rules_by_month[month].each do |ex|
if ex['name'] == rule['name'] and ex['wday'] == rule['wday'] and ex['mday'] == rule['mday'] and ex['week'] == rule['week']
ex['regions'] << rule['regions'].flatten
existed = true
end
end
unless existed
rules_by_month[month] << rule
end
end
end
out = "# This file is generated by one of the Ruby Holiday gem's Rake tasks.\n"
out << "DEFINED_REGIONS = [:" + regions.keys.join(', :') + "]\n\n"
out << "HOLIDAYS_BY_MONTH = {\n"
month_strs = []
rules_by_month.each do |month, rules|
month_str = " #{month.to_s} => ["
rule_strings = []
rules.each do |rule|
str = '{'
if rule['mday']
str << ":mday => #{rule['mday']}, "
elsif rule['function']
str << ":function => #{rule['function']}, "
else
str << ":wday => #{rule['wday']}, :week => #{rule['week']}, "
end
# shouldn't allow the same region twice
str << ":name => \"#{rule['name']}\", :regions => [:" + rule['regions'].uniq.join(', :') + "]}"
rule_strings << str
end
month_str << rule_strings.join(",\n ") + "]"
month_strs << month_str
end
month_strs.join(",\n")
out << month_strs.join(",\n") + "\n}"
File.open("test_file.rb","w") do |file|
file.puts out
end