Module: FindWeek::Week
Constant Summary
Constants included from Constant
Constant::DAYS_IN_SEQUENCE, Constant::MONTH_WITH_DAY, Constant::MONTH_WITH_SEQUENCE, Constant::WEEK_IN_ENG, Constant::WEEK_IN_FR, Constant::WEEK_IN_GER, Constant::WEEK_IN_JAP, Constant::WEEK_IN_TAMIL, Constant::WEEK_IN_TELUGU
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.
-
#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.
-
#find_week ⇒ Fixnum
returns week of month for a given date Date.new(2012,11,15).week_of_month => 3.
-
#first_week? ⇒ Boolean
checks whether the given day lies in first week of month Date.new(2012,11,1).first_week? => true.
-
#lang ⇒ String
this code generates method like ‘week_of_month_eng’, ‘week_of_month_fr’ etc.
-
#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_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
120 121 122 |
# File 'lib/modules/week.rb', line 120 def beginning_of_week self.class.new(year,month,current_week.detect {|i| !i.nil?}) 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]
174 175 176 |
# File 'lib/modules/week.rb', line 174 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
110 111 112 |
# File 'lib/modules/week.rb', line 110 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
100 101 102 |
# File 'lib/modules/week.rb', line 100 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
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/modules/week.rb', line 130 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 |
#find_week ⇒ 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 find_week week_split.each_with_index do |o,i| return (i + 1) if o.include?(self.day) end end |
#first_week? ⇒ Boolean
checks whether the given day lies in first week of month Date.new(2012,11,1).first_week?
=> true
24 25 26 |
# File 'lib/modules/week.rb', line 24 def first_week? week_split[0].include?((self.day)) end |
#lang ⇒ 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"
87 88 89 90 91 92 |
# File 'lib/modules/week.rb', line 87 ['eng', 'fr', 'ger', 'jap','tamil','telugu'].each do |lang| method_name = "find_week_in_#{lang}" define_method(method_name) do eval "WEEK_IN_#{lang.upcase}[find_week]" end end |
#last_week? ⇒ Boolean
checks whether the given day lies in last week of month Date.new(2012,11,8).last_week?
=> false
40 41 42 |
# File 'lib/modules/week.rb', line 40 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
148 149 150 151 152 153 154 |
# File 'lib/modules/week.rb', line 148 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
162 163 164 165 166 167 168 |
# File 'lib/modules/week.rb', line 162 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
32 33 34 |
# File 'lib/modules/week.rb', line 32 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
48 49 50 |
# File 'lib/modules/week.rb', line 48 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
56 57 58 |
# File 'lib/modules/week.rb', line 56 def week_end? saturday? || sunday? 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]
77 78 79 |
# File 'lib/modules/week.rb', line 77 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
64 65 66 |
# File 'lib/modules/week.rb', line 64 def working_day? !week_end? end |