From 1f11004bd71469c6ad59c9426ba0c52715341c13 Mon Sep 17 00:00:00 2001 From: Alex Dunae Date: Wed, 21 Nov 2007 01:03:20 +0000 Subject: [PATCH] Added merging functions in the definition builder Added DEFINED_REGIONS output to the definition builder --- create_defs.rb | 53 +++++++++++++++++++++++++++++----------------- data/ca.csv | 7 ++++++ data/christian.csv | 5 +++++ data/us.csv | 15 +++++++++---- 4 files changed, 57 insertions(+), 23 deletions(-) create mode 100644 data/ca.csv create mode 100644 data/christian.csv diff --git a/create_defs.rb b/create_defs.rb index a3eea61..d009930 100644 --- a/create_defs.rb +++ b/create_defs.rb @@ -1,29 +1,41 @@ 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 = {} +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] -FasterCSV.foreach('data/us.csv', {: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']] - rule = {} - row.each do |key, val| - rule[key] = val + # 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 - - rules_by_month[month] << rule 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 ") + "]" diff --git a/data/ca.csv b/data/ca.csv new file mode 100644 index 0000000..08f12be --- /dev/null +++ b/data/ca.csv @@ -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" \ No newline at end of file diff --git a/data/christian.csv b/data/christian.csv new file mode 100644 index 0000000..b4c4f59 --- /dev/null +++ b/data/christian.csv @@ -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" diff --git a/data/us.csv b/data/us.csv index 11fc43d..c6695ed 100644 --- a/data/us.csv +++ b/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" \ No newline at end of file