Updated docs
Renamed Date#is_holiday? to Date#holiday? Added Date#holidays
This commit is contained in:
parent
1da5732747
commit
208e2eea70
2 changed files with 29 additions and 15 deletions
20
CUSTOM DATES
20
CUSTOM DATES
|
@ -2,11 +2,11 @@
|
||||||
|
|
||||||
It's easy to add your own custom holiday definitions to the Holidays gem. In a nutshell:
|
It's easy to add your own custom holiday definitions to the Holidays gem. In a nutshell:
|
||||||
1. Build a definition file
|
1. Build a definition file
|
||||||
2. Run a <tt>rake</tt> to make a custom module
|
2. Run the <tt>rake</tt> task to make a custom module
|
||||||
3. <tt>require</tt> your new module
|
3. <tt>require</tt> your new module and enjoy!
|
||||||
|
|
||||||
|
|
||||||
== Building the YAML file
|
== 1. Building the YAML file
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
|
@ -21,8 +21,9 @@ Holidays are grouped by month from 1 through 12. Each entry within a month can
|
||||||
|
|
||||||
[<tt>wday</tt>] Integer representing day of the month (1 through 31).
|
[<tt>wday</tt>] Integer representing day of the month (1 through 31).
|
||||||
|
|
||||||
For example, the following holiday is on the first of the month and available in the <tt>ca</tt>, <tt>us</tt> and <tt>au</tt> regions.
|
For example, the following holiday is on the first of January and available in the <tt>ca</tt>, <tt>us</tt> and <tt>au</tt> regions.
|
||||||
|
|
||||||
|
1:
|
||||||
- name: New Year's Day
|
- name: New Year's Day
|
||||||
regions: [ca,us,au]
|
regions: [ca,us,au]
|
||||||
mday: 1
|
mday: 1
|
||||||
|
@ -34,8 +35,9 @@ For example, the following holiday is on the first of the month and available in
|
||||||
[<tt>week</tt>] Integer representing week number (1 = first week, 3 = third week, -1 = last week),
|
[<tt>week</tt>] Integer representing week number (1 = first week, 3 = third week, -1 = last week),
|
||||||
|
|
||||||
|
|
||||||
For example, the following holiday is on the first Monday of the month and available in the <tt>ca</tt> region.
|
For example, the following holiday is on the first Monday of September and available in the <tt>ca</tt> region.
|
||||||
|
|
||||||
|
9:
|
||||||
- name: Labour Day
|
- name: Labour Day
|
||||||
regions: [ca]
|
regions: [ca]
|
||||||
week: 1
|
week: 1
|
||||||
|
@ -74,20 +76,22 @@ Functions called in this manner must return either a Date object or an integer r
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
== Building the definition module
|
== 2. Building the definition module
|
||||||
|
|
||||||
Once you have your YAML definition file you need to generate a Ruby module using <tt>rake</tt>.
|
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
|
To build the module for school holidays
|
||||||
|
|
||||||
rake generate Custom custom.yaml
|
rake generate MyCustomDates custom.yaml
|
||||||
|
|
||||||
|
|
||||||
Your module can include definitions from multiple YAML files.
|
Your module can include definitions from multiple YAML files.
|
||||||
|
|
||||||
rake generate Custom ca.yaml us.yaml mx.yaml custom.yaml
|
rake generate MyCustomDates ca.yaml us.yaml mx.yaml custom.yaml
|
||||||
|
|
||||||
|
|
||||||
|
== 3. Using your custom definitions
|
||||||
|
|
||||||
Your custom module is ready to use.
|
Your custom module is ready to use.
|
||||||
|
|
||||||
require 'holidays'
|
require 'holidays'
|
||||||
|
|
|
@ -2,7 +2,7 @@ $:.unshift File.dirname(__FILE__)
|
||||||
require 'holidays/easter'
|
require 'holidays/easter'
|
||||||
|
|
||||||
module Holidays
|
module Holidays
|
||||||
# Exception thrown when an unknown region is encountered.
|
# Exception thrown when an unknown region is requested.
|
||||||
class UnkownRegionError < ArgumentError; end
|
class UnkownRegionError < ArgumentError; end
|
||||||
|
|
||||||
self.extend Easter
|
self.extend Easter
|
||||||
|
@ -43,7 +43,7 @@ module Holidays
|
||||||
# [<tt>:name</tt>] String.
|
# [<tt>:name</tt>] String.
|
||||||
# [<tt>:regions</tt>] An array of region symbols.
|
# [<tt>:regions</tt>] An array of region symbols.
|
||||||
# [<tt>:types</tt>] An array of holiday-type symbols.
|
# [<tt>:types</tt>] An array of holiday-type symbols.
|
||||||
def self.by_day(date, regions = [:ca, :us])
|
def self.by_day(date, regions = :any)
|
||||||
regions = validate_regions(regions)
|
regions = validate_regions(regions)
|
||||||
|
|
||||||
hbm = HOLIDAYS_BY_MONTH.values_at(0,date.mon).flatten
|
hbm = HOLIDAYS_BY_MONTH.values_at(0,date.mon).flatten
|
||||||
|
@ -86,7 +86,7 @@ module Holidays
|
||||||
# format.
|
# format.
|
||||||
#--
|
#--
|
||||||
# TODO: do not take full months
|
# TODO: do not take full months
|
||||||
def self.between(start_date, end_date, regions = [:ca,:us])
|
def self.between(start_date, end_date, regions = :any)
|
||||||
regions = validate_regions(regions)
|
regions = validate_regions(regions)
|
||||||
holidays = []
|
holidays = []
|
||||||
|
|
||||||
|
@ -141,15 +141,25 @@ end
|
||||||
class Date
|
class Date
|
||||||
include Holidays
|
include Holidays
|
||||||
|
|
||||||
# Check if the current date is a holiday in a given region.
|
# Get holidays on the current date.
|
||||||
#
|
#
|
||||||
# Date.civil('2008-01-01').is_holiday?(:ca)
|
# Returns an array.
|
||||||
# => true
|
#
|
||||||
def is_holiday?(regions = :any)
|
# Date.civil('2008-01-01').holidays(:ca)
|
||||||
|
# => [{:name => 'Canada Day',...}]
|
||||||
|
def holidays(regions = :any)
|
||||||
holidays = Holidays.by_day(self, regions)
|
holidays = Holidays.by_day(self, regions)
|
||||||
!holidays.empty?
|
!holidays.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Check if the current date is a holiday.
|
||||||
|
#
|
||||||
|
# Date.civil('2008-01-01').holiday?(:ca)
|
||||||
|
# => true
|
||||||
|
def holiday?(regions = :any)
|
||||||
|
!self.holidays(regions).empty?
|
||||||
|
end
|
||||||
|
|
||||||
# Calculate day of the month based on the week number and the day of the
|
# Calculate day of the month based on the week number and the day of the
|
||||||
# week.
|
# week.
|
||||||
#
|
#
|
||||||
|
|
Loading…
Add table
Reference in a new issue