holiday 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +80 -0
- data/Rakefile +14 -0
- data/holiday.gemspec +24 -0
- data/lib/holiday.rb +59 -0
- data/lib/holiday.yml +18 -0
- data/lib/holiday/builder.rb +18 -0
- data/lib/holiday/parser.rb +41 -0
- data/lib/holiday/query.rb +11 -0
- data/lib/holiday/version.rb +3 -0
- data/test/helper.rb +2 -0
- data/test/holiday.yml +18 -0
- data/test/holiday_builder_test.rb +16 -0
- data/test/holiday_parser_test.rb +41 -0
- data/test/holiday_query_test.rb +16 -0
- data/test/holiday_test.rb +53 -0
- metadata +81 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# Holiday
|
2
|
+
|
3
|
+
Holiday is a gem that lets you configure holidays for multiple countries with YAML.
|
4
|
+
|
5
|
+
## Default Usage
|
6
|
+
|
7
|
+
First setup path to a YAML file and set the country code.
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
|
11
|
+
Holiday.yaml_file = "/path/to/yaml/holiday.yml"
|
12
|
+
Holiday.country = "US"
|
13
|
+
|
14
|
+
```
|
15
|
+
|
16
|
+
You can query for dates
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
|
20
|
+
# Defaults to current year
|
21
|
+
Holiday.find("christmas")
|
22
|
+
Holiday.find("labour day")
|
23
|
+
|
24
|
+
```
|
25
|
+
|
26
|
+
Or supply a year
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
|
30
|
+
# Can supply the year as optional argument
|
31
|
+
Holiday.find(:christmas, 2005)
|
32
|
+
Holiday.find(:thanksgiving, 1999)
|
33
|
+
|
34
|
+
```
|
35
|
+
|
36
|
+
## YAML
|
37
|
+
|
38
|
+
The YAML format for Holiday is as follows
|
39
|
+
|
40
|
+
```yaml
|
41
|
+
holiday:
|
42
|
+
US:
|
43
|
+
christmas:
|
44
|
+
when: december 25th
|
45
|
+
as: christmas
|
46
|
+
halloween:
|
47
|
+
when: october 31st
|
48
|
+
as: halloween, all hallows eve
|
49
|
+
labor_day:
|
50
|
+
when: 1st monday in september
|
51
|
+
as: labor day, labour day
|
52
|
+
thanksgiving:
|
53
|
+
when: 4th thursday in november
|
54
|
+
as: thanksgiving, turkey day
|
55
|
+
```
|
56
|
+
|
57
|
+
Holidays are scoped by country code. Each holiday needs both "when" and "as" keys. The "when" key can either be an exact
|
58
|
+
month and day, or the occurrence of the holiday in the given month. E.g., "1st monday in september".
|
59
|
+
|
60
|
+
The "as" key is used to identify holidays by alternate names and spellings. For instance, "thanksgiving" and "turkey day" should both
|
61
|
+
point to the 4th thursday in november.
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
|
65
|
+
Holiday.find("turkey day")
|
66
|
+
Holiday.find(:thanksgiving)
|
67
|
+
|
68
|
+
```
|
69
|
+
|
70
|
+
## Other methods
|
71
|
+
|
72
|
+
There are a few other methods that may be useful
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
|
76
|
+
Holiday.all # array containing all keys and alternate names of holidays from yaml file
|
77
|
+
Holiday.scan("a string containing a holiday by name, like christmas") # => christmas
|
78
|
+
Holiday::Query.find("all hallows eve") # => october 31st
|
79
|
+
|
80
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
# Run the test with 'rake' or 'rake test'
|
6
|
+
desc 'Default: run hashed_attributes unit tests.'
|
7
|
+
task :default => :test
|
8
|
+
|
9
|
+
desc 'Test the hashed_attributes gem.'
|
10
|
+
Rake::TestTask.new(:test) do |t|
|
11
|
+
t.libs << 'lib' << 'test'
|
12
|
+
t.pattern = 'test/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
data/holiday.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "holiday/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "holiday"
|
7
|
+
s.version = Holiday::VERSION
|
8
|
+
s.authors = ["Sean Behan"]
|
9
|
+
s.email = ["bseanvt@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/bseanvt/holiday"
|
11
|
+
s.summary = %q{Holiday is a gem that lets you configure holidays for any country using a simple YAML file format.}
|
12
|
+
s.description = %q{Holiday is a gem that lets you configure holidays for any country using a simple YAML file format.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "holiday"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
s.add_runtime_dependency "activesupport" #, ">= 3.1.0"
|
24
|
+
end
|
data/lib/holiday.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'active_support/time'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
require "#{File.dirname(__FILE__)}/holiday/builder"
|
5
|
+
require "#{File.dirname(__FILE__)}/holiday/parser"
|
6
|
+
require "#{File.dirname(__FILE__)}/holiday/query"
|
7
|
+
|
8
|
+
module Holiday
|
9
|
+
class << self
|
10
|
+
attr_accessor :yaml_file, :country
|
11
|
+
|
12
|
+
def find(holiday,year=nil)
|
13
|
+
year = year || Time.now.year
|
14
|
+
|
15
|
+
holiday = holiday.to_s
|
16
|
+
occurrs = Query.find(holiday)
|
17
|
+
parts = Parser.parse(occurrs)
|
18
|
+
|
19
|
+
date = if parts.kind_of?(Hash)
|
20
|
+
d = Date.parse("#{parts[:month]} 1 #{year}")
|
21
|
+
weeks = (d.beginning_of_month..d.end_of_month).group_by{|d| d.wday}
|
22
|
+
weeks[parts[:wday]][parts[:occurrence]-1]
|
23
|
+
else
|
24
|
+
Date.parse([parts, year].join(" "))
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns all holidays
|
29
|
+
def all
|
30
|
+
Builder.build.keys
|
31
|
+
end
|
32
|
+
|
33
|
+
# holiday = Holiday.all.map { |h| string.scan(h) }.uniq.flatten # return match
|
34
|
+
def scan(string)
|
35
|
+
all.map{ |h| string.scan(h) }.uniq.flatten.join
|
36
|
+
end
|
37
|
+
|
38
|
+
def include?(holiday)
|
39
|
+
all.keys.include?(holiday)
|
40
|
+
end
|
41
|
+
|
42
|
+
def yaml
|
43
|
+
@yaml ||= YAML.load_file(yaml_file)["holiday"]
|
44
|
+
end
|
45
|
+
|
46
|
+
def holidays
|
47
|
+
yaml[country].keys
|
48
|
+
end
|
49
|
+
|
50
|
+
def country_holidays
|
51
|
+
yaml[country]
|
52
|
+
end
|
53
|
+
|
54
|
+
# TODO add method missing to allow for methods like...
|
55
|
+
# Holiday.christmas_2005
|
56
|
+
# Holiday.thanksgiving(1999)
|
57
|
+
# Holiday.third_thursday_in_march(2005)
|
58
|
+
end
|
59
|
+
end
|
data/lib/holiday.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
holiday:
|
2
|
+
US:
|
3
|
+
christmas:
|
4
|
+
when: december 25th
|
5
|
+
as: christmas
|
6
|
+
halloween:
|
7
|
+
when: october 31st
|
8
|
+
as: halloween, all hallows eve
|
9
|
+
labor_day:
|
10
|
+
when: 1st monday in september
|
11
|
+
as: labor day, labour day
|
12
|
+
thanksgiving:
|
13
|
+
when: 4th thursday in november
|
14
|
+
as: thanksgiving, turkey day
|
15
|
+
CA:
|
16
|
+
christmas:
|
17
|
+
when: december 25th
|
18
|
+
as: christmas, noël
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Holiday
|
2
|
+
# Sets up a container for holidays based on keys and alternative holiday names from yaml file.
|
3
|
+
class Builder
|
4
|
+
class << self
|
5
|
+
def build
|
6
|
+
return @container if defined?(@container)
|
7
|
+
@container = {}
|
8
|
+
Holiday.holidays.each do |holiday|
|
9
|
+
Holiday.country_holidays[holiday]["as"].split(",").each do |other|
|
10
|
+
@container[holiday] = holiday # yaml key should be looked against as well
|
11
|
+
@container[other.strip] = holiday # natural name, labour day, is a key
|
12
|
+
end
|
13
|
+
end
|
14
|
+
@container
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Holiday
|
2
|
+
# Extracts the relevant information from strings
|
3
|
+
class Parser
|
4
|
+
class << self
|
5
|
+
def parse(string)
|
6
|
+
if string =~ /in/
|
7
|
+
w,m,o = weekday(string), month(string), occurrence(string)
|
8
|
+
d = wday(w)
|
9
|
+
|
10
|
+
{ :weekday => w, :wday => d, :month => m, :occurrence => o }
|
11
|
+
else
|
12
|
+
string
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def weekday(string)
|
17
|
+
(string.split(' ').map(&:downcase) & weekdays).join
|
18
|
+
end
|
19
|
+
|
20
|
+
def month(string)
|
21
|
+
(string.split(' ').map(&:downcase) & months).join
|
22
|
+
end
|
23
|
+
|
24
|
+
def occurrence(string)
|
25
|
+
string.scan(/\d/).join.to_i
|
26
|
+
end
|
27
|
+
|
28
|
+
def wday(which)
|
29
|
+
weekdays.index(which)
|
30
|
+
end
|
31
|
+
|
32
|
+
def weekdays
|
33
|
+
%W(sunday monday tuesday wednesday thursday friday saturday)
|
34
|
+
end
|
35
|
+
|
36
|
+
def months
|
37
|
+
%W(january february march april may june july august september october november december)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/test/helper.rb
ADDED
data/test/holiday.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
holiday:
|
2
|
+
US:
|
3
|
+
christmas:
|
4
|
+
when: december 25th
|
5
|
+
as: christmas
|
6
|
+
halloween:
|
7
|
+
when: october 31st
|
8
|
+
as: halloween, all hallows eve
|
9
|
+
labor_day:
|
10
|
+
when: 1st monday in september
|
11
|
+
as: labor day, labour day
|
12
|
+
thanksgiving:
|
13
|
+
when: 4th thursday in november
|
14
|
+
as: thanksgiving, turkey day
|
15
|
+
CA:
|
16
|
+
christmas:
|
17
|
+
when: december 25th
|
18
|
+
as: christmas, noël
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'helper.rb'
|
2
|
+
|
3
|
+
class HolidayTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
Holiday.yaml_file = "#{File.dirname(__FILE__)}/holiday.yml"
|
6
|
+
Holiday.country = "US"
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_builder
|
10
|
+
assert Holiday::Builder.build
|
11
|
+
assert_kind_of Hash, Holiday::Builder.build
|
12
|
+
|
13
|
+
assert_equal "halloween", Holiday::Builder.build["all hallows eve"]
|
14
|
+
assert_equal "labor_day", Holiday::Builder.build["labour day"]
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'helper.rb'
|
2
|
+
|
3
|
+
class HolidayTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
Holiday.yaml_file = "#{File.dirname(__FILE__)}/holiday.yml"
|
6
|
+
Holiday.country = "US"
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_parser_parse
|
10
|
+
assert_kind_of Hash, Holiday::Parser.parse("1st monday in june")
|
11
|
+
parts = {:weekday => "thursday", :occurrence => 2, :month => "october", :wday=>4}
|
12
|
+
assert_equal parts, Holiday::Parser.parse("2nd thursday in october"), Holiday::Parser.parse('2nd thursday in october')
|
13
|
+
assert_equal "december 25th", Holiday::Parser.parse("december 25th")
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_parser_wday
|
17
|
+
assert_equal 0, Holiday::Parser.wday("sunday")
|
18
|
+
assert_equal 1, Holiday::Parser.wday("monday")
|
19
|
+
assert_equal 2, Holiday::Parser.wday("tuesday")
|
20
|
+
assert_equal 3, Holiday::Parser.wday("wednesday")
|
21
|
+
assert_equal 4, Holiday::Parser.wday("thursday")
|
22
|
+
assert_equal 5, Holiday::Parser.wday("friday")
|
23
|
+
assert_equal 6, Holiday::Parser.wday("saturday")
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_parser_weekday
|
27
|
+
assert_equal "monday", Holiday::Parser.weekday("today is monday")
|
28
|
+
assert_equal "friday", Holiday::Parser.weekday("thank goodness it's Friday")
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_parser_month
|
32
|
+
assert_equal "january", Holiday::Parser.month("january is the first month of the year")
|
33
|
+
assert_equal "october", Holiday::Parser.month("halloween is a holiday that occurrs in october")
|
34
|
+
assert_equal "december", Holiday::Parser.month("christmas is in December")
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_parser_occurrence
|
38
|
+
assert_equal 1, Holiday::Parser.occurrence("i'm looking for the number 1")
|
39
|
+
assert_equal 4, Holiday::Parser.occurrence("i love the 4th of july")
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'helper.rb'
|
2
|
+
|
3
|
+
class HolidayTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
Holiday.yaml_file = "#{File.dirname(__FILE__)}/holiday.yml"
|
6
|
+
Holiday.country = "US"
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_query
|
10
|
+
assert_kind_of String, Holiday::Query.find("christmas")
|
11
|
+
assert_equal "december 25th", Holiday::Query.find("christmas")
|
12
|
+
assert_equal "october 31st", Holiday::Query.find("all hallows eve"), Holiday::Query.find("all hallows eve")
|
13
|
+
assert_equal "1st monday in september", Holiday::Query.find("labour day"), Holiday::Query.find("labour day")
|
14
|
+
assert_equal Holiday::Query.find("labour day"), Holiday::Query.find("labor_day"), Holiday::Query.find("labour day")
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'helper.rb'
|
2
|
+
|
3
|
+
class HolidayTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
Holiday.yaml_file = "#{File.dirname(__FILE__)}/holiday.yml"
|
6
|
+
Holiday.country = "US"
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_config
|
10
|
+
assert "holiday.yml", Holiday.yaml_file
|
11
|
+
assert "US", Holiday.country
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_yaml_file_loaded
|
15
|
+
assert_kind_of Hash, Holiday.yaml
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_country_holidays
|
19
|
+
assert Holiday.holidays.include?("christmas"), Holiday.holidays.join(" ")
|
20
|
+
assert Holiday.holidays.include?("labor_day"), Holiday.holidays.join(" ")
|
21
|
+
|
22
|
+
Holiday.country = "CA"
|
23
|
+
assert_equal "CA", Holiday.country
|
24
|
+
assert Holiday.holidays.include?("christmas")
|
25
|
+
assert !Holiday.holidays.include?("labor_day")
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_scan_string
|
29
|
+
assert_equal "labour day", Holiday.scan("labour day 2009"), Holiday.scan("hello labour day world")
|
30
|
+
assert_equal "turkey day", Holiday.scan("turkey day 2007"), Holiday.scan("turkey day 2007")
|
31
|
+
assert_equal false, Holiday.scan("octoberfest 1990").present?, Holiday.scan("octoberfest 1990")
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_holiday_find
|
35
|
+
assert_kind_of Date, Holiday.find(:christmas, 2005)
|
36
|
+
|
37
|
+
# by keys and by alternate names
|
38
|
+
assert_equal "2005-12-25", Holiday.find(:christmas, 2005).to_s # symbol
|
39
|
+
assert_equal "2005-12-25", Holiday.find("christmas", 2005).to_s # or string
|
40
|
+
assert_equal "2004-09-06", Holiday.find("labor_day", 2004).to_s
|
41
|
+
assert_equal "2004-09-06", Holiday.find("labor day", 2004).to_s
|
42
|
+
assert_equal "2004-09-06", Holiday.find("labour day", 2004).to_s
|
43
|
+
assert_equal "1999-11-25", Holiday.find("thanksgiving", 1999).to_s
|
44
|
+
assert_equal "1999-11-25", Holiday.find("turkey day", 1999).to_s
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_year_defaults_to_this_year
|
49
|
+
# defaults to current year
|
50
|
+
assert_equal "#{Time.now.year}-12-25", Holiday.find("christmas").to_s
|
51
|
+
assert_equal "#{Time.now.year}-11-24", Holiday.find("thanksgiving").to_s
|
52
|
+
end
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: holiday
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sean Behan
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-12 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: &70225950789700 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70225950789700
|
25
|
+
description: Holiday is a gem that lets you configure holidays for any country using
|
26
|
+
a simple YAML file format.
|
27
|
+
email:
|
28
|
+
- bseanvt@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- Gemfile
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- holiday.gemspec
|
38
|
+
- lib/holiday.rb
|
39
|
+
- lib/holiday.yml
|
40
|
+
- lib/holiday/builder.rb
|
41
|
+
- lib/holiday/parser.rb
|
42
|
+
- lib/holiday/query.rb
|
43
|
+
- lib/holiday/version.rb
|
44
|
+
- test/helper.rb
|
45
|
+
- test/holiday.yml
|
46
|
+
- test/holiday_builder_test.rb
|
47
|
+
- test/holiday_parser_test.rb
|
48
|
+
- test/holiday_query_test.rb
|
49
|
+
- test/holiday_test.rb
|
50
|
+
homepage: https://github.com/bseanvt/holiday
|
51
|
+
licenses: []
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project: holiday
|
70
|
+
rubygems_version: 1.8.7
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Holiday is a gem that lets you configure holidays for any country using a
|
74
|
+
simple YAML file format.
|
75
|
+
test_files:
|
76
|
+
- test/helper.rb
|
77
|
+
- test/holiday.yml
|
78
|
+
- test/holiday_builder_test.rb
|
79
|
+
- test/holiday_parser_test.rb
|
80
|
+
- test/holiday_query_test.rb
|
81
|
+
- test/holiday_test.rb
|