Cleaned-up docs
Tests for ZA and AU
This commit is contained in:
parent
59f5d2e91c
commit
b6968ccdb3
9 changed files with 76 additions and 111 deletions
4
README
4
README
|
@ -16,9 +16,9 @@ Extends Ruby's built-in Date class and supports custom holiday definition lists.
|
|||
# Get all holidays in July, 2008 in Canada and the US
|
||||
from = Date.civil(2008,7,1)
|
||||
to = Date.civil(2008,7,31)
|
||||
Holidays.between(from, to, [:ca,:us])
|
||||
Holidays.between(from, to, :ca,:us)
|
||||
=> [{:name => 'Canada Day',...}
|
||||
{:name => 'Idenpendence Day',...}]
|
||||
{:name => 'Independence Day',...}]
|
||||
|
||||
|
||||
==== Extending Ruby's Date class
|
||||
|
|
|
@ -1,12 +1,4 @@
|
|||
= Custom dates with the Ruby Holidays Gem
|
||||
|
||||
It's easy to add your own custom holiday definitions to the Holidays gem. In a nutshell:
|
||||
1. Build a definition file
|
||||
2. Run the <tt>rake</tt> task to make a custom module
|
||||
3. <tt>require</tt> your new module and enjoy!
|
||||
|
||||
|
||||
== 1. Building the YAML file
|
||||
== Holiday Gem Definition Syntax
|
||||
|
||||
Definition files have two main parts: *months* and *methods*. Before you start, you may want to look some of the existing files at http://code.dunae.ca/svn/holidays/trunk/data.
|
||||
|
||||
|
@ -28,7 +20,6 @@ For example, the following holiday is on the first of January and available in t
|
|||
regions: [ca, us, au]
|
||||
mday: 1
|
||||
|
||||
|
||||
===== Dates defined by a week number (e.g. first Monday of a month)
|
||||
|
||||
[<tt>wday</tt>] Integer representing day of the week (0 = Sunday through 6 = Saturday).
|
||||
|
@ -43,9 +34,6 @@ For example, the following holiday is on the first Monday of September and avail
|
|||
week: 1
|
||||
wday: 1
|
||||
|
||||
|
||||
|
||||
|
||||
=== Calculating dates with methods
|
||||
|
||||
In addition to defining holidays by day or week, you can create custom methods to calculate a date.
|
||||
|
@ -69,40 +57,29 @@ This would be represented in the <tt>months</tt> section as:
|
|||
5:
|
||||
- name: Victoria Day
|
||||
regions: [ca]
|
||||
function: lambda { |year| ca_victoria_day(year) }
|
||||
function: ca_victoria_day(year)
|
||||
|
||||
If a holiday can occur in different months (e.g. Easter) it can go in the '0' month.
|
||||
|
||||
0:
|
||||
- name: Easter Monday
|
||||
regions: [ca]
|
||||
function: easter(year)+1
|
||||
|
||||
Calculated-date functions take the year (integer) as a parameter and must return either a Date object or an integer representing the day of the month.
|
||||
|
||||
|
||||
Functions called in this manner must return either a Date object or an integer representing the day of the month.
|
||||
=== Calculating observed dates
|
||||
|
||||
Several built-in methods are available for holidays that are observed on varying dates. For example, for a holiday that is observed on Monday if it falls on a weekend you could write:
|
||||
|
||||
7:
|
||||
- name: Canada Day
|
||||
regions: [ca]
|
||||
mday: 1
|
||||
observed: to_monday_if_weekend(date)
|
||||
|
||||
Observed-date functions take a Date object as a parameter and must return either a Date object or an integer representing the day of the month.
|
||||
|
||||
|
||||
|
||||
== 2. Building the definition module
|
||||
|
||||
Once you have your YAML definition file you need to generate a Ruby module using <tt>rake</tt>.
|
||||
|
||||
To build the module for school holidays
|
||||
|
||||
rake generate MyCustomDates custom.yaml
|
||||
|
||||
|
||||
Your module can include definitions from multiple YAML files.
|
||||
|
||||
rake generate MyCustomDates ca.yaml us.yaml mx.yaml custom.yaml
|
||||
|
||||
|
||||
== 3. Using your custom definitions
|
||||
|
||||
Your custom module is ready to use.
|
||||
|
||||
require 'holidays'
|
||||
require 'path/to/custom'
|
||||
|
||||
Holidays.by_day(date, :your_region)
|
||||
|
||||
|
||||
|
||||
=== Sharing
|
||||
|
||||
If you've built a definition file that others might enjoy, why not share it?
|
||||
|
||||
Send any definition files to 'code' at 'dunae.ca'.
|
38
data/au.yaml
38
data/au.yaml
|
@ -1,6 +1,10 @@
|
|||
# Australian holiday definitions for the Ruby Holiday gem.
|
||||
# Updated: 2008-11-21.
|
||||
# Source: http://en.wikipedia.org/wiki/Australian_public_holidays
|
||||
# Updated: 2008-11-29.
|
||||
# Sources:
|
||||
# - http://en.wikipedia.org/wiki/Australian_public_holidays
|
||||
# - http://www.docep.wa.gov.au/lr/LabourRelations/Content/Wages%20and%20Conditions/Public%20Holidays/Public_Holidays.html
|
||||
# - http://www.wst.tas.gov.au/employment_info/public_holidays
|
||||
# TODO: missing some regional holidays
|
||||
---
|
||||
months:
|
||||
0:
|
||||
|
@ -28,8 +32,12 @@ months:
|
|||
regions: [au_wa]
|
||||
week: 1
|
||||
wday: 1
|
||||
- name: Eight Hours Day
|
||||
regions: [au_tas]
|
||||
week: 2
|
||||
wday: 1
|
||||
- name: Labour Day
|
||||
regions: [au_vic, au_tas]
|
||||
regions: [au_vic]
|
||||
week: 2
|
||||
wday: 1
|
||||
4:
|
||||
|
@ -38,12 +46,20 @@ months:
|
|||
mday: 25
|
||||
5:
|
||||
- name: Labour Day
|
||||
regions: [au_qld, au_nt]
|
||||
regions: [au_qld]
|
||||
week: 1
|
||||
wday: 1
|
||||
- name: May Day
|
||||
regions: [au_nt]
|
||||
week: 1
|
||||
wday: 1
|
||||
6:
|
||||
- name: Foundation Day
|
||||
regions: [au_wa]
|
||||
week: 1
|
||||
wday: 1
|
||||
- name: Queen's Birthday
|
||||
regions: [au]
|
||||
regions: [au_act, au_nsw, au_sa, au_tas, au_nt, au_qld, au_vic]
|
||||
week: 2
|
||||
wday: 1
|
||||
- name: Queensland Day
|
||||
|
@ -62,15 +78,3 @@ months:
|
|||
- name: Boxing Day
|
||||
regions: [au]
|
||||
mday: 26
|
||||
methods:
|
||||
ca_victoria_day: |
|
||||
# Monday on or before May 24
|
||||
def self.ca_victoria_day(year)
|
||||
date = Date.civil(year,5,24)
|
||||
if date.wday > 1
|
||||
date -= (date.wday - 1)
|
||||
elsif date.wday == 0
|
||||
date -= 6
|
||||
end
|
||||
date
|
||||
end
|
|
@ -1,17 +0,0 @@
|
|||
# Christian holiday definitions for the Ruby Holiday gem.
|
||||
# Updated 2008-11-21.
|
||||
---
|
||||
0:
|
||||
- name: Easter Sunday
|
||||
regions: [christian]
|
||||
function: easter(year) }
|
||||
- name: Palm Sunday
|
||||
regions: [christian]
|
||||
function: easter(year)-7 }
|
||||
- name: Good Friday
|
||||
regions: [christian]
|
||||
function: easter(year)-2 }
|
||||
12:
|
||||
- name: Christmas Day
|
||||
regions: [christian]
|
||||
mday: 25
|
14
data/za.yaml
14
data/za.yaml
|
@ -1,6 +1,6 @@
|
|||
# South African holiday definitions for the Ruby Holiday gem.
|
||||
#
|
||||
# Updated: 2008-11-22.
|
||||
# Updated: 2008-11-29.
|
||||
# Sources:
|
||||
# - http://en.wikipedia.org/wiki/Public_holidays_in_South_Africa
|
||||
# - http://www.info.gov.za/aboutsa/holidays.htm
|
||||
|
@ -17,38 +17,48 @@ months:
|
|||
- name: New Year's Day
|
||||
regions: [za]
|
||||
mday: 1
|
||||
observed: to_monday_if_sunday
|
||||
3:
|
||||
- name: Human Rights Day
|
||||
regions: [za]
|
||||
mday: 21
|
||||
observed: to_monday_if_sunday
|
||||
4:
|
||||
- name: Freedom Day
|
||||
regions: [za]
|
||||
mday: 27
|
||||
observed: to_monday_if_sunday
|
||||
5:
|
||||
- name: Workers' Day
|
||||
- name: Workers Day
|
||||
regions: [za]
|
||||
mday: 1
|
||||
observed: to_monday_if_sunday
|
||||
6:
|
||||
- name: Youth Day
|
||||
regions: [za]
|
||||
mday: 16
|
||||
observed: to_monday_if_sunday
|
||||
8:
|
||||
- name: National Women's Day
|
||||
regions: [za]
|
||||
mday: 9
|
||||
observed: to_monday_if_sunday
|
||||
9:
|
||||
- name: Heritage Day
|
||||
regions: [za]
|
||||
mday: 24
|
||||
observed: to_monday_if_sunday
|
||||
12:
|
||||
- name: Day of Reconciliation
|
||||
regions: [za]
|
||||
mday: 16
|
||||
observed: to_monday_if_sunday
|
||||
- name: Christmas Day
|
||||
regions: [za]
|
||||
mday: 25
|
||||
observed: to_monday_if_sunday
|
||||
- name: Day of Goodwill
|
||||
regions: [za]
|
||||
mday: 26
|
||||
observed: to_weekday_if_boxing_weekend
|
||||
|
||||
|
|
|
@ -11,38 +11,30 @@ module Holidays
|
|||
#
|
||||
# More definitions are available at http://code.dunae.ca/holidays.
|
||||
module AU # :nodoc:
|
||||
DEFINED_REGIONS = [:au_qld, :au_nt, :au, :au_tas, :au_wa, :au_vic, :au_act, :au_nsw, :au_sa]
|
||||
DEFINED_REGIONS = [:au_qld, :au_nt, :au, :au_tas, :au_wa, :au_act, :au_nsw, :au_sa, :au_vic]
|
||||
|
||||
HOLIDAYS_BY_MONTH = {
|
||||
5 => [{:wday => 1, :week => 1, :name => "Labour Day", :regions => [:au_qld, :au_nt]}],
|
||||
5 => [{:wday => 1, :week => 1, :name => "Labour Day", :regions => [:au_qld]},
|
||||
{:wday => 1, :week => 1, :name => "May Day", :regions => [:au_nt]}],
|
||||
0 => [{:function => lambda { |year| Holidays.easter(year)-2 }, :function_id => "easter(year)-2", :name => "Good Friday", :regions => [:au]},
|
||||
{:function => lambda { |year| Holidays.easter(year)-1 }, :function_id => "easter(year)-1", :name => "Easter Saturday", :regions => [:au]},
|
||||
{:function => lambda { |year| Holidays.easter(year)+1 }, :function_id => "easter(year)+1", :name => "Easter Monday", :regions => [:au]},
|
||||
{:function => lambda { |year| Holidays.easter(year)+2 }, :function_id => "easter(year)+2", :name => "Easter Monday", :regions => [:au_tas]}],
|
||||
6 => [{:wday => 1, :week => 2, :name => "Queen's Birthday", :regions => [:au]},
|
||||
6 => [{:wday => 1, :week => 1, :name => "Foundation Day", :regions => [:au_wa]},
|
||||
{:wday => 1, :week => 2, :name => "Queen's Birthday", :regions => [:au_act, :au_nsw, :au_sa, :au_tas, :au_nt, :au_qld, :au_vic]},
|
||||
{:mday => 6, :type => :informal, :name => "Queensland Day", :regions => [:au_qld]}],
|
||||
1 => [{:mday => 1, :name => "New Year's Day", :regions => [:au]},
|
||||
{:mday => 26, :name => "Australia Day", :regions => [:au]}],
|
||||
12 => [{:mday => 25, :name => "Christmas Day", :regions => [:au]},
|
||||
{:mday => 26, :name => "Boxing Day", :regions => [:au]}],
|
||||
3 => [{:wday => 1, :week => 1, :name => "Labour Day", :regions => [:au_wa]},
|
||||
{:wday => 1, :week => 2, :name => "Labour Day", :regions => [:au_vic, :au_tas]}],
|
||||
{:wday => 1, :week => 2, :name => "Eight Hours Day", :regions => [:au_tas]},
|
||||
{:wday => 1, :week => 2, :name => "Labour Day", :regions => [:au_vic]}],
|
||||
4 => [{:mday => 25, :name => "ANZAC Day", :regions => [:au]}],
|
||||
10 => [{:wday => 1, :week => 1, :name => "Labour Day", :regions => [:au_act, :au_nsw, :au_sa]}]
|
||||
}
|
||||
end
|
||||
|
||||
# Monday on or before May 24
|
||||
def self.ca_victoria_day(year)
|
||||
date = Date.civil(year,5,24)
|
||||
if date.wday > 1
|
||||
date -= (date.wday - 1)
|
||||
elsif date.wday == 0
|
||||
date -= 6
|
||||
end
|
||||
date
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
|
|
@ -14,18 +14,18 @@ module Holidays
|
|||
DEFINED_REGIONS = [:za]
|
||||
|
||||
HOLIDAYS_BY_MONTH = {
|
||||
5 => [{:mday => 1, :name => "Workers' Day", :regions => [:za]}],
|
||||
5 => [{:mday => 1, :observed => lambda { |date| Holidays.to_monday_if_sunday(date) }, :observed_id => "to_monday_if_sunday", :name => "Workers Day", :regions => [:za]}],
|
||||
0 => [{:function => lambda { |year| Holidays.easter(year)-2 }, :function_id => "easter(year)-2", :name => "Good Friday", :regions => [:za]},
|
||||
{:function => lambda { |year| Holidays.easter(year)+1 }, :function_id => "easter(year)+1", :name => "Family Day", :regions => [:za]}],
|
||||
6 => [{:mday => 16, :name => "Youth Day", :regions => [:za]}],
|
||||
1 => [{:mday => 1, :name => "New Year's Day", :regions => [:za]}],
|
||||
12 => [{:mday => 16, :name => "Day of Reconciliation", :regions => [:za]},
|
||||
{:mday => 25, :name => "Christmas Day", :regions => [:za]},
|
||||
{:mday => 26, :name => "Day of Goodwill", :regions => [:za]}],
|
||||
8 => [{:mday => 9, :name => "National Women's Day", :regions => [:za]}],
|
||||
3 => [{:mday => 21, :name => "Human Rights Day", :regions => [:za]}],
|
||||
9 => [{:mday => 24, :name => "Heritage Day", :regions => [:za]}],
|
||||
4 => [{:mday => 27, :name => "Freedom Day", :regions => [:za]}]
|
||||
6 => [{:mday => 16, :observed => lambda { |date| Holidays.to_monday_if_sunday(date) }, :observed_id => "to_monday_if_sunday", :name => "Youth Day", :regions => [:za]}],
|
||||
1 => [{:mday => 1, :observed => lambda { |date| Holidays.to_monday_if_sunday(date) }, :observed_id => "to_monday_if_sunday", :name => "New Year's Day", :regions => [:za]}],
|
||||
12 => [{:mday => 16, :observed => lambda { |date| Holidays.to_monday_if_sunday(date) }, :observed_id => "to_monday_if_sunday", :name => "Day of Reconciliation", :regions => [:za]},
|
||||
{:mday => 25, :observed => lambda { |date| Holidays.to_monday_if_sunday(date) }, :observed_id => "to_monday_if_sunday", :name => "Christmas Day", :regions => [:za]},
|
||||
{:mday => 26, :observed => lambda { |date| Holidays.to_weekday_if_boxing_weekend(date) }, :observed_id => "to_weekday_if_boxing_weekend", :name => "Day of Goodwill", :regions => [:za]}],
|
||||
8 => [{:mday => 9, :observed => lambda { |date| Holidays.to_monday_if_sunday(date) }, :observed_id => "to_monday_if_sunday", :name => "National Women's Day", :regions => [:za]}],
|
||||
3 => [{:mday => 21, :observed => lambda { |date| Holidays.to_monday_if_sunday(date) }, :observed_id => "to_monday_if_sunday", :name => "Human Rights Day", :regions => [:za]}],
|
||||
9 => [{:mday => 24, :observed => lambda { |date| Holidays.to_monday_if_sunday(date) }, :observed_id => "to_monday_if_sunday", :name => "Heritage Day", :regions => [:za]}],
|
||||
4 => [{:mday => 27, :observed => lambda { |date| Holidays.to_monday_if_sunday(date) }, :observed_id => "to_monday_if_sunday", :name => "Freedom Day", :regions => [:za]}]
|
||||
}
|
||||
end
|
||||
|
||||
|
|
|
@ -98,7 +98,6 @@ EOH
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
desc 'Generate documentation.'
|
||||
Rake::RDocTask.new(:rdoc) do |rdoc|
|
||||
rdoc.rdoc_dir = 'doc'
|
||||
|
@ -106,8 +105,8 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|||
rdoc.options << '--all' << '--inline-source' << '--line-numbers'
|
||||
rdoc.options << '--charset' << 'utf-8'
|
||||
rdoc.rdoc_files.include('README')
|
||||
rdoc.rdoc_files.include('data/SYNTAX')
|
||||
rdoc.rdoc_files.include('lib/holidays/MANIFEST')
|
||||
rdoc.rdoc_files.include('CUSTOM DATES')
|
||||
rdoc.rdoc_files.include('REFERENCES')
|
||||
rdoc.rdoc_files.include('LICENSE')
|
||||
rdoc.rdoc_files.include('lib/*.rb')
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
require File.dirname(__FILE__) + '/test_helper'
|
||||
require 'holidays/europe'
|
||||
|
||||
class RegionTests < Test::Unit::TestCase
|
||||
class EuropeTests < Test::Unit::TestCase
|
||||
def test_de
|
||||
{Date.civil(2009,1,1) => 'Neujahrstag',
|
||||
Date.civil(2009,4,10) => 'Karfreitag',
|
||||
|
|
Loading…
Reference in a new issue