Module: WeekOfMonth::Week
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_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- Date.new(2012,1,1).week_split => [[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
143 144 145 |
# File 'lib/modules/week.rb', line 143 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"
111 112 113 114 115 |
# File 'lib/modules/week.rb', line 111 constants.select{|x| x.to_s.match("WEEK_OF_MONTH_IN_")}.each do |const| define_method(const.to_s.downcase) do eval "#{const.to_s}[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]
197 198 199 |
# File 'lib/modules/week.rb', line 197 def current_week week_split.select{|c| c.include?((self.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
133 134 135 |
# File 'lib/modules/week.rb', line 133 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
123 124 125 |
# File 'lib/modules/week.rb', line 123 def days_past_in_week self.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
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/modules/week.rb', line 153 def end_of_week if current_week.index(self.day) == 6 self.class.new(year,month,current_week.last) elsif current_week.index(self.day) < 6 if self.class == Date self + (6 - current_week.index(self.day)) elsif self.class == Time self + (60 * 60 * 24 * (6 - current_week.index(self.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
48 49 50 |
# File 'lib/modules/week.rb', line 48 def first_week? week_split[0].include?((self.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
36 37 38 39 40 41 42 |
# File 'lib/modules/week.rb', line 36 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
64 65 66 |
# File 'lib/modules/week.rb', line 64 def last_week? week_split.last.include?((self.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
171 172 173 174 175 176 177 |
# File 'lib/modules/week.rb', line 171 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
185 186 187 188 189 190 191 |
# File 'lib/modules/week.rb', line 185 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
56 57 58 |
# File 'lib/modules/week.rb', line 56 def second_week? week_split[1].include?((self.day)) end |
#total_weeks ⇒ Fixnum
returns total number of weeks in month Date.new(2012,11,8).total_weeks
=> 5
72 73 74 |
# File 'lib/modules/week.rb', line 72 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
80 81 82 |
# File 'lib/modules/week.rb', line 80 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
14 15 16 17 18 |
# File 'lib/modules/week.rb', line 14 def week_of_month week_split.each_with_index do |o,i| return (i + 1) if o.include?(self.day) end end |
#week_split ⇒ Array
returns week split of the month for the given date example- Date.new(2012,1,1).week_split
=> [[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]
101 102 103 |
# File 'lib/modules/week.rb', line 101 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
88 89 90 |
# File 'lib/modules/week.rb', line 88 def working_day? !week_end? end |