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::WEEK_OF_MONTH_IN_ENG, Constant::WEEK_OF_MONTH_IN_FR, Constant::WEEK_OF_MONTH_IN_GER, Constant::WEEK_OF_MONTH_IN_JAP

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:



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

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

#day_nameArray

this code generates method named like ‘all_mondays_in_month’, ‘all_tuesdays_in_month’ etc. for all seven days of week Date.new(2012,11,1).all_mondays_in_month

=> [5, 12, 19, 26]

Returns:

  • (Array)


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

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:



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

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)


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

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)


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

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)


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

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