Module: WeekOfMonth::Week
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
-
#beginning_of_week ⇒ Date || Time
it returns date of the first day(sunday) of the week Date.new(2012,11,15).beginning_of_week => #<Date: 2012-11-12 ((2456244j,0s,0n),+0s,2299161j)> Time.new(2012,11,30).beginning_of_week => 2012-11-29 23:59:55 +0530.
-
#const ⇒ String
this code generates method like ‘week_of_month_eng’, ‘week_of_month_fr’ etc.
-
#current_week ⇒ Array
it returns array of days in current week.
-
#days_left_in_week ⇒ Fixnum
it returns days left in the week Date.new(2012,11,15).days_left_in_week => 3 Time.new(2012,11,30).days_left_in_week => 2.
-
#days_past_in_week ⇒ Fixnum
it returns days past in the week Date.new(2012,11,15).days_past_in_week => 4 Time.new(2012,11,30).days_past_in_week => 5.
-
#end_of_week ⇒ Date || Time
it returns date of the last day(saturday) of the week Date.new(2012,11,15).end_of_week => #<Date: 2012-11-19 ((2456251j,0s,0n),+0s,2299161j)> Time.new(2012,11,30).end_of_week => 2012-11-30 00:00:02 +0530.
-
#first_week? ⇒ Boolean
checks whether the given day lies in first week of month Date.new(2012,11,1).first_week? => true.
-
#general_week_of_month ⇒ Fixnum
November week_split would like this [[nil, nil, nil, nil, nil, 1, 2], [3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16], [17, 18, 19, 20, 21, 22, 23], [24, 25, 26, 27, 28, 29, 30]].
-
#last_week? ⇒ Boolean
checks whether the given day lies in last week of month Date.new(2012,11,8).last_week? => false.
-
#next_week ⇒ Date || Time
it returns date of the next week day.
-
#previous_week ⇒ Date || Time
it returns date of the previous week day.
-
#second_week? ⇒ Boolean
checks whether the given day lies in second week of month Date.new(2012,11,8).second_week? => true.
-
#total_weeks ⇒ Fixnum
returns total number of weeks in month Date.new(2012,11,8).total_weeks => 5.
-
#week_end? ⇒ Boolean
checks whether the given day is saturday or sunday.
-
#week_of_month ⇒ Fixnum
returns week of month for a given date Date.new(2012,11,15).week_of_month => 3.
-
#week_split ⇒ Array
returns week split of the month for the given date example- Optional argument to change first week day to Monday Date.new(2012,1,1).week_split -> Sunday is first week day => [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21], [22, 23, 24, 25, 26, 27, 28], [29, 30, 31] Date.new(2012,1,1).week_split(true) -> Monday is first week day => [[nil,nil,nil,nil,nil,nil,1], [2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15], [16, 17, 18, 19, 20, 21, 22], [23, 24, 25, 26, 27, 28, 29], [30, 31].
-
#working_day? ⇒ Boolean
checks whether the given day is not saturday or sunday.
Instance Method Details
#beginning_of_week ⇒ Date || Time
it returns date of the first day(sunday) of the week Date.new(2012,11,15).beginning_of_week
=> #<Date: 2012-11-12 ((2456244j,0s,0n),+0s,2299161j)>
Time.new(2012,11,30).beginning_of_week
=> 2012-11-29 23:59:55 +0530
150 151 152 |
# File 'lib/modules/week.rb', line 150 def beginning_of_week self.class.new(year, month, current_week.detect { |i| !i.nil? }) end |
#const ⇒ String
this code generates method like ‘week_of_month_eng’, ‘week_of_month_fr’ etc. Date.new(2012,11,15).week_of_month_in_eng
=> 'Third'
Date.new(2012,11,30).week_of_month_in_fr
=> "Cinquième"
118 119 120 121 122 |
# File 'lib/modules/week.rb', line 118 constants.select { |x| x.to_s.match('WEEK_OF_MONTH_IN_') }.each do |const| define_method(const.to_s.downcase) do eval "#{const}[week_of_month]" end end |
#current_week ⇒ Array
it returns array of days in current week. Date.new(2013,1,13).current_week
> [7, 8, 9, 10, 11, 12, 13]
204 205 206 |
# File 'lib/modules/week.rb', line 204 def current_week week_split.select { |c| c.include?(day) }.flatten end |
#days_left_in_week ⇒ Fixnum
it returns days left in the week Date.new(2012,11,15).days_left_in_week
=> 3
Time.new(2012,11,30).days_left_in_week
=> 2
140 141 142 |
# File 'lib/modules/week.rb', line 140 def days_left_in_week 7 - days_past_in_week end |
#days_past_in_week ⇒ Fixnum
it returns days past in the week Date.new(2012,11,15).days_past_in_week
=> 4
Time.new(2012,11,30).days_past_in_week
=> 5
130 131 132 |
# File 'lib/modules/week.rb', line 130 def days_past_in_week to_date.cwday end |
#end_of_week ⇒ Date || Time
it returns date of the last day(saturday) of the week Date.new(2012,11,15).end_of_week
=> #<Date: 2012-11-19 ((2456251j,0s,0n),+0s,2299161j)>
Time.new(2012,11,30).end_of_week
=> 2012-11-30 00:00:02 +0530
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/modules/week.rb', line 160 def end_of_week if current_week.index(day) == 6 self.class.new(year, month, current_week.last) elsif current_week.index(day) < 6 if self.class == Date self + (6 - current_week.index(day)) elsif self.class == Time self + (60 * 60 * 24 * (6 - current_week.index(day))) end end end |
#first_week? ⇒ Boolean
checks whether the given day lies in first week of month Date.new(2012,11,1).first_week?
=> true
47 48 49 |
# File 'lib/modules/week.rb', line 47 def first_week? week_split[0].include?(day) end |
#general_week_of_month ⇒ Fixnum
November week_split would like this [[nil, nil, nil, nil, nil, 1, 2],
[3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16],
[17, 18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29, 30]]
The First Day of week decide its month of week. The W1 Nov should be Nov 3 - Nov 9
Date.new(2012,11,15).general_week_of_month
=> 2
Date.new(2013,11,3).general_week_of_month
=> 1
35 36 37 38 39 40 41 |
# File 'lib/modules/week.rb', line 35 def general_week_of_month if week_split.first.first.nil? week_of_month - 1 else week_of_month end end |
#last_week? ⇒ Boolean
checks whether the given day lies in last week of month Date.new(2012,11,8).last_week?
=> false
63 64 65 |
# File 'lib/modules/week.rb', line 63 def last_week? week_split.last.include?(day) end |
#next_week ⇒ Date || Time
it returns date of the next week day. Date.new(2012,11,15).next_week
=> #<Date: 2012-11-22 ((2456254j,0s,0n),+0s,2299161j)>
Time.new(2012,11,30).next_week
=> 2012-11-30 00:00:07 +0530
178 179 180 181 182 183 184 |
# File 'lib/modules/week.rb', line 178 def next_week if self.class == Date self + 7 elsif self.class == Time self + (60 * 60 * 24 * 7) end end |
#previous_week ⇒ Date || Time
it returns date of the previous week day. Date.new(2012,11,15).previous_week
=> #<Date: 2012-11-08 ((2456240j,0s,0n),+0s,2299161j)>
Time.new(2012,11,30).previous_week
=> 2012-11-29 23:59:53 +0530
192 193 194 195 196 197 198 |
# File 'lib/modules/week.rb', line 192 def previous_week if self.class == Date self - 7 elsif self.class == Time self - (60 * 60 * 24 * 7) end end |
#second_week? ⇒ Boolean
checks whether the given day lies in second week of month Date.new(2012,11,8).second_week?
=> true
55 56 57 |
# File 'lib/modules/week.rb', line 55 def second_week? week_split[1].include?(day) end |
#total_weeks ⇒ Fixnum
returns total number of weeks in month Date.new(2012,11,8).total_weeks
=> 5
71 72 73 |
# File 'lib/modules/week.rb', line 71 def total_weeks week_split.size end |
#week_end? ⇒ Boolean
checks whether the given day is saturday or sunday. Date.new(2012,11,8).week_end?
=> false
79 80 81 |
# File 'lib/modules/week.rb', line 79 def week_end? saturday? || sunday? end |
#week_of_month ⇒ Fixnum
returns week of month for a given date Date.new(2012,11,15).week_of_month
=> 3
13 14 15 16 17 |
# File 'lib/modules/week.rb', line 13 def week_of_month week_split.each_with_index do |o, i| return (i + 1) if o.include?(day) end end |
#week_split ⇒ Array
returns week split of the month for the given date example- Optional argument to change first week day to Monday Date.new(2012,1,1).week_split -> Sunday is first week day
=> [[1, 2, 3, 4, 5, 6, 7],
[8, 9, 10, 11, 12, 13, 14],
[15, 16, 17, 18, 19, 20, 21],
[22, 23, 24, 25, 26, 27, 28],
[29, 30, 31]
Date.new(2012,1,1).week_split(true) -> Monday is first week day
=> [[nil,nil,nil,nil,nil,nil,1],
[2, 3, 4, 5, 6, 7, 8],
[9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22],
[23, 24, 25, 26, 27, 28, 29],
[30, 31]
108 109 110 |
# File 'lib/modules/week.rb', line 108 def week_split days_array.each_slice(7).to_a end |
#working_day? ⇒ Boolean
checks whether the given day is not saturday or sunday. Date.new(2012,11,8).working_day?
=> true
87 88 89 |
# File 'lib/modules/week.rb', line 87 def working_day? !week_end? end |