Added merging functions in the definition builder

Added DEFINED_REGIONS output to the definition builder
This commit is contained in:
Alex Dunae 2007-11-21 01:03:20 +00:00
parent d98a443337
commit 1f11004bd7
4 changed files with 57 additions and 23 deletions

View file

@ -1,17 +1,10 @@
require 'fastercsv' require 'fastercsv'
# ==== Rows files = ['data/ca.csv', 'data/christian.csv', 'data/us.csv']
# 0 -> month regions = {}
# 1 -> region
# 2 -> mday
# 3 -> wday
# 4 -> week
# 5 -> name
rules_by_month = {} rules_by_month = {}
files.each do |file|
FasterCSV.foreach('data/us.csv', {:headers => :first_row, :return_headers => false, :force_quotes => true}) do |row| FasterCSV.foreach(file, {:headers => :first_row, :return_headers => false, :force_quotes => true}) do |row|
month = row['month'].to_i month = row['month'].to_i
rules_by_month[month] = [] unless rules_by_month[month] 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| row.each do |key, val|
rule[key] = val rule[key] = val
end end
regions[rule['regions'].to_sym] = true
rule['regions'] = [rule['regions']]
rules_by_month[month] << rule # 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
out = "# This file is generated by one of the Holiday gem's Rake tasks.\n" 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" out << "HOLIDAYS_BY_MONTH = {\n"
@ -35,11 +47,14 @@ rules_by_month.each do |month, rules|
str = '{' str = '{'
if rule['mday'] if rule['mday']
str << ":mday => #{rule['mday']}, " str << ":mday => #{rule['mday']}, "
elsif rule['function']
str << ":function => #{rule['function']}, "
else else
str << ":wday => #{rule['wday']}, :week => #{rule['week']}, " str << ":wday => #{rule['wday']}, :week => #{rule['week']}, "
end 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 rule_strings << str
end end
month_str << rule_strings.join(",\n ") + "]" month_str << rule_strings.join(",\n ") + "]"

7
data/ca.csv Normal file
View 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"
1 month regions function mday wday week name
2 1 ca 1 New Year's Day
3 7 ca 1 Canada Day
4 9 ca 1 1 Labour Day
5 11 ca 11 Rememberance Day
6 12 ca 25 Christmas Day
7 12 ca 26 Boxing Day

5
data/christian.csv Normal file
View 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"
1 month regions function mday wday week name
2 0 christian lambda { |year| easter(year) } Easter Sunday
3 0 christian lambda { |year| easter(year)-7 } Palm Sunday
4 0 christian lambda { |year| easter(year)-2 } Good Friday
5 12 christian 25 Christmas Day

View file

@ -1,4 +1,11 @@
month,region,mday,wday,week,name month,regions,function,mday,wday,week,name
1,us,1,,,"New Year's Day" 1,us,,1,,,"New Year's Day"
1,us,,1,3,"Martin Luther King, Jr. Day" 1,us,,,1,3,"Martin Luther King, Jr. Day"
3,us,,1,3,"George Washington's Birthday" 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"
1 month region regions function mday wday week name
2 1 us us 1 New Year's Day
3 1 us us 1 3 Martin Luther King, Jr. Day
4 3 us us 1 3 George Washington's Birthday
5 5 us 1 -1 Memorial Day
6 6 us 14 Flag Day
7 7 us 4 Independence Day
8 9 us 1 1 Labor Day
9 11 us 11 Veterans Day
10 11 us 4 4 Thanksgiving Day
11 12 us 25 Christmas Day