Module: WeekOfMonth::Month
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
-
#beginning_of_month ⇒ Date
returns date of first day of month for a given date.
-
#day_name ⇒ Array
this code generates method named like ‘all_mondays_in_month’, ‘all_tuesdays_in_month’ etc.
-
#end_of_month ⇒ Date
returns date of last day of month for a given date.
-
#last_day_of_month ⇒ Fixnum
returns day of last day of month for a given date.
-
#month_name ⇒ Boolean
this code generates method named like january?..december? to check whether a month is january or march? etc.
-
#name_of_month ⇒ String
returns name of month for a given date.
Instance Method Details
#beginning_of_month ⇒ Date
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)>
43 44 45 |
# File 'lib/modules/month.rb', line 43 def beginning_of_month self.class.new(year,month,1) end |
#day_name ⇒ Array
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]
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_month ⇒ Date
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)>
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_month ⇒ Fixnum
returns day of last day of month for a given date. Date.new(2012,11,1).last_day_of_month
=> 30
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_name ⇒ Boolean
this code generates method named like january?..december? to check whether a month is january or march? etc.
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_month ⇒ String
returns name of month for a given date.
Date.new(2012,11,1).name_of_month
=> "November"
51 52 53 |
# File 'lib/modules/month.rb', line 51 def name_of_month self.class.new(year,month,day).strftime('%B') end |