2007-11-24 01:45:30 +01:00
|
|
|
require 'yaml'
|
|
|
|
|
2007-11-29 03:24:00 +01:00
|
|
|
# Functions are stored in generated files as both Procs (:function) and
|
|
|
|
# Strings (:function_id). The String version makes comparisons of Procs much
|
|
|
|
# easier.
|
|
|
|
#
|
|
|
|
# TODO:
|
|
|
|
# - better comparison of existing rules
|
2007-11-24 01:45:30 +01:00
|
|
|
def parse_holiday_defs(module_name, files)
|
|
|
|
regions = []
|
|
|
|
rules_by_month = {}
|
|
|
|
custom_methods = {}
|
2007-12-05 23:27:05 +01:00
|
|
|
test_strs = []
|
2007-11-24 01:45:30 +01:00
|
|
|
|
|
|
|
files.each do |file|
|
|
|
|
def_file = YAML.load_file(file)
|
|
|
|
puts " Loading #{file}"
|
|
|
|
if def_file['months']
|
|
|
|
puts " - importing dates..."
|
|
|
|
def_file['months'].each do |month, definitions|
|
|
|
|
rules_by_month[month] = [] unless rules_by_month[month]
|
|
|
|
definitions.each do |definition|
|
|
|
|
rule = {}
|
|
|
|
definition.each do |key, val|
|
|
|
|
rule[key] = val
|
|
|
|
end
|
|
|
|
|
|
|
|
rule['regions'] = rule['regions'].collect { |r| r.to_sym }
|
|
|
|
|
|
|
|
regions << rule['regions']
|
|
|
|
|
|
|
|
exists = false
|
|
|
|
rules_by_month[month].each do |ex|
|
2007-11-30 00:23:35 +01:00
|
|
|
if ex['name'] == rule['name'] and ex['wday'] == rule['wday'] and ex['mday'] == rule['mday'] and ex['week'] == rule['week'] and ex['type'] == rule['type'] and ex['function'] == rule['function'] and ex['observed'] == rule['observed']
|
2007-11-24 01:45:30 +01:00
|
|
|
ex['regions'] << rule['regions'].flatten
|
|
|
|
exists = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
unless exists
|
|
|
|
rules_by_month[month] << rule
|
|
|
|
end
|
|
|
|
|
|
|
|
end # /defs.each
|
|
|
|
end
|
|
|
|
end
|
2007-11-25 00:22:10 +01:00
|
|
|
|
2007-11-24 01:45:30 +01:00
|
|
|
if def_file['methods']
|
|
|
|
puts " - importing methods..."
|
|
|
|
def_file['methods'].each do |name, code|
|
|
|
|
custom_methods[name] = code
|
|
|
|
end # /methods.each
|
|
|
|
end
|
|
|
|
|
2007-12-05 23:27:05 +01:00
|
|
|
if def_file['tests']
|
2010-11-12 21:45:12 +01:00
|
|
|
puts " - importing tests..."
|
2007-12-05 23:27:05 +01:00
|
|
|
test_strs << def_file['tests']
|
|
|
|
end
|
2007-11-24 01:45:30 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Build the definitions
|
|
|
|
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']
|
2007-11-29 03:24:00 +01:00
|
|
|
str << ":function => lambda { |year| Holidays.#{rule['function']} }, "
|
|
|
|
str << ":function_id => \"#{rule['function'].to_s}\", "
|
2007-11-24 01:45:30 +01:00
|
|
|
else
|
|
|
|
str << ":wday => #{rule['wday']}, :week => #{rule['week']}, "
|
|
|
|
end
|
|
|
|
|
2007-11-30 00:23:35 +01:00
|
|
|
if rule['observed']
|
|
|
|
str << ":observed => lambda { |date| Holidays.#{rule['observed']}(date) }, "
|
2007-11-30 01:03:43 +01:00
|
|
|
str << ":observed_id => \"#{rule['observed'].to_s}\", "
|
2007-11-30 00:23:35 +01:00
|
|
|
end
|
|
|
|
|
2007-11-24 21:50:20 +01:00
|
|
|
if rule['type']
|
|
|
|
str << ":type => :#{rule['type']}, "
|
|
|
|
end
|
|
|
|
|
2007-11-24 01:45:30 +01:00
|
|
|
# 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")
|
|
|
|
|
|
|
|
|
|
|
|
# Build the methods
|
|
|
|
method_str = ''
|
|
|
|
custom_methods.each do |key, code|
|
|
|
|
method_str << code + "\n\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2007-12-05 23:27:05 +01:00
|
|
|
# Build the module file
|
|
|
|
module_src =<<-EOM
|
2010-11-12 21:45:12 +01:00
|
|
|
# encoding: utf-8
|
2007-11-24 01:45:30 +01:00
|
|
|
module Holidays
|
|
|
|
# This file is generated by the Ruby Holiday gem.
|
|
|
|
#
|
|
|
|
# Definitions loaded: #{files.join(', ')}
|
|
|
|
#
|
2007-11-25 00:22:10 +01:00
|
|
|
# To use the definitions in this file, load them right after you load the
|
2007-11-24 01:45:30 +01:00
|
|
|
# Holiday gem:
|
|
|
|
#
|
|
|
|
# require 'holidays'
|
2007-11-25 00:22:10 +01:00
|
|
|
# require 'holidays/#{module_name.downcase}'
|
2007-11-24 01:45:30 +01:00
|
|
|
#
|
|
|
|
# More definitions are available at http://code.dunae.ca/holidays.
|
|
|
|
module #{module_name} # :nodoc:
|
|
|
|
DEFINED_REGIONS = [:#{regions.flatten.uniq.join(', :')}]
|
|
|
|
|
|
|
|
HOLIDAYS_BY_MONTH = {
|
|
|
|
#{month_strs.join(",\n")}
|
|
|
|
}
|
2007-11-25 00:22:10 +01:00
|
|
|
end
|
2007-11-24 01:45:30 +01:00
|
|
|
|
|
|
|
#{method_str}
|
|
|
|
end
|
|
|
|
|
|
|
|
Holidays.merge_defs(Holidays::#{module_name}::DEFINED_REGIONS, Holidays::#{module_name}::HOLIDAYS_BY_MONTH)
|
2007-12-05 23:27:05 +01:00
|
|
|
EOM
|
|
|
|
|
|
|
|
|
|
|
|
# Build the test file
|
|
|
|
unless test_strs.empty?
|
|
|
|
test_src =<<-EndOfTests
|
2010-11-12 21:45:12 +01:00
|
|
|
# encoding: utf-8
|
|
|
|
require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
|
2007-12-05 23:27:05 +01:00
|
|
|
|
|
|
|
# This file is generated by the Ruby Holiday gem.
|
|
|
|
#
|
|
|
|
# Definitions loaded: #{files.join(', ')}
|
|
|
|
class #{module_name.capitalize}DefinitionTests < Test::Unit::TestCase # :nodoc:
|
|
|
|
|
|
|
|
def test_#{module_name.downcase}
|
|
|
|
#{test_strs.join("\n\n")}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
EndOfTests
|
|
|
|
end
|
2007-11-24 01:45:30 +01:00
|
|
|
|
2007-12-05 23:27:05 +01:00
|
|
|
return module_src, test_src || ''
|
2007-11-24 01:45:30 +01:00
|
|
|
|
2007-12-19 02:06:57 +01:00
|
|
|
end
|