Last active
December 16, 2015 22:49
-
-
Save jimweirich/5509412 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in Norway | |
module Calendar | |
module Holiday | |
FEBRUARY = 2 | |
NOVEMBER = 11 | |
def mothersday(year) | |
second_sunday_in(FEBRUARY, year) | |
end | |
def fathersday(year) | |
second_sunday_in(NOVEMBER, year) | |
end | |
def next_mothersday(date = Date.today) | |
next_holiday_after(date) { |year| mothersday(year) } | |
end | |
def next_fathersday(date = Date.today) | |
next_holiday_after(date) { |year| fathersday(year) } | |
end | |
private | |
def next_holiday_after(date) | |
this_years = yield(date.year) | |
if this_years >= date | |
this_years | |
else | |
yield(date.year + 1) | |
end | |
end | |
def second_sunday_in(month, year) | |
# Day number 8 always falls within | |
# the second week of the month | |
second_week = Date.new(year, month, 8) | |
second_week + days_until_sunday(second_week) | |
end | |
def days_until_sunday(date) | |
(7 - date.wday) % 7 | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec-given' | |
require './holiday' | |
RSpec::Given.use_natural_assertions | |
describe Calendar::Holiday do | |
include Calendar::Holiday | |
Given(:this_date) { Date.parse("2013-05-03") } | |
Then { mothersday(2013) == Date.parse("2013-02-10") } | |
Then { mothersday(2014) == Date.parse("2014-02-09") } | |
Then { next_mothersday(this_date) == Date.parse("2014-02-09") } | |
Then { fathersday(2012) == Date.parse("2012-11-11") } | |
Then { fathersday(2013) == Date.parse("2013-11-10") } | |
Then { next_fathersday(this_date) == Date.parse("2013-11-10") } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment