holidays 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a6ab4b51615aa61fac469c01fb99d7d94ad6d876
4
- data.tar.gz: 4ee8ff425142c2e74de1ca88784679af0a0c99a5
3
+ metadata.gz: 858b7ca8ee12adf0ca16bd039b094ac5b396592b
4
+ data.tar.gz: 6dea8e8c8abc0ecb1e1983a932aa8877c60544ef
5
5
  SHA512:
6
- metadata.gz: e430c6651f9fd96b2c5ec24fccc1e3f8ac93d11e7726e5f6d4c9265296cfd2ad4bc41da5b629f93d883f0ef05f90471f5a37215585747d76d89be708ad78d259
7
- data.tar.gz: e586b641f155808fbfd03ce5a0af6207a890aa55c7fbb514be390e6082110a894d1fa69f82af44ac999b80bca568298c18468227646ff46a49dbc3a8e86d33c9
6
+ metadata.gz: 9be91c59476aebe6cb91da7ab7c2a7003d0286e2e723c4853f804e62f96b33681bdc2797fe73170dfbd67a659725e2ee34c28717d9da30b0fbc6c523dde90716
7
+ data.tar.gz: 51a8a704f91f3966ecba80796a02d3087db3966afa8a59f492a9efe7ef272f2f00bf98154fde2dd4ff1b7149506d9ef198ba01163f211ac69f6bea650d890aa9
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Ruby Holidays Gem CHANGELOG
2
2
 
