Module: Holidays::CoreExtensions::Date

Defined in:
lib/holidays/core_extensions/date.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
# File 'lib/holidays/core_extensions/date.rb', line 4

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#change(options) ⇒ Object

Returns a new Date where one or more of the elements have been changed according to the options parameter. The options parameter is a hash with a combination of these keys: :year, :month, :day.

Date.new(2007, 5, 12).change(day: 1)               # => Date.new(2007, 5, 1)
Date.new(2007, 5, 12).change(year: 2005, month: 1) # => Date.new(2005, 1, 12)


37
38
39
40
41
42
43
# File 'lib/holidays/core_extensions/date.rb', line 37

def change(options)
  ::Date.new(
    options.fetch(:year, year),
    options.fetch(:month, month),
    options.fetch(:day, day)
  )
end

#end_of_monthObject



45
46
47
48
# File 'lib/holidays/core_extensions/date.rb', line 45

def end_of_month
  last_day = ::Time.days_in_month( self.month, self.year )
  change(:day => last_day)
end

#holiday?(*options) ⇒ Boolean

Check if the current date is a holiday.

Returns true or false.

Date.civil('2008-01-01').holiday?(:ca)
=> true

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/holidays/core_extensions/date.rb', line 27

def holiday?(*options)
  holidays = self.holidays(options)
  holidays && !holidays.empty?
end

#holidays(*options) ⇒ Object

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.



17
18
19
# File 'lib/holidays/core_extensions/date.rb', line 17

def holidays(*options)
  Holidays.on(self, options)
end