Added merging functions in the definition builder
Added DEFINED_REGIONS output to the definition builder
This commit is contained in:
parent
d98a443337
commit
1f11004bd7
4 changed files with 57 additions and 23 deletions
|
@ -1,17 +1,10 @@
|
|||
require 'fastercsv'
|
||||
|
||||
# ==== Rows
|
||||
# 0 -> month
|
||||
# 1 -> region
|
||||
# 2 -> mday
|
||||
# 3 -> wday
|
||||
# 4 -> week
|
||||
# 5 -> name
|
||||
|
||||
|
||||
files = ['data/ca.csv', 'data/christian.csv', 'data/us.csv']
|
||||
regions = {}
|
||||
rules_by_month = {}
|
||||
|
||||
FasterCSV.foreach('data/us.csv', {:headers => :first_row, :return_headers => false, :force_quotes => true}) do |row|
|
||||
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]
|
||||
|
||||
|
@ -19,11 +12,30 @@ FasterCSV.foreach('data/us.csv', {:headers => :first_row, :return_headers => fal
|
|||
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 Holiday gem's Rake tasks.\n"
|
||||
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"
|
||||
|
||||
|
||||
|
@ -35,11 +47,14 @@ rules_by_month.each do |month, rules|
|
|||
str = '{'
|
||||
if rule['mday']
|
||||
str << ":mday => #{rule['mday']}, "
|
||||
elsif rule['function']
|
||||
str << ":function => #{rule['function']}, "
|
||||
else
|
||||
str << ":wday => #{rule['wday']}, :week => #{rule['week']}, "
|
||||
end
|
||||
|
||||
str << ":name => \"#{rule['name']}\", :regions => [:#{rule['region']}]}"
|
||||
# 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 ") + "]"
|
||||
|
|
7
data/ca.csv
Normal file
7
data/ca.csv
Normal file
|
@ -0,0 +1,7 @@
|
|||
month,regions,function,mday,wday,week,name
|
||||
1,ca,,1,,,"New Year's Day"
|
||||
7,ca,,1,,,"Canada Day"
|
||||
9,ca,,,1,1,"Labour Day"
|
||||
11,ca,,11,,,"Rememberance Day"
|
||||
12,ca,,25,,,"Christmas Day"
|
||||
12,ca,,26,,,"Boxing Day"
|
|
5
data/christian.csv
Normal file
5
data/christian.csv
Normal file
|
@ -0,0 +1,5 @@
|
|||
month,regions,function,mday,wday,week,name
|
||||
0,christian,"lambda { |year| easter(year) }",,,,"Easter Sunday"
|
||||
0,christian,"lambda { |year| easter(year)-7 }",,,,"Palm Sunday"
|
||||
0,christian,"lambda { |year| easter(year)-2 }",,,,"Good Friday"
|
||||
12,christian,,25,,,"Christmas Day"
|
|
15
data/us.csv
15
data/us.csv
|
@ -1,4 +1,11 @@
|
|||
month,region,mday,wday,week,name
|
||||
1,us,1,,,"New Year's Day"
|
||||
1,us,,1,3,"Martin Luther King, Jr. Day"
|
||||
3,us,,1,3,"George Washington's Birthday"
|
||||
month,regions,function,mday,wday,week,name
|
||||
1,us,,1,,,"New Year's Day"
|
||||
1,us,,,1,3,"Martin Luther King, Jr. Day"
|
||||
3,us,,,1,3,"George Washington's Birthday"
|
||||
5,us,,,1,-1,"Memorial Day"
|
||||
6,us,,14,,,"Flag Day"
|
||||
7,us,,4,,,"Independence Day"
|
||||
9,us,,,1,1,"Labor Day"
|
||||
11,us,,11,,,"Veterans Day"
|
||||
11,us,,,4,4,"Thanksgiving Day"
|
||||
12,us,,25,,,"Christmas Day"
|
|
Loading…
Reference in a new issue