3
+ ## 1.2.0
4
+
5
+ * Remove inauguration day from USA Federal Reserve definitions (https://github.com/aripollak)
6
+ * Add caching functionality for date ranges (https://github.com/ndbroadbent & https://github.com/ghiculescu)
7
+
3
8
  ## 1.1.0
4
9
 
5
10
  * Add support to load custom holidays on the fly
data/README.md CHANGED
@@ -1,9 +1,11 @@
1
- # Ruby Holidays Gem
1
+ # Ruby Holidays Gem [![Build Status](https://travis-ci.org/alexdunae/holidays.svg?branch=master)](https://travis-ci.org/alexdunae/holidays)
2
2
 
3
3
  A set of functions to deal with holidays in Ruby.
4
4
 
5
5
  Extends Ruby's built-in Date class and supports custom holiday definition lists.
6
6
 
7
+ Full documentation can be found [here](http://www.rubydoc.info/github/alexdunae/holidays/master/frames).
8
+
7
9
  ## Installation
8
10
 
9
11
  To install the gem from RubyGems:
@@ -94,6 +96,14 @@ Lookup Canada Day in different regions.
94
96
  d.holiday?(:fr) # France
95
97
  => false
96
98
 
99
+ ### Caching Holiday Lookups
100
+
101
+ If you are checking holidays regularly you can cache your results for improved performance. Run this before looking up a holiday (eg. in an initializer):
102
+
103
+ Holidays.cache_between(Time.now, 2.years.from_now, :ca, :us, :observed)
104
+
105
+ Holidays for the regions specified within the dates specified will be pre-calculated.
106
+
97
107
  ### How to contribute
98
108
 
99
109
  To make changes to any of the definitions, edit the YAML files only.
@@ -106,14 +116,8 @@ It is also very appreciated if documentation is attached to the pull request. A
106
116
 
107
117
  ### Credits and code
108
118
 
109
- * Source: https://github.com/alexdunae/holidays
110
- * Docs: http://rdoc.info/github/alexdunae/holidays/master/frames
111
- * Contributors: https://github.com/alexdunae/holidays/contributors
112
- * Build status: http://travis-ci.org/#!/alexdunae/holidays
113
-
114
- Started by [Alex Dunae](http://dunae.ca) (e-mail 'code' at the same domain), 2007-12.
115
- Maintained by [Hana Wang](https://github.com/hahahana), 2013
116
-
117
- On Twitter: @MrMrBug.
119
+ * Started by [Alex Dunae](http://dunae.ca) (e-mail 'code' at the same domain), 2007-12
120
+ * Maintained by [Hana Wang](https://github.com/hahahana), 2013
121
+ * Maintained by [Phil Trimble](https://github.com/ptrimble), 2014-present
118
122
 
119
- Made on Vancouver Island. Maintained in the way more beautiful Houston, TX.
123
+ Plus all of these [wonderful contributors!](https://github.com/alexdunae/holidays/contributors)
data/Rakefile CHANGED
@@ -1,4 +1,5 @@
1
1
  $:.unshift File.expand_path('../lib', __FILE__)
2
+ $:.unshift File.expand_path('../test', __FILE__)
2
3
 
3
4
  require 'bundler/gem_tasks'
4
5
  require 'rake/testtask'
@@ -8,7 +9,7 @@ require 'holidays'
8
9
 
9
10
  Rake::TestTask.new(:test) do |t|
10
11
  t.libs << 'test'
11
- t.test_files = FileList['test/**/test*.rb']
12
+ t.test_files = FileList['test/**/test_*.rb']
12
13
  end
13
14
 
14
15
  task :default => :test
@@ -15,9 +15,6 @@ months:
15
15
  week: 3
16
16
  wday: 1
17
17
  regions: [federal_reserve]
18
- - name: Inauguration Day
19
- function: us_inauguration_day(year)
20
- regions: [federal_reserve]
21
18
  2:
22
19
  - name: Washington's Birthday
23
20
  week: 3
@@ -57,14 +54,6 @@ months:
57
54
  regions: [federal_reserve]
58
55
  mday: 25
59
56
  observed: to_monday_if_sunday
60
- methods:
61
- us_inauguration_day: |
62
- # January 20, every fourth year, following Presidential election
63
- def self.us_inauguration_day(year)
64
- year % 4 == 1 ? 20 : nil
65
- end
66
-
67
-
68
57
  tests: |
69
58
  {Date.civil(2012,1,2) => "New Year's Day",
70
59
  Date.civil(2012,1,16) => "Birthday of Martin Luther King, Jr",
@@ -87,7 +76,7 @@ tests: |
87
76
  Date.civil(2013,11,11) => "Veterans Day",
88
77
  Date.civil(2013,11,28) => "Thanksgiving Day",
89
78
  Date.civil(2013,12,25) => "Christmas Day",
90
-
79
+
91
80
  Date.civil(2014,1,1) => "New Year's Day",
92
81
  Date.civil(2014,1,20) => "Birthday of Martin Luther King, Jr",
93
82
  Date.civil(2014,2,17) => "Washington's Birthday",
@@ -120,8 +109,6 @@ tests: |
120
109
  Date.civil(2016,11,11) => "Veterans Day",
121
110
  Date.civil(2016,11,24) => "Thanksgiving Day",
122
111
  Date.civil(2016,12,26) => "Christmas Day",
123
-
124
- Date.civil(2017,1, 20) => "Inauguration Day",
125
112
  }.each do |date, name|
126
113
  assert_equal name, (Holidays.on(date, :federal_reserve, :observed)[0] || {})[:name]
127
114
  end
data/lib/holidays.rb CHANGED
@@ -51,6 +51,13 @@ module Holidays
51
51
  @@holidays_by_month = {}
52
52
  @@proc_cache = {}
53
53
 
54
+ @@cache = {}
55
+ @@cache_range = {}
56
+ class << self
57
+ def cache_range; @@cache_range; end
58
+ def cache; @@cache; end
59
+ end
60
+
54
61
  WEEKS = {:first => 1, :second => 2, :third => 3, :fourth => 4, :fifth => 5, :last => -1, :second_last => -2, :third_last => -3}
55
62
  MONTH_LENGTHS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
56
63
  DAY_SYMBOLS = Date::DAYNAMES.collect { |n| n.downcase.intern }
@@ -108,16 +115,14 @@ module Holidays
108
115
  end_date = end_date.new_offset(0) + end_date.offset if end_date.respond_to?(:new_offset)
109
116
 
110
117
  # get simple dates
111
- if start_date.respond_to?(:to_date)
112
- start_date = start_date.to_date
113
- else
114
- start_date = Date.civil(start_date.year, start_date.mon, start_date.mday)
115
- end
118
+ start_date, end_date = get_date(start_date), get_date(end_date)
116
119
 
117
- if end_date.respond_to?(:to_date)
118
- end_date = end_date.to_date
119
- else
120
- end_date = Date.civil(end_date.year, end_date.mon, end_date.mday)
120
+ if range = @@cache_range[options]
121
+ if range.begin < start_date && range.end > end_date
122
+ return @@cache[options].select do |holiday|
123
+ holiday[:date] >= start_date && holiday[:date] <= end_date
124
+ end
125
+ end
121
126
  end
122
127
 
123
128
  regions, observed, informal = parse_options(options)
@@ -179,6 +184,13 @@ module Holidays
179
184
  holidays.sort{|a, b| a[:date] <=> b[:date] }
180
185
  end
181
186
 
187
+ # Allows a developer to explicitly calculate and cache holidays within a given period
188
+ def self.cache_between(start_date, end_date, *options)
189
+ start_date, end_date = get_date(start_date), get_date(end_date)
190
+ @@cache[options] = between(start_date, end_date, *options)
191
+ @@cache_range[options] = start_date..end_date
192
+ end
193
+
182
194
  # Merge a new set of definitions into the Holidays module.
183
195
  #
184
196
  # This method is automatically called when including holiday definition
@@ -336,6 +348,14 @@ private
336
348
  return regions, observed, informal
337
349
  end
338
350
 
351
+ def self.get_date(date)
352
+ if date.respond_to?(:to_date)
353
+ date.to_date
354
+ else
355
+ Date.civil(date.year, date.mon, date.mday)
356
+ end
357
+ end
358
+
339
359
  # Derive the containing region from a sub region wild-card or a sub region
340
360
  # and load its definition. (Common code factored out from parse_regions)
341
361
  def self.load_containing_region(sub_reg)
@@ -19,8 +19,7 @@ module Holidays
19
19
  def self.holidays_by_month
20
20
  {
21
21
  1 => [{:mday => 1, :observed => lambda { |date| Holidays.to_monday_if_sunday(date) }, :observed_id => "to_monday_if_sunday", :name => "New Year's Day", :regions => [:federal_reserve]},
22
- {:wday => 1, :week => 3, :name => "Birthday of Martin Luther King, Jr", :regions => [:federal_reserve]},
23
- {:function => lambda { |year| Holidays.us_inauguration_day(year) }, :function_id => "us_inauguration_day(year)", :name => "Inauguration Day", :regions => [:federal_reserve]}],
22
+ {:wday => 1, :week => 3, :name => "Birthday of Martin Luther King, Jr", :regions => [:federal_reserve]}],
24
23
  2 => [{:wday => 1, :week => 3, :name => "Washington's Birthday", :regions => [:federal_reserve]}],
25
24
  5 => [{:wday => 1, :week => -1, :name => "Memorial Day", :regions => [:federal_reserve]}],
26
25
  7 => [{:mday => 4, :observed => lambda { |date| Holidays.to_monday_if_sunday(date) }, :observed_id => "to_monday_if_sunday", :name => "Independence Day", :regions => [:federal_reserve]}],
@@ -33,14 +32,6 @@ module Holidays
33
32
  end
34
33
  end
35
34
 
36
- # January 20, every fourth year, following Presidential election
37
- def self.us_inauguration_day(year)
38
- year % 4 == 1 ? 20 : nil
39
- end
40
-
41
-
42
-
43
-
44
35
 
45
36
  end
46
37
 
@@ -1,3 +1,3 @@
1
1
  module Holidays
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
@@ -28,7 +28,7 @@ class Federal_reserveDefinitionTests < Test::Unit::TestCase # :nodoc:
28
28
  Date.civil(2013,11,11) => "Veterans Day",
29
29
  Date.civil(2013,11,28) => "Thanksgiving Day",
30
30
  Date.civil(2013,12,25) => "Christmas Day",
31
-
31
+
32
32
  Date.civil(2014,1,1) => "New Year's Day",
33
33
  Date.civil(2014,1,20) => "Birthday of Martin Luther King, Jr",
34
34
  Date.civil(2014,2,17) => "Washington's Birthday",
@@ -61,8 +61,6 @@ class Federal_reserveDefinitionTests < Test::Unit::TestCase # :nodoc:
61
61
  Date.civil(2016,11,11) => "Veterans Day",
62
62
  Date.civil(2016,11,24) => "Thanksgiving Day",
63
63
  Date.civil(2016,12,26) => "Christmas Day",
64
-
65
- Date.civil(2017,1, 20) => "Inauguration Day",
66
64
  }.each do |date, name|
67
65
  assert_equal name, (Holidays.on(date, :federal_reserve, :observed)[0] || {})[:name]
68
66
  end
@@ -157,4 +157,19 @@ class HolidaysTests < Test::Unit::TestCase
157
157
  }
158
158
  }
159
159
  end
160
+
161
+ def test_caching
162
+ Holidays.cache_between(Date.civil(2008,3,21), Date.civil(2008,3,25), :ca, :informal)
163
+
164
+ # Test that cache has been set
165
+ cache_key = [:ca, :informal]
166
+ assert_equal Date.civil(2008,3,21), Holidays.cache_range[cache_key].begin
167
+ assert_equal Date.civil(2008,3,25), Holidays.cache_range[cache_key].end
168
+ assert_equal Date.civil(2008,3,21), Holidays.cache[cache_key].first[:date]
169
+ assert_equal Date.civil(2008,3,24), Holidays.cache[cache_key].last[:date]
170
+
171
+ # Test that correct results are returned outside the cache range, and with no caching
172
+ assert_equal 1, Holidays.on(Date.civil(2035,1,1), :ca, :informal).length
173
+ assert_equal 1, Holidays.on(Date.civil(2035,1,1), :us).length
174
+ end
160
175
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: holidays
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Dunae
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-17 00:00:00.000000000 Z
11
+ date: 2015-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -46,6 +46,7 @@ executables: []
46
46
  extensions: []
47
47
  extra_rdoc_files: []
48
48
  files:
49
+ - ".coveralls.yml"
49
50
  - CHANGELOG.md
50
51
  - Gemfile
51
52
  - LICENSE