Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom holidays #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ Get holidays in Japan.
>> holidays = HolidayJp.between(Date.new(2010, 9, 14), Date.new(2010, 9, 21))
>> holidays.first.name # 敬老の日

# Loading Custom Definitions
>> my_holiday = Date.new(2015, 5, 13)
>> HolidayJp.holiday? my_holiday # false
>> HolidayJp.add_custom_holiday_sources '/path/to/custom_holidays_1.yml', '/path/to/custom_holidays_2.yml'
>> HolidayJp.holiday? my_holiday # true

## Note on Patches/Pull Requests

* Fork the project.
Expand Down
43 changes: 36 additions & 7 deletions lib/holiday_jp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,42 @@
require 'holiday_jp/holidays'

module HolidayJp
def self.between(start, last)
Holidays.new.holidays.find_all do |date, _holiday|
start <= date && date <= last
end.map(&:last)
end

def self.holiday?(date)
Holidays.new.holidays[date]
class << self
def between(start, last)
Holidays.new.holidays.find_all do |date, _holiday|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer select over find_all.

start <= date && date <= last
end.map(&:last)
end

def holiday?(date)
Holidays.new.holidays[date]
end

def add_custom_holiday_sources(*files)
files.to_a.flatten.compact.uniq.each do |file|
next unless File.exist? file
add_custom_holiday_source file
end
end

def holiday_sources
default_holiday_sources.concat(custom_holiday_sources).flatten.freeze
end

private

def add_custom_holiday_source(source)
@custom_holiday_sources ||= []
@custom_holiday_sources << source
end

def default_holiday_sources
[File.expand_path("../../holidays.yml", __FILE__)]
end

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at body end.

def custom_holiday_sources
@custom_holiday_sources.to_a
end
end
end
13 changes: 10 additions & 3 deletions lib/holiday_jp/holidays.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ class Holidays

def initialize
@holidays = {}
yaml = YAML.load_file(File.expand_path('../../../holidays.yml', __FILE__))
yaml.map do |key, value|
@holidays[key] = Holiday.new(key, value)
holiday_sources.each do |source|
YAML.load_file(source).each do |key, value|
@holidays[key] = Holiday.new(key, value)
end
end
end

private

def holiday_sources
HolidayJp.holiday_sources
end
end
end
1 change: 1 addition & 0 deletions test/custom_holidays_1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2015-05-13: 誕生日
1 change: 1 addition & 0 deletions test/custom_holidays_2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2015-12-07: 創立記念日
18 changes: 18 additions & 0 deletions test/test_holiday_jp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,22 @@ def test_mountain_day_from_2016
assert HolidayJp.holiday?(Date.new(year, 8, 11))
end
end

def test_custom_holiday
my_holiday_1 = Date.new(2015, 5, 13)
my_holiday_2 = Date.new(2015, 12, 7)

assert !HolidayJp.holiday?(my_holiday_1)
assert !HolidayJp.holiday?(my_holiday_2)

custom_holidays_1 = File.expand_path("../custom_holidays_1.yml", __FILE__)
custom_holidays_2 = File.expand_path("../custom_holidays_2.yml", __FILE__)
HolidayJp.add_custom_holiday_sources custom_holidays_1, custom_holidays_2
assert HolidayJp.holiday?(my_holiday_1)
assert HolidayJp.holiday?(my_holiday_2)

custom_holiday = HolidayJp.holiday?(my_holiday_1)
assert_equal custom_holiday.name, "誕生日"
assert_equal custom_holiday.name_en, nil
end
end