Module: WeekOfMonth::Month

Includes:
Constant
Included in:
Date, Time
Defined in:
lib/modules/month.rb

Constant Summary

Constants included from Constant

Constant::DAYS_IN_SEQUENCE, Constant::MONTH_WITH_DAY, Constant::MONTH_WITH_SEQUENCE, Constant::WEEKS_IN_SEQUENCE, Constant::WEEK_OF_MONTH_IN_ENG, Constant::WEEK_OF_MONTH_IN_FR, Constant::WEEK_OF_MONTH_IN_GER, Constant::WEEK_OF_MONTH_IN_JA

Instance Method Summary collapse

Instance Method Details

#beginning_of_monthDate

returns date of first day of month for a given date. Date.new(2012,11,1).beginning_of_month

=> #<Date: 2012-11-01 ((2456233j,0s,0n),+0s,2299161j)>

Returns:



42
43
44
# File 'lib/modules/month.rb', line 42

def beginning_of_month
  self.class.new(year, month, 1)
end

#day_nameDate

this code generates method named like ‘first_monday_in_month’, ‘second_tuesday_in_month’, ‘last_friday_in_month’ etc. for first, second, third, fourth and last seven days of week Date.new(2014,11).third_saturday_in_month

=> #<Date: 2014-11-15>

Returns:



59
60
61
62
63
64
# File 'lib/modules/month.rb', line 59

DAYS_IN_SEQUENCE.each_with_index do |day_name, i|
  method_name = "all_#{day_name.downcase}s_in_month".to_sym
  define_method(method_name) do
    week_split.map { |d| d[i] }.compact
  end
end

#end_of_monthDate

returns date of last day of month for a given date. Date.new(2012,11,1).end_of_month

=> #<Date: 2012-11-30 ((2456262j,0s,0n),+0s,2299161j)>

Returns:



34
35
36
# File 'lib/modules/month.rb', line 34

def end_of_month
  self.class.new(year, month, last_day_of_month)
end

#last_day_of_monthFixnum

returns day of last day of month for a given date. Date.new(2012,11,1).last_day_of_month

=> 30

Returns:

  • (Fixnum)


22
23
24
25
26
27
28
# File 'lib/modules/month.rb', line 22

def last_day_of_month
  if leap? && february?
    29
  else
    MONTH_WITH_DAY[MONTH_WITH_SEQUENCE.key(month)]
  end
end

#month_nameBoolean

this code generates method named like january?..december? to check whether a month is january or march? etc.

Returns:

  • (Boolean)


12
13
14
15
16
# File 'lib/modules/month.rb', line 12

MONTH_WITH_DAY.keys.each do |month_name|
  define_method((month_name.to_s + '?').to_sym) do
    MONTH_WITH_SEQUENCE[month_name] == month
  end
end

#name_of_monthString

returns name of month for a given date. Date.new(2012,11,1).name_of_month

=> "November"

Returns:

  • (String)


50
51
52
# File 'lib/modules/month.rb', line 50

def name_of_month
  self.class.new(year, month, day).strftime('%B')
end