week_of_month 1.2.1 → 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +24 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +8 -0
- data/README.rdoc +111 -0
- data/Rakefile +10 -0
- data/lib/modules/constant.rb +41 -0
- data/lib/modules/day.rb +64 -0
- data/lib/modules/month.rb +68 -0
- data/lib/modules/week.rb +179 -0
- data/lib/modules/year.rb +24 -0
- data/lib/test/modules/test_constant.rb +63 -0
- data/lib/test/modules/test_day.rb +129 -0
- data/lib/test/modules/test_month.rb +187 -0
- data/lib/test/modules/test_week.rb +196 -0
- data/lib/test/modules/test_year.rb +17 -0
- data/lib/week_of_month.rb +12 -3
- data/week_of_month.gemspec +13 -0
- metadata +20 -7
- data/lib/test/test_week_of_month_for_date_object.rb +0 -350
- data/lib/test/test_week_of_month_for_time_object.rb +0 -350
- data/lib/week_helper.rb +0 -120
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
= Week Of Month
|
2
|
+
|
3
|
+
Week Of Month is a library which gives you week_of_month method on Date and Time
|
4
|
+
class object, that returns week of the month. It basically extends the Date and Time
|
5
|
+
class with several useful date helpers.
|
6
|
+
|
7
|
+
== Getting Started
|
8
|
+
|
9
|
+
Week Of Month is released as a Ruby Gem. The gem is to be installed within a Ruby
|
10
|
+
or Rails application. To install, simply add the following to your Gemfile:
|
11
|
+
|
12
|
+
gem 'week_of_month'
|
13
|
+
|
14
|
+
== Usage
|
15
|
+
|
16
|
+
It returns week split of that date's month
|
17
|
+
|
18
|
+
Date.new(2012,1,1).week_split
|
19
|
+
|
20
|
+
=> [[1, 2, 3, 4, 5, 6, 7],
|
21
|
+
[8, 9, 10, 11, 12, 13, 14],
|
22
|
+
[15, 16, 17, 18, 19, 20, 21],
|
23
|
+
[22, 23, 24, 25, 26, 27, 28],
|
24
|
+
[29, 30, 31]]
|
25
|
+
|
26
|
+
It returns month's week in which the date lies
|
27
|
+
|
28
|
+
Date.new(2012,1,1).week_of_month
|
29
|
+
=> 1
|
30
|
+
|
31
|
+
It return true if date lies in first week of month, else false.
|
32
|
+
|
33
|
+
Date.new(2012,1,1).first_week?
|
34
|
+
=> true
|
35
|
+
|
36
|
+
It returns true if date lies in second week of month, else false.
|
37
|
+
|
38
|
+
Date.new(2012,1,9).second_week?
|
39
|
+
=> true
|
40
|
+
|
41
|
+
It returns true if date lies in last week of month, else false.
|
42
|
+
|
43
|
+
Date.new(2012,1,31).last_week?
|
44
|
+
=> true
|
45
|
+
|
46
|
+
It returns total number of weeks in month.
|
47
|
+
|
48
|
+
Date.new(2012,1,31).total_weeks
|
49
|
+
=> 5
|
50
|
+
|
51
|
+
It returns month's week in which the date lies
|
52
|
+
|
53
|
+
Date.new(2012,1,31).week_of_month_in_eng
|
54
|
+
=> "Fifth"
|
55
|
+
|
56
|
+
== Tools Being Used
|
57
|
+
|
58
|
+
We believe strongly in not writing code unless we have to, so Week Of Month is built using
|
59
|
+
|
60
|
+
Ruby Date Class
|
61
|
+
|
62
|
+
Ruby Time Class
|
63
|
+
|
64
|
+
== Version History
|
65
|
+
|
66
|
+
1.2.1
|
67
|
+
|
68
|
+
Support for Time class
|
69
|
+
|
70
|
+
Methods Added
|
71
|
+
|
72
|
+
name_of_week_day, name_of_month, week_end?, working_day?,
|
73
|
+
all_sundays_in_month, all_mondays_in_month, all_tuesdays_in_month,
|
74
|
+
all_wednesdays_in_month, all_thursdays_in_month, all_fridays_in_month,
|
75
|
+
all_saturdays_in_month
|
76
|
+
|
77
|
+
1.1.0
|
78
|
+
|
79
|
+
ActiveSupport Dependency removed
|
80
|
+
|
81
|
+
Methods Added
|
82
|
+
|
83
|
+
january?, february?, march?, april?, may?, june?, july?,
|
84
|
+
august?, september?, october?, november?, december?, last_day_of_month
|
85
|
+
|
86
|
+
== Contributing to Week Of Month
|
87
|
+
|
88
|
+
Fork, fix, then send me a pull request.
|
89
|
+
|
90
|
+
== Copyright
|
91
|
+
|
92
|
+
(The MIT License)
|
93
|
+
|
94
|
+
Copyright (c) 2012 Sachin Singh. See LICENSE for details.
|
95
|
+
|
96
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
97
|
+
of this software and associated documentation files (the ‘Software’), to deal
|
98
|
+
in the Software without restriction, including without limitation the rights
|
99
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
100
|
+
of the Software, and to permit persons to whom the Software is furnished to do so,
|
101
|
+
subject to the following conditions:
|
102
|
+
|
103
|
+
The above copyright notice and this permission notice shall be included in all
|
104
|
+
copies or substantial portions of the Software.
|
105
|
+
|
106
|
+
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
107
|
+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
108
|
+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
109
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
110
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
111
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
#!/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
# @author Sachin Singh
|
5
|
+
|
6
|
+
module WeekOfMonth
|
7
|
+
module Constant
|
8
|
+
|
9
|
+
# hash containg english words from one to seventh
|
10
|
+
WEEK_IN_ENG = { 1 => 'First', 2 => 'Second', 3 => 'Third',
|
11
|
+
4 => 'Fourth', 5 => 'Fifth', 6 => 'Sixth', 7 => 'Seventh'}
|
12
|
+
|
13
|
+
# hash containg french words from one to seventh
|
14
|
+
WEEK_IN_FR = { 1 => 'First', 2 => 'Second', 3 => 'Third',
|
15
|
+
4 => 'Quatrième', 5 => 'Cinquième', 6 => 'sixième', 7 => 'septième'}
|
16
|
+
|
17
|
+
# hash containg german words from one to seventh
|
18
|
+
WEEK_IN_GER = { 1 => 'First', 2 => 'Second', 3 => 'Dritten',
|
19
|
+
4 => 'Vierte', 5 => 'Fünfte', 6 => 'Sechste', 7 => 'siebte'}
|
20
|
+
|
21
|
+
# hash containg japneese words from one to seventh
|
22
|
+
WEEK_IN_JAP = { 1 => '最初', 2 => '秒', 3 => 'サード',
|
23
|
+
4 => '第4回', 5 => '第五', 6=> 'シックス', 7 => '第7' }
|
24
|
+
|
25
|
+
# hash containg month name with days in that month(in non leap yaer)
|
26
|
+
MONTH_WITH_DAY = { :january => 31, :february => 28, :march => 31,
|
27
|
+
:april => 30, :may => 31, :june => 30, :july => 31,
|
28
|
+
:august => 31, :september => 30, :october => 31,
|
29
|
+
:november => 30, :december => 31 }
|
30
|
+
|
31
|
+
# hash containg month names with their sequence
|
32
|
+
MONTH_WITH_SEQUENCE = { :january => 1, :february => 2, :march => 3,
|
33
|
+
:april => 4, :may => 5, :june => 6, :july => 7,
|
34
|
+
:august => 8, :september => 9, :october => 10,
|
35
|
+
:november => 11, :december => 12 }
|
36
|
+
|
37
|
+
# array of days names ordered starting with sunday and ending with saturday.
|
38
|
+
DAYS_IN_SEQUENCE = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
|
39
|
+
"Saturday"]
|
40
|
+
end
|
41
|
+
end
|
data/lib/modules/day.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
#!/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
# @author Sachin Singh
|
5
|
+
|
6
|
+
module WeekOfMonth
|
7
|
+
module Day
|
8
|
+
|
9
|
+
# gives array of days in month
|
10
|
+
# Date.new(2012,1,1).days_array
|
11
|
+
# => [ 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
12
|
+
# 10, 11, 12, 13, 14, 15, 16,
|
13
|
+
# 17, 18, 19, 20, 21, 22, 23,
|
14
|
+
# 24, 25, 26, 27, 28, 29, 30,
|
15
|
+
# 31]
|
16
|
+
# @return [Array]
|
17
|
+
def days_array
|
18
|
+
day = self.beginning_of_month.to_date.wday
|
19
|
+
array = []
|
20
|
+
array[day] = 1
|
21
|
+
(2..self.end_of_month.mday).each {|i| array << i }
|
22
|
+
array
|
23
|
+
end
|
24
|
+
|
25
|
+
# Date.new(2012,11,1).name_of_week_day
|
26
|
+
# => 'Thursday'
|
27
|
+
# @return [String]
|
28
|
+
def name_of_week_day
|
29
|
+
self.class.new(year,month,day).strftime('%A')
|
30
|
+
end
|
31
|
+
|
32
|
+
# this code generates method names like 'upcomong_monday' and 'previous_monday'
|
33
|
+
# Date.new(2013,1,1).upcoming_monday
|
34
|
+
# => #<Date: 2013-01-07 ((2456300j,0s,0n),+0s,2299161j)>
|
35
|
+
# Date.new(2013,1,1).previous_monday
|
36
|
+
# => #<Date: 2012-12-31 ((2456293j,0s,0n),+0s,2299161j)>
|
37
|
+
{ 'upcoming' => '+', 'previous' => '-' }.each_pair do |key,value|
|
38
|
+
Date::DAYNAMES.each do |day_name|
|
39
|
+
name = "#{key}_#{day_name.downcase}".to_sym
|
40
|
+
check = "#{day_name.downcase}?".to_sym
|
41
|
+
define_method(name) do
|
42
|
+
date = eval "self"
|
43
|
+
if date.send(check)
|
44
|
+
if date.class == Date
|
45
|
+
date = date.send(value,7)
|
46
|
+
elsif date.class == Time
|
47
|
+
date = date.send(value,(60 * 60 * 24 * 7))
|
48
|
+
end
|
49
|
+
else
|
50
|
+
until date.send(check)
|
51
|
+
if date.class == Date
|
52
|
+
date = date.send(value,1)
|
53
|
+
elsif date.class == Time
|
54
|
+
date = date.send(value,(60 * 60 * 24))
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
date
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# @author Sachion Singh
|
2
|
+
|
3
|
+
require_relative 'constant'
|
4
|
+
|
5
|
+
module WeekOfMonth
|
6
|
+
module Month
|
7
|
+
|
8
|
+
include WeekOfMonth::Constant
|
9
|
+
|
10
|
+
# this code generates method named like january?..december?
|
11
|
+
# to check whether a month is january or march? etc.
|
12
|
+
# @return [Boolean]
|
13
|
+
MONTH_WITH_DAY.keys.each do |month_name|
|
14
|
+
define_method((month_name.to_s + '?').to_sym) do
|
15
|
+
MONTH_WITH_SEQUENCE[month_name] == month
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# returns day of last day of month for a given date.
|
20
|
+
# Date.new(2012,11,1).last_day_of_month
|
21
|
+
# => 30
|
22
|
+
# @return [Fixnum]
|
23
|
+
def last_day_of_month
|
24
|
+
if leap? && february?
|
25
|
+
29
|
26
|
+
else
|
27
|
+
MONTH_WITH_DAY[MONTH_WITH_SEQUENCE.key(month)]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# returns date of last day of month for a given date.
|
32
|
+
# Date.new(2012,11,1).end_of_month
|
33
|
+
# => #<Date: 2012-11-30 ((2456262j,0s,0n),+0s,2299161j)>
|
34
|
+
# @return [Date]
|
35
|
+
def end_of_month
|
36
|
+
self.class.new(year,month,last_day_of_month)
|
37
|
+
end
|
38
|
+
|
39
|
+
# returns date of first day of month for a given date.
|
40
|
+
# Date.new(2012,11,1).beginning_of_month
|
41
|
+
# => #<Date: 2012-11-01 ((2456233j,0s,0n),+0s,2299161j)>
|
42
|
+
# @return [Date]
|
43
|
+
def beginning_of_month
|
44
|
+
self.class.new(year,month,1)
|
45
|
+
end
|
46
|
+
|
47
|
+
# returns name of month for a given date.
|
48
|
+
# Date.new(2012,11,1).name_of_month
|
49
|
+
# => "November"
|
50
|
+
# @return [String]
|
51
|
+
def name_of_month
|
52
|
+
self.class.new(year,month,day).strftime('%B')
|
53
|
+
end
|
54
|
+
|
55
|
+
# this code generates method named like 'all_mondays_in_month',
|
56
|
+
# 'all_tuesdays_in_month' etc. for all seven days of week
|
57
|
+
# Date.new(2012,11,1).all_mondays_in_month
|
58
|
+
# => [5, 12, 19, 26]
|
59
|
+
# @return [Array]
|
60
|
+
DAYS_IN_SEQUENCE.each_with_index do |day_name, i|
|
61
|
+
method_name = "all_#{day_name.downcase}s_in_month".to_sym
|
62
|
+
define_method(method_name) do
|
63
|
+
week_split.map{|d| d[i] }.compact
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
data/lib/modules/week.rb
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
# @author Sachion Singh
|
2
|
+
|
3
|
+
require_relative 'constant'
|
4
|
+
|
5
|
+
module WeekOfMonth
|
6
|
+
module Week
|
7
|
+
|
8
|
+
include WeekOfMonth::Constant
|
9
|
+
|
10
|
+
# returns week of month for a given date
|
11
|
+
# Date.new(2012,11,15).week_of_month
|
12
|
+
# => 3
|
13
|
+
# @return [Fixnum]
|
14
|
+
def week_of_month
|
15
|
+
week_split.each_with_index do |o,i|
|
16
|
+
return (i + 1) if o.include?(self.day)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# checks whether the given day lies in first week of month
|
21
|
+
# Date.new(2012,11,1).first_week?
|
22
|
+
# => true
|
23
|
+
# @return [Boolean]
|
24
|
+
def first_week?
|
25
|
+
week_split[0].include?((self.day))
|
26
|
+
end
|
27
|
+
|
28
|
+
# checks whether the given day lies in second week of month
|
29
|
+
# Date.new(2012,11,8).second_week?
|
30
|
+
# => true
|
31
|
+
# @return [Boolean]
|
32
|
+
def second_week?
|
33
|
+
week_split[1].include?((self.day))
|
34
|
+
end
|
35
|
+
|
36
|
+
# checks whether the given day lies in last week of month
|
37
|
+
# Date.new(2012,11,8).last_week?
|
38
|
+
# => false
|
39
|
+
# @return [Boolean]
|
40
|
+
def last_week?
|
41
|
+
week_split.last.include?((self.day))
|
42
|
+
end
|
43
|
+
|
44
|
+
# returns total number of weeks in month
|
45
|
+
# Date.new(2012,11,8).total_weeks
|
46
|
+
# => 5
|
47
|
+
# @return [Fixnum]
|
48
|
+
def total_weeks
|
49
|
+
week_split.size
|
50
|
+
end
|
51
|
+
|
52
|
+
# checks whether the given day is saturday or sunday.
|
53
|
+
# Date.new(2012,11,8).week_end?
|
54
|
+
# => false
|
55
|
+
# @return [Boolean]
|
56
|
+
def week_end?
|
57
|
+
saturday? || sunday?
|
58
|
+
end
|
59
|
+
|
60
|
+
# checks whether the given day is not saturday or sunday.
|
61
|
+
# Date.new(2012,11,8).working_day?
|
62
|
+
# => true
|
63
|
+
# @return [Boolean]
|
64
|
+
def working_day?
|
65
|
+
!week_end?
|
66
|
+
end
|
67
|
+
|
68
|
+
# returns week split of the month for the given date
|
69
|
+
# example-
|
70
|
+
# Date.new(2012,1,1).week_split
|
71
|
+
# => [[1, 2, 3, 4, 5, 6, 7],
|
72
|
+
# [8, 9, 10, 11, 12, 13, 14],
|
73
|
+
# [15, 16, 17, 18, 19, 20, 21],
|
74
|
+
# [22, 23, 24, 25, 26, 27, 28],
|
75
|
+
# [29, 30, 31]
|
76
|
+
# @return [Array]
|
77
|
+
def week_split
|
78
|
+
days_array.each_slice(7).to_a
|
79
|
+
end
|
80
|
+
|
81
|
+
# this code generates method like 'week_of_month_eng', 'week_of_month_fr' etc.
|
82
|
+
# Date.new(2012,11,15).week_of_month_in_eng
|
83
|
+
# => 'Third'
|
84
|
+
# Date.new(2012,11,30).week_of_month_in_fr
|
85
|
+
# => "Cinquième"
|
86
|
+
# @return [String]
|
87
|
+
['eng', 'fr', 'ger', 'jap'].each do |lang|
|
88
|
+
method_name = "week_of_month_in_#{lang}"
|
89
|
+
define_method(method_name) do
|
90
|
+
eval "WEEK_IN_#{lang.upcase}[week_of_month]"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# it returns days past in the week
|
95
|
+
# Date.new(2012,11,15).days_past_in_week
|
96
|
+
# => 4
|
97
|
+
# Time.new(2012,11,30).days_past_in_week
|
98
|
+
# => 5
|
99
|
+
# @return [Fixnum]
|
100
|
+
def days_past_in_week
|
101
|
+
self.to_date.cwday
|
102
|
+
end
|
103
|
+
|
104
|
+
# it returns days left in the week
|
105
|
+
# Date.new(2012,11,15).days_left_in_week
|
106
|
+
# => 3
|
107
|
+
# Time.new(2012,11,30).days_left_in_week
|
108
|
+
# => 2
|
109
|
+
# @return [Fixnum]
|
110
|
+
def days_left_in_week
|
111
|
+
7 - days_past_in_week
|
112
|
+
end
|
113
|
+
|
114
|
+
# it returns date of the first day(sunday) of the week
|
115
|
+
# Date.new(2012,11,15).beginning_of_week
|
116
|
+
# => #<Date: 2012-11-12 ((2456244j,0s,0n),+0s,2299161j)>
|
117
|
+
# Time.new(2012,11,30).beginning_of_week
|
118
|
+
# => 2012-11-29 23:59:55 +0530
|
119
|
+
# @return [Date || Time]
|
120
|
+
def beginning_of_week
|
121
|
+
self.class.new(year,month,current_week.detect {|i| !i.nil?})
|
122
|
+
end
|
123
|
+
|
124
|
+
# it returns date of the last day(saturday) of the week
|
125
|
+
# Date.new(2012,11,15).end_of_week
|
126
|
+
# => #<Date: 2012-11-19 ((2456251j,0s,0n),+0s,2299161j)>
|
127
|
+
# Time.new(2012,11,30).end_of_week
|
128
|
+
# => 2012-11-30 00:00:02 +0530
|
129
|
+
# @return [Date || Time]
|
130
|
+
def end_of_week
|
131
|
+
if current_week.index(self.day) == 6
|
132
|
+
self.class.new(year,month,current_week.last)
|
133
|
+
elsif current_week.index(self.day) < 6
|
134
|
+
if self.class == Date
|
135
|
+
self + (6 - current_week.index(self.day))
|
136
|
+
elsif self.class == Time
|
137
|
+
self + (60 * 60 * 24 * (6 - current_week.index(self.day)))
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
# it returns date of the next week day.
|
143
|
+
# Date.new(2012,11,15).next_week
|
144
|
+
# => #<Date: 2012-11-22 ((2456254j,0s,0n),+0s,2299161j)>
|
145
|
+
# Time.new(2012,11,30).next_week
|
146
|
+
# => 2012-11-30 00:00:07 +0530
|
147
|
+
# @return [Date || Time]
|
148
|
+
def next_week
|
149
|
+
if self.class == Date
|
150
|
+
self + 7
|
151
|
+
elsif self.class == Time
|
152
|
+
self + (60 * 60 * 24 * 7)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
# it returns date of the previous week day.
|
157
|
+
# Date.new(2012,11,15).previous_week
|
158
|
+
# => #<Date: 2012-11-08 ((2456240j,0s,0n),+0s,2299161j)>
|
159
|
+
# Time.new(2012,11,30).previous_week
|
160
|
+
# => 2012-11-29 23:59:53 +0530
|
161
|
+
# @return [Date || Time]
|
162
|
+
def previous_week
|
163
|
+
if self.class == Date
|
164
|
+
self - 7
|
165
|
+
elsif self.class == Time
|
166
|
+
self - (60 * 60 * 24 * 7)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
# it returns array of days in current week.
|
171
|
+
# Date.new(2013,1,13).current_week
|
172
|
+
# => [7, 8, 9, 10, 11, 12, 13]
|
173
|
+
# @return [Array]
|
174
|
+
def current_week
|
175
|
+
week_split.select{|c| c.include?((self.day))}.flatten
|
176
|
+
end
|
177
|
+
|
178
|
+
end
|
179
|
+
end
|