-
Notifications
You must be signed in to change notification settings - Fork 57
Add custom holidays #16
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,13 +3,34 @@ | |
| 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| | ||
| 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 add_custom_holiday_source(source) | ||
| @custom_holiday_sources ||= [] | ||
| @custom_holiday_sources << source | ||
| end | ||
|
|
||
| def default_holiday_sources | ||
| [File.expand_path('../../holidays.yml', __FILE__)].concat(@custom_holiday_sources.to_a).flatten.freeze | ||
| end | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra empty line detected at body end. |
||
| end | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra empty line detected at body end. |
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,10 +7,18 @@ 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.default_holiday_sources | ||
| end | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra empty line detected at body end. |
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 2015-05-13: 誕生日 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 2015-12-7: 創立記念日 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,4 +30,20 @@ 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) | ||
|
|
||
| HolidayJp.add_custom_holiday_sources File.expand_path('../custom_holidays_1.yml', __FILE__), File.expand_path('../custom_holidays_2.yml', __FILE__) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [151/80] |
||
| assert HolidayJp.holiday?(my_holiday_1) | ||
| assert HolidayJp.holiday?(my_holiday_2) | ||
|
|
||
| custom_holiday = HolidayJp.holiday?(my_holiday_1) | ||
| assert_equal custom_holiday.name, '誕生日' | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
| assert_equal custom_holiday.name_en, nil | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer
selectoverfind_all.