2007-11-21 01:13:18 +01:00
|
|
|
require 'fastercsv'
|
|
|
|
|
2007-11-21 02:03:20 +01:00
|
|
|
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]
|
2007-11-21 01:13:18 +01:00
|
|
|
|
2007-11-21 02:03:20 +01:00
|
|
|
rule = {}
|
|
|
|
row.each do |key, val|
|
|
|
|
rule[key] = val
|
|
|
|
end
|
|
|
|
regions[rule['regions'].to_sym] = true
|
|
|
|
rule['regions'] = [rule['regions']]
|
2007-11-21 01:13:18 +01:00
|
|
|
|
2007-11-21 02:03:20 +01:00
|
|
|
# TODO: convert weeks to symbols
|
|
|
|
# added data checking
|
2007-11-21 01:13:18 +01:00
|
|
|
|
2007-11-21 02:03:20 +01:00
|
|
|
# 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
|
2007-11-21 01:13:18 +01:00
|
|
|
|
2007-11-21 02:03:20 +01:00
|
|
|
end
|
|
|
|
unless existed
|
|
|
|
rules_by_month[month] << rule
|
|
|
|
end
|
2007-11-21 01:13:18 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-11-21 02:03:20 +01:00
|
|
|
out = "# This file is generated by one of the Ruby Holiday gem's Rake tasks.\n"
|
|
|
|
out << "DEFINED_REGIONS = [:" + regions.keys.join(', :') + "]\n\n"
|
|
|
|
|
2007-11-21 01:13:18 +01:00
|
|
|
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']}, "
|
2007-11-21 02:03:20 +01:00
|
|
|
elsif rule['function']
|
|
|
|
str << ":function => #{rule['function']}, "
|
2007-11-21 01:13:18 +01:00
|
|
|
else
|
|
|
|
str << ":wday => #{rule['wday']}, :week => #{rule['week']}, "
|
|
|
|
end
|
|
|
|
|
2007-11-21 02:03:20 +01:00
|
|
|
# shouldn't allow the same region twice
|
|
|
|
str << ":name => \"#{rule['name']}\", :regions => [:" + rule['regions'].uniq.join(', :') + "]}"
|
2007-11-21 01:13:18 +01:00
|
|
|
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
|