Module: WeekOfMonth::Month
Constant Summary
Constants included from Constant
Constant::DAYS_IN_SEQUENCE, Constant::DAYS_IN_SEQUENCE_STARTING_MONDAY, 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
-
#beginning_of_month ⇒ Date
returns date of first day of month for a given date.
-
#day_name ⇒ Date
this code generates method named like ‘first_monday_in_month’, ‘second_tuesday_in_month’, ‘last_friday_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)>
42 43 44 |
# File 'lib/modules/month.rb', line 42 def beginning_of_month self.class.new(year, month, 1) end |
#day_name ⇒ Date
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>
59 60 61 62 63 64 65 66 67 68 |
# 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 if WeekOfMonth.configuration.monday_active week_split.map { |d| d[i-1] }.compact else week_split.map { |d| d[i] }.compact end 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)>
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_month ⇒ Fixnum
returns day of last day of month for a given date. Date.new(2012,11,1).last_day_of_month
=> 30
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_name ⇒ Boolean
this code generates method named like january?..december? to check whether a month is january or march? etc.
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_month ⇒ String
returns name of month for a given date. Date.new(2012,11,1).name_of_month
=> "November"
50 51 52 |
# File 'lib/modules/month.rb', line 50 def name_of_month self.class.new(year, month, day).strftime('%B') end |