diff --git a/lib/holidays.rb b/lib/holidays.rb index 74a5252..8c73aff 100644 --- a/lib/holidays.rb +++ b/lib/holidays.rb @@ -22,6 +22,9 @@ require 'date' # [:any] # Any region. Return holidays from any loaded region. # +# +# You can load all the available holiday definition sets by running +# Holidays.load_all # == Other options # [:observed] Return holidays on the day they are observed (e.g. on a Monday if they fall on a Sunday). # [:informal] Include informal holidays (e.g. Valentine's Day) @@ -52,6 +55,7 @@ module Holidays WEEKS = {:first => 1, :second => 2, :third => 3, :fourth => 4, :fifth => 5, :last => -1, :second_last => -2, :third_last => -3} MONTH_LENGTHS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] DAY_SYMBOLS = Date::DAYNAMES.collect { |n| n.downcase.intern } + DEFINITION_PATH = File.expand_path(File.dirname(__FILE__) + '/holidays/') # Get all holidays on a given date. # @@ -269,6 +273,19 @@ module Holidays date -= 1 if date.wday == 6 date end + + # Returns an array of symbols all the available holiday definitions. + # + # Optional `full_path` param is used internally for loading all the definitions. + def self.available(full_path = false) + paths = Dir.glob(DEFINITION_PATH + '/*.rb') + full_path ? paths : paths.collect { |path| path.match(/([a-z_-]+)\.rb/i)[1].to_sym } + end + + # Load all available holiday definitions + def self.load_all + self.available(true).each { |path| require path } + end private # Returns [(arr)regions, (bool)observed, (bool)informal] @@ -302,7 +319,7 @@ private end regions.flatten! - + require "holidays/north_america" if regions.include?(:us) # special case for north_america/US cross-linking raise UnknownRegionError unless regions.all? { |r| r == :any or @@regions.include?(r) or begin require "holidays/#{r.to_s}"; rescue LoadError; false; end } diff --git a/test/test_all_regions.rb b/test/test_all_regions.rb new file mode 100644 index 0000000..fa522a0 --- /dev/null +++ b/test/test_all_regions.rb @@ -0,0 +1,48 @@ +require File.expand_path(File.dirname(__FILE__)) + '/test_helper' + +class MultipleRegionsTests < Test::Unit::TestCase + + def test_definition_dir + assert File.directory?(Holidays::DEFINITION_PATH) + end + + def test_getting_available_paths + defs = Holidays.available(true) + assert_equal def_count, defs.size + + defs.each do |f| + assert f.kind_of?(String) + assert File.exists?(f) + end + end + + def test_getting_available_symbols + defs = Holidays.available(false) + assert_equal def_count, defs.size + + defs.each { |f| assert f.kind_of?(Symbol) } + + # some spot checks + assert defs.include?(:ca) + assert defs.include?(:united_nations) + end + + + def test_loading_all + Holidays.load_all + holidays = Holidays.on(Date.civil(2011, 5, 1), :any) + + # at least 15 now, but there could be more in the future + assert holidays.size > 15 + + # some spot checks + assert holidays.any? { |h| h[:name] == 'Staatsfeiertag' } # :at + assert holidays.any? { |h| h[:name] == 'Dia do Trabalho' } # :br + assert holidays.any? { |h| h[:name] == 'Vappu' } # :fi + end + +private + def def_count + Dir.glob(Holidays::DEFINITION_PATH + '/*.rb').size + end +end