Skip to content

Commit

Permalink
Refactor: simplify Date core extension usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Trimble committed Dec 13, 2015
1 parent 4c6f8b2 commit 9316f31
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 37 deletions.
15 changes: 5 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ Multiple files can also be passed:

### Extending Ruby's Date class

We offer two sets of functionality for the Date class that can be optionally included by your application.

To include new instance methods:
To extend the 'Date' class:

require 'holidays/core_extensions/date'
Date.include Holidays::CoreExtensions::Date::Include
class Date
include Holidays::CoreExtensions::Date
end

Now you can check which holidays occur in Iceland on January 1, 2008:

Expand All @@ -103,12 +103,7 @@ Or lookup Canada Day in different regions:
d.holiday?(:fr) # France
=> false

To include new class methods:

require 'holidays/core_extensions/date'
Date.extend Holidays::CoreExtensions::Date::Extend

Now you can calculate the day of the month:
Or you can calculate the day of the month:

Date.calculate_mday(2015, 4, :first, 2)
=> 7
Expand Down
50 changes: 26 additions & 24 deletions lib/holidays/core_extensions/date.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
module Holidays
module CoreExtensions
module Date
module Include
# Get holidays on the current date.
#
# Returns an array of hashes or nil. See Holidays#between for options
# and the output format.
#
# Date.civil('2008-01-01').holidays(:ca_)
# => [{:name => 'New Year\'s Day',...}]
#
# Also available via Holidays#on.
def holidays(*options)
Holidays.on(self, options)
end
def self.included(base)
base.extend ClassMethods
end

# Check if the current date is a holiday.
#
# Returns true or false.
#
# Date.civil('2008-01-01').holiday?(:ca)
# => true
def holiday?(*options)
holidays = self.holidays(options)
holidays && !holidays.empty?
end
# Get holidays on the current date.
#
# Returns an array of hashes or nil. See Holidays#between for options
# and the output format.
#
# Date.civil('2008-01-01').holidays(:ca_)
# => [{:name => 'New Year\'s Day',...}]
#
# Also available via Holidays#on.
def holidays(*options)
Holidays.on(self, options)
end

# Check if the current date is a holiday.
#
# Returns true or false.
#
# Date.civil('2008-01-01').holiday?(:ca)
# => true
def holiday?(*options)
holidays = self.holidays(options)
holidays && !holidays.empty?
end

module Extend
module ClassMethods
def calculate_mday(year, month, week, wday)
Holidays.calculate_mday(year, month, week, wday)
end
Expand Down
8 changes: 5 additions & 3 deletions test/holidays/core_extensions/test_date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

require 'holidays/core_extensions/date'

class DateTests < Test::Unit::TestCase
class Date
include Holidays::CoreExtensions::Date
end

class CoreExtensionDateTests < Test::Unit::TestCase
def setup
Date.include Holidays::CoreExtensions::Date::Include
Date.extend Holidays::CoreExtensions::Date::Extend
@date = Date.civil(2008,1,1)
end

Expand Down

0 comments on commit 9316f31

Please sign in to comment.