week_of_month 1.2.3.4 → 1.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,6 @@ RUBY_VERSION < '1.9' ? require('modules/constant') : require_relative('constant'
4
4
 
5
5
  module WeekOfMonth
6
6
  module Week
7
-
8
7
  include WeekOfMonth::Constant
9
8
 
10
9
  # returns week of month for a given date
@@ -12,8 +11,8 @@ module WeekOfMonth
12
11
  # => 3
13
12
  # @return [Fixnum]
14
13
  def week_of_month
15
- week_split.each_with_index do |o,i|
16
- return (i + 1) if o.include?(self.day)
14
+ week_split.each_with_index do |o, i|
15
+ return (i + 1) if o.include?(day)
17
16
  end
18
17
  end
19
18
 
@@ -46,7 +45,7 @@ module WeekOfMonth
46
45
  # => true
47
46
  # @return [Boolean]
48
47
  def first_week?
49
- week_split[0].include?((self.day))
48
+ week_split[0].include?(day)
50
49
  end
51
50
 
52
51
  # checks whether the given day lies in second week of month
@@ -54,7 +53,7 @@ module WeekOfMonth
54
53
  # => true
55
54
  # @return [Boolean]
56
55
  def second_week?
57
- week_split[1].include?((self.day))
56
+ week_split[1].include?(day)
58
57
  end
59
58
 
60
59
  # checks whether the given day lies in last week of month
@@ -62,7 +61,7 @@ module WeekOfMonth
62
61
  # => false
63
62
  # @return [Boolean]
64
63
  def last_week?
65
- week_split.last.include?((self.day))
64
+ week_split.last.include?(day)
66
65
  end
67
66
 
68
67
  # returns total number of weeks in month
@@ -116,9 +115,9 @@ module WeekOfMonth
116
115
  # Date.new(2012,11,30).week_of_month_in_fr
117
116
  # => "Cinquième"
118
117
  # @return [String]
119
- constants.select{|x| x.to_s.match("WEEK_OF_MONTH_IN_")}.each do |const|
118
+ constants.select { |x| x.to_s.match('WEEK_OF_MONTH_IN_') }.each do |const|
120
119
  define_method(const.to_s.downcase) do
121
- eval "#{const.to_s}[week_of_month]"
120
+ eval "#{const}[week_of_month]"
122
121
  end
123
122
  end
124
123
 
@@ -129,7 +128,7 @@ module WeekOfMonth
129
128
  # => 5
130
129
  # @return [Fixnum]
131
130
  def days_past_in_week
132
- self.to_date.cwday
131
+ to_date.cwday
133
132
  end
134
133
 
135
134
  # it returns days left in the week
@@ -149,7 +148,7 @@ module WeekOfMonth
149
148
  # => 2012-11-29 23:59:55 +0530
150
149
  # @return [Date || Time]
151
150
  def beginning_of_week
152
- self.class.new(year,month,current_week.detect {|i| !i.nil?})
151
+ self.class.new(year, month, current_week.detect { |i| !i.nil? })
153
152
  end
154
153
 
155
154
  # it returns date of the last day(saturday) of the week
@@ -159,13 +158,13 @@ module WeekOfMonth
159
158
  # => 2012-11-30 00:00:02 +0530
160
159
  # @return [Date || Time]
161
160
  def end_of_week
162
- if current_week.index(self.day) == 6
163
- self.class.new(year,month,current_week.last)
164
- elsif current_week.index(self.day) < 6
161
+ if current_week.index(day) == 6
162
+ self.class.new(year, month, current_week.last)
163
+ elsif current_week.index(day) < 6
165
164
  if self.class == Date
166
- self + (6 - current_week.index(self.day))
165
+ self + (6 - current_week.index(day))
167
166
  elsif self.class == Time
168
- self + (60 * 60 * 24 * (6 - current_week.index(self.day)))
167
+ self + (60 * 60 * 24 * (6 - current_week.index(day)))
169
168
  end
170
169
  end
171
170
  end
@@ -203,8 +202,7 @@ module WeekOfMonth
203
202
  # => [7, 8, 9, 10, 11, 12, 13]
204
203
  # @return [Array]
205
204
  def current_week
206
- week_split.select{|c| c.include?((self.day))}.flatten
205
+ week_split.select { |c| c.include?(day) }.flatten
207
206
  end
208
-
209
207
  end
210
208
  end
@@ -2,11 +2,10 @@
2
2
 
3
3
  module WeekOfMonth
4
4
  module Year
5
-
6
5
  def self.included(klass)
7
6
  klass.extend(ClassMethods)
8
7
  end
9
-
8
+
10
9
  module ClassMethods
11
10
  # @param[Date,Date]
12
11
  # Date.years_between_dates(Date.new(2015,11,1),Date.new(2012,11,15))
@@ -15,10 +14,9 @@ module WeekOfMonth
15
14
  # Time.years_between_dates(Time.new(2015,11,1),Time.new(2012,11,15))
16
15
  # => 3
17
16
  # @return [Fixnum]
18
- def years_between_dates(date1,date2)
17
+ def years_between_dates(date1, date2)
19
18
  (date1.year - date2.year).abs
20
19
  end
21
20
  end
22
-
23
21
  end
24
- end
22
+ end
@@ -0,0 +1,58 @@
1
+ #!/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'test/unit'
5
+ RUBY_VERSION < '1.9' ? require('lib/week_of_month') : require_relative('../../../week_of_month')
6
+
7
+ class TestConstantForDate < Test::Unit::TestCase
8
+ def test_constants_present?
9
+ assert Date::WEEK_OF_MONTH_IN_ENG
10
+
11
+ assert Date::WEEK_OF_MONTH_IN_GER
12
+
13
+ assert Date::WEEK_OF_MONTH_IN_FR
14
+
15
+ assert Date::WEEK_OF_MONTH_IN_JA
16
+
17
+ assert Date::MONTH_WITH_DAY
18
+
19
+ assert Date::MONTH_WITH_SEQUENCE
20
+
21
+ assert Date::DAYS_IN_SEQUENCE
22
+ end
23
+
24
+ def test_constants_value
25
+ assert_equal({ 1 => 'First', 2 => 'Second',
26
+ 3 => 'Third', 4 => 'Fourth',
27
+ 5 => 'Fifth', 6 => 'Sixth',
28
+ 7 => 'Seventh' }, Date::WEEK_OF_MONTH_IN_ENG)
29
+
30
+ assert_equal({ 1 => 'Premier', 2 => 'Deuxième',
31
+ 3 => 'Troisième', 4 => 'Quatrième',
32
+ 5 => 'Cinquième', 6 => 'Sixième',
33
+ 7 => 'Septième' }, Date::WEEK_OF_MONTH_IN_FR)
34
+
35
+ assert_equal({ 1 => 'First', 2 => 'Second',
36
+ 3 => 'Dritten', 4 => 'Vierte',
37
+ 5 => 'Fünfte', 6 => 'Sechste',
38
+ 7 => 'siebte' }, Date::WEEK_OF_MONTH_IN_GER)
39
+
40
+ assert_equal({ 1 => '第一', 2 => '第二',
41
+ 3 => '第三', 4 => '第四',
42
+ 5 => '第五', 6 => '第六',
43
+ 7 => '第七' }, Date::WEEK_OF_MONTH_IN_JA)
44
+
45
+ assert_equal({ january: 1, february: 2, march: 3,
46
+ april: 4, may: 5, june: 6, july: 7,
47
+ august: 8, september: 9, october: 10,
48
+ november: 11, december: 12 }, Date::MONTH_WITH_SEQUENCE)
49
+
50
+ assert_equal({ january: 31, february: 28, march: 31,
51
+ april: 30, may: 31, june: 30, july: 31,
52
+ august: 31, september: 30, october: 31,
53
+ november: 30, december: 31 }, Date::MONTH_WITH_DAY)
54
+
55
+ assert_equal(%w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday],
56
+ Date::DAYS_IN_SEQUENCE)
57
+ end
58
+ end
@@ -0,0 +1,143 @@
1
+ #!/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'test/unit'
5
+
6
+ RUBY_VERSION < '1.9' ? require('lib/week_of_month') : require_relative('../../../week_of_month')
7
+ class TestDayForDate < Test::Unit::TestCase
8
+ def test_days_array
9
+ object = Date.new(2012, 2, 8)
10
+
11
+ days_array_for_february = [nil, nil, nil, 1, 2, 3, 4, 5,
12
+ 6, 7, 8, 9, 10, 11, 12, 13, 14,
13
+ 15, 16, 17, 18, 19, 20, 21, 22,
14
+ 23, 24, 25, 26, 27, 28, 29]
15
+ assert_kind_of Array, object.days_array
16
+ assert_equal days_array_for_february, object.days_array
17
+
18
+ object = Date.new(2012, 7, 1)
19
+ days_array_for_july = [1, 2, 3, 4, 5, 6, 7,
20
+ 8, 9, 10, 11, 12, 13,
21
+ 14, 15, 16, 17, 18, 19,
22
+ 20, 21, 22, 23, 24, 25,
23
+ 26, 27, 28, 29, 30, 31]
24
+ assert_kind_of Array, object.days_array
25
+ assert_equal days_array_for_july, object.days_array
26
+ end
27
+
28
+ def test_days_array_monday
29
+ WeekOfMonth.configuration.monday_active = true
30
+ object = Date.new(2014, 11, 3)
31
+
32
+ days_array_for_november = [nil, nil, nil, nil, nil, 1, 2,
33
+ 3, 4, 5, 6, 7, 8, 9, 10, 11,
34
+ 12, 13, 14, 15, 16, 17, 18,
35
+ 19, 20, 21, 22, 23, 24, 25, 26,
36
+ 27, 28, 29, 30]
37
+ assert_kind_of Array, object.days_array
38
+ assert_equal days_array_for_november, object.days_array
39
+
40
+ object = Date.new(2014, 12, 1)
41
+ days_array_for_december = [1, 2, 3, 4, 5, 6, 7,
42
+ 8, 9, 10, 11, 12, 13,
43
+ 14, 15, 16, 17, 18, 19,
44
+ 20, 21, 22, 23, 24, 25,
45
+ 26, 27, 28, 29, 30, 31]
46
+ assert_kind_of Array, object.days_array
47
+ assert_equal days_array_for_december, object.days_array
48
+ end
49
+
50
+ def test_name_of_week_day
51
+ WeekOfMonth.configuration.monday_active = false
52
+ assert_equal 'Saturday', Date.new(2012, 12, 1).name_of_week_day
53
+ assert_equal 'Sunday', Date.new(2012, 12, 2).name_of_week_day
54
+ assert_equal 'Monday', Date.new(2012, 12, 3).name_of_week_day
55
+ assert_equal 'Tuesday', Date.new(2012, 12, 4).name_of_week_day
56
+ assert_equal 'Wednesday', Date.new(2012, 12, 5).name_of_week_day
57
+ assert_equal 'Thursday', Date.new(2012, 12, 6).name_of_week_day
58
+ assert_equal 'Friday', Date.new(2012, 12, 7).name_of_week_day
59
+ end
60
+
61
+ def test_upcoming_sunday
62
+ assert_equal Date.new(2013, 1, 6), Date.new(2013, 1, 1).upcoming_sunday
63
+ assert_equal Date.new(2013, 1, 6), Date.new(2013, 1, 5).upcoming_sunday
64
+ assert_equal Date.new(2013, 1, 13), Date.new(2013, 1, 7).upcoming_sunday
65
+ assert_equal Date.new(2013, 1, 6), Date.new(2012, 12, 30).upcoming_sunday
66
+ end
67
+
68
+ def test_upcoming_monday
69
+ assert_equal Date.new(2013, 1, 7), Date.new(2013, 1, 1).upcoming_monday
70
+ end
71
+
72
+ def test_upcoming_tuesday
73
+ assert_equal Date.new(2013, 1, 8), Date.new(2013, 1, 1).upcoming_tuesday
74
+ end
75
+
76
+ def test_upcoming_wednesday
77
+ assert_equal Date.new(2013, 1, 2), Date.new(2013, 1, 1).upcoming_wednesday
78
+ end
79
+
80
+ def test_upcoming_thursday
81
+ assert_equal Date.new(2013, 1, 3), Date.new(2013, 1, 1).upcoming_thursday
82
+ end
83
+
84
+ def test_upcoming_friday
85
+ assert_equal Date.new(2013, 1, 4), Date.new(2013, 1, 1).upcoming_friday
86
+ end
87
+
88
+ def test_upcoming_saturday
89
+ assert_equal Date.new(2013, 1, 5), Date.new(2013, 1, 1).upcoming_saturday
90
+ end
91
+
92
+ def test_previous_saturday
93
+ assert_equal Date.new(2012, 12, 29), Date.new(2013, 1, 1).previous_saturday
94
+ end
95
+
96
+ def test_previous_friday
97
+ assert_equal Date.new(2012, 12, 28), Date.new(2013, 1, 1).previous_friday
98
+ end
99
+
100
+ def test_previous_thursday
101
+ assert_equal Date.new(2012, 12, 27), Date.new(2013, 1, 1).previous_thursday
102
+ end
103
+
104
+ def test_previous_wednesday
105
+ assert_equal Date.new(2012, 12, 26), Date.new(2013, 1, 1).previous_wednesday
106
+ end
107
+
108
+ def test_previous_tuesday
109
+ assert_equal Date.new(2012, 12, 25), Date.new(2013, 1, 1).previous_tuesday
110
+ end
111
+
112
+ def test_previous_monday
113
+ assert_equal Date.new(2012, 12, 31), Date.new(2013, 1, 1).previous_monday
114
+ end
115
+
116
+ def test_previous_sunday
117
+ assert_equal Date.new(2012, 12, 30), Date.new(2013, 1, 1).previous_sunday
118
+ assert_equal Date.new(2012, 12, 30), Date.new(2013, 1, 1).previous_sunday
119
+ end
120
+
121
+ def test_all_working_days_of_month
122
+ assert_equal [
123
+ Date.new(2013, 4, 30), Date.new(2013, 4, 29),
124
+ Date.new(2013, 4, 26), Date.new(2013, 4, 25), Date.new(2013, 4, 24), Date.new(2013, 4, 23), Date.new(2013, 4, 22),
125
+ Date.new(2013, 4, 19), Date.new(2013, 4, 18), Date.new(2013, 4, 17), Date.new(2013, 4, 16), Date.new(2013, 4, 15),
126
+ Date.new(2013, 4, 12), Date.new(2013, 4, 11), Date.new(2013, 4, 10), Date.new(2013, 4, 9), Date.new(2013, 4, 8),
127
+ Date.new(2013, 4, 5), Date.new(2013, 4, 4), Date.new(2013, 4, 3), Date.new(2013, 4, 2), Date.new(2013, 4, 1)
128
+ ], Date.new(2013, 4, 1).all_working_days_of_month
129
+ end
130
+
131
+ def test_all_non_week_days_of_month
132
+ assert_equal [
133
+ Date.new(2013, 4, 28), Date.new(2013, 4, 27),
134
+ Date.new(2013, 4, 21), Date.new(2013, 4, 20),
135
+ Date.new(2013, 4, 14), Date.new(2013, 4, 13),
136
+ Date.new(2013, 4, 7), Date.new(2013, 4, 6)
137
+ ], Date.new(2013, 4, 1).all_non_week_days_of_month
138
+ end
139
+
140
+ def test_first_working_day_of_the_month
141
+ assert_equal Date.new(2013, 1, 1), Date.new(2013, 1, 1).first_working_day_of_the_month
142
+ end
143
+ end
@@ -0,0 +1,166 @@
1
+ #!/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'test/unit'
5
+
6
+ if RUBY_VERSION < '1.9'
7
+ require('lib/week_of_month')
8
+ else
9
+ require_relative('../../../week_of_month')
10
+ end
11
+
12
+ class TestMonthForDate < Test::Unit::TestCase
13
+ def test_month?
14
+ assert Date.new(2012, 1, 1).january?
15
+
16
+ assert Date.new(2012, 2, 1).february?
17
+
18
+ assert Date.new(2012, 3, 1).march?
19
+
20
+ assert Date.new(2012, 4, 1).april?
21
+
22
+ assert Date.new(2012, 5, 1).may?
23
+
24
+ assert Date.new(2012, 6, 1).june?
25
+
26
+ assert Date.new(2012, 7, 1).july?
27
+
28
+ assert Date.new(2012, 8, 1).august?
29
+
30
+ assert Date.new(2012, 9, 1).september?
31
+
32
+ assert Date.new(2012, 10, 1).october?
33
+
34
+ assert Date.new(2012, 11, 1).november?
35
+
36
+ assert Date.new(2012, 12, 1).december?
37
+ end
38
+
39
+ def test_last_day_of_month
40
+ assert_equal 31, Date.new(2012, 1, 31).last_day_of_month
41
+
42
+ assert_equal 29, Date.new(2012, 2, 29).last_day_of_month
43
+
44
+ assert_equal 31, Date.new(2012, 3, 31).last_day_of_month
45
+
46
+ assert_equal 30, Date.new(2012, 4, 30).last_day_of_month
47
+
48
+ assert_equal 31, Date.new(2012, 5, 31).last_day_of_month
49
+
50
+ assert_equal 30, Date.new(2012, 6, 30).last_day_of_month
51
+
52
+ assert_equal 31, Date.new(2012, 7, 31).last_day_of_month
53
+
54
+ assert_equal 31, Date.new(2012, 8, 31).last_day_of_month
55
+
56
+ assert_equal 30, Date.new(2012, 9, 30).last_day_of_month
57
+
58
+ assert_equal 31, Date.new(2012, 10, 31).last_day_of_month
59
+
60
+ assert_equal 30, Date.new(2012, 11, 30).last_day_of_month
61
+
62
+ assert_equal 31, Date.new(2012, 12, 31).last_day_of_month
63
+ end
64
+
65
+ def test_end_of_month
66
+ assert_equal Date.new(2012, 1, 31), Date.new(2012, 1, 1).end_of_month
67
+
68
+ assert_equal Date.new(2012, 2, 29), Date.new(2012, 2, 2).end_of_month
69
+
70
+ assert_equal Date.new(2012, 3, 31), Date.new(2012, 3, 1).end_of_month
71
+
72
+ assert_equal Date.new(2012, 4, 30), Date.new(2012, 4, 3).end_of_month
73
+
74
+ assert_equal Date.new(2012, 5, 31), Date.new(2012, 5, 1).end_of_month
75
+
76
+ assert_equal Date.new(2012, 6, 30), Date.new(2012, 6, 30).end_of_month
77
+
78
+ assert_equal Date.new(2012, 7, 31), Date.new(2012, 7, 1).end_of_month
79
+
80
+ assert_equal Date.new(2012, 8, 31), Date.new(2012, 8, 5).end_of_month
81
+
82
+ assert_equal Date.new(2012, 9, 30), Date.new(2012, 9, 2).end_of_month
83
+
84
+ assert_equal Date.new(2012, 10, 31), Date.new(2012, 10, 22).end_of_month
85
+
86
+ assert_equal Date.new(2012, 11, 30), Date.new(2012, 11, 10).end_of_month
87
+
88
+ assert_equal Date.new(2012, 12, 31), Date.new(2012, 12, 15).end_of_month
89
+ end
90
+
91
+ def test_beginning_of_month
92
+ assert_equal Date.new(2012, 1, 1), Date.new(2012, 1, 31).beginning_of_month
93
+
94
+ assert_equal Date.new(2012, 2, 1), Date.new(2012, 2, 29).beginning_of_month
95
+
96
+ assert_equal Date.new(2012, 3, 1), Date.new(2012, 3, 31).beginning_of_month
97
+
98
+ assert_equal Date.new(2012, 4, 1), Date.new(2012, 4, 30).beginning_of_month
99
+
100
+ assert_equal Date.new(2012, 5, 1), Date.new(2012, 5, 31).beginning_of_month
101
+
102
+ assert_equal Date.new(2012, 6, 1), Date.new(2012, 6, 30).beginning_of_month
103
+
104
+ assert_equal Date.new(2012, 7, 1), Date.new(2012, 7, 31).beginning_of_month
105
+
106
+ assert_equal Date.new(2012, 8, 1), Date.new(2012, 8, 31).beginning_of_month
107
+
108
+ assert_equal Date.new(2012, 9, 1), Date.new(2012, 9, 30).beginning_of_month
109
+
110
+ assert_equal Date.new(2012, 10, 1), Date.new(2012, 10, 31).beginning_of_month
111
+
112
+ assert_equal Date.new(2012, 11, 1), Date.new(2012, 11, 30).beginning_of_month
113
+
114
+ assert_equal Date.new(2012, 12, 1), Date.new(2012, 12, 31).beginning_of_month
115
+ end
116
+
117
+ def test_all_sundays_in_month
118
+ assert_equal [6, 13, 20, 27], Date.new(2013, 1, 1).all_sundays_in_month
119
+ assert_equal [3, 10, 17, 24], Date.new(2013, 2, 1).all_sundays_in_month
120
+ end
121
+
122
+ def test_all_mondays_in_month
123
+ assert_equal [7, 14, 21, 28], Date.new(2013, 1, 1).all_mondays_in_month
124
+ assert_equal [4, 11, 18, 25], Date.new(2013, 2, 1).all_mondays_in_month
125
+ end
126
+
127
+ def test_all_tuesdays_in_month
128
+ assert_equal [1, 8, 15, 22, 29], Date.new(2013, 1, 1).all_tuesdays_in_month
129
+ assert_equal [5, 12, 19, 26], Date.new(2013, 2, 1).all_tuesdays_in_month
130
+ end
131
+
132
+ def test_all_wednesdays_in_month
133
+ assert_equal [2, 9, 16, 23, 30], Date.new(2013, 1, 1).all_wednesdays_in_month
134
+ assert_equal [6, 13, 20, 27], Date.new(2013, 2, 1).all_wednesdays_in_month
135
+ end
136
+
137
+ def test_all_thursdays_in_month
138
+ assert_equal [3, 10, 17, 24, 31], Date.new(2013, 1, 1).all_thursdays_in_month
139
+ assert_equal [7, 14, 21, 28], Date.new(2013, 2, 1).all_thursdays_in_month
140
+ end
141
+
142
+ def test_all_fridays_in_month
143
+ assert_equal [4, 11, 18, 25], Date.new(2013, 1, 1).all_fridays_in_month
144
+ assert_equal [1, 8, 15, 22], Date.new(2013, 2, 1).all_fridays_in_month
145
+ end
146
+
147
+ def test_all_saturdays_in_month
148
+ assert_equal [5, 12, 19, 26], Date.new(2013, 1, 1).all_saturdays_in_month
149
+ assert_equal [2, 9, 16, 23], Date.new(2013, 2, 1).all_saturdays_in_month
150
+ end
151
+
152
+ def test_name_of_month
153
+ assert_equal 'January', Date.new(2012, 1, 1).name_of_month
154
+ assert_equal 'February', Date.new(2012, 2, 1).name_of_month
155
+ assert_equal 'March', Date.new(2012, 3, 1).name_of_month
156
+ assert_equal 'April', Date.new(2012, 4, 1).name_of_month
157
+ assert_equal 'May', Date.new(2012, 5, 1).name_of_month
158
+ assert_equal 'June', Date.new(2012, 6, 1).name_of_month
159
+ assert_equal 'July', Date.new(2012, 7, 1).name_of_month
160
+ assert_equal 'August', Date.new(2012, 8, 1).name_of_month
161
+ assert_equal 'September', Date.new(2012, 9, 1).name_of_month
162
+ assert_equal 'October', Date.new(2012, 10, 1).name_of_month
163
+ assert_equal 'November', Date.new(2012, 11, 1).name_of_month
164
+ assert_equal 'December', Date.new(2012, 12, 1).name_of_month
165
+ end
166
+ end