weekday 1.0.2 → 1.0.3

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.
data/History.txt CHANGED
@@ -1,3 +1,9 @@
1
+ == 1.0.3 / 2011-04-06
2
+
3
+ * 2 enhancements
4
+ * new method nth
5
+ * new method Weekday.mondays(year) .. Weekday.sundays(year)
6
+
1
7
  == 1.0.2 / 2011-02-23
2
8
 
3
9
  * 1 bugfix
data/examples/termine.rb CHANGED
@@ -53,5 +53,6 @@ puts '-'*ueberschrift.length
53
53
  puts_event tag, "rug-b Treffen (jeden 1. Do)", Weekday.first_thursday(year,month) == tag
54
54
  puts_event tag, "Ocean Club Radio Eins (jeden 2. Sa 1-5 h)", Weekday.second_saturday(year,month) == tag
55
55
  puts_event tag, "Ocean Club Radio Eins (jeden 4. Sa 1-5 h)", Weekday.fourth_saturday(year,month) == tag
56
+ puts_event tag, "freiesMagazin erscheint (jeden 1. So", Weekday.first_sunday(year,month) == tag
56
57
  tag += 1
57
58
  end
data/lib/weekday.rb CHANGED
@@ -10,6 +10,7 @@ The Class Weekday can create date objects by calling class methods like
10
10
  * Weekday.third_thursday(2011, 3) # third_thursday in march 2011
11
11
  * Weekday.last_sunday(2011, 8) # last sunday in august 2011
12
12
  * Weekday.fridays(2011, 9) # an array of date objects with all fridays in 9/2011
13
+ * Weekday.first_thursdays(2011) # an array of date objects with all first thursdays in 2011
13
14
 
14
15
  Every combination from 'first' to 'fifth' and 'last' and the a name of a weekday from
15
16
  'monday' to 'sunday' is allowed.
@@ -33,18 +34,35 @@ If a certain date like 'fifth_saturday' is not possible, nil is returned.
33
34
  # * Weekday.first_monday(2011) returns the very first monday in this year
34
35
  # * Weekday.last_monday(2011) returns the very last monday in this year
35
36
  # * Weekday.mondays(2011) returns all mondays in this year as an array of Date objects
37
+ # * Weekday.first_thursdays(2011) returns all first thursdays in this year as an array of Date objects
36
38
  def self.method_missing(m, *args)
39
+ # first_thursdays(year)
40
+ count_s,weekday = m.to_s.split('_')
41
+ if DAYNAMESS.include?(weekday) and args.size == 1
42
+ unless COUNTWORDS.include?(count_s)
43
+ raise NoMethodError, "undefined method `#{m}'"
44
+ end
45
+ count = COUNTWORDS.index(count_s)+1
46
+ wnum = DAYNAMESS.index(weekday)
47
+ year = args[0]
48
+ result = []
49
+ 1.upto(12) do |month|
50
+ result << self.send(count_s+"_"+DAYNAMES[wnum],year,month)
51
+ end
52
+ return result
53
+ end
54
+ # end first_thursdays
37
55
  # self.mondays .. self.sundays
38
56
  if DAYNAMESS.include?(m.to_s)
39
57
  wnum = DAYNAMESS.index(m.to_s)
40
58
  raise ArgumentError, ILLEGAL_ARGUMENTS unless args[0] or args[1]
41
59
  # mondays .. sundays without a month => calculate complete year
42
60
  unless args[1]
43
- result = []
44
- 1.upto(12) do |mm|
45
- result = (result + self.send(m,args[0],mm)).flatten
46
- end
47
- return result
61
+ result = []
62
+ 1.upto(12) do |mm|
63
+ result = (result + self.send(m,args[0],mm)).flatten
64
+ end
65
+ return result
48
66
  end
49
67
  begin
50
68
  d = Date.new(args[0],args[1])
@@ -77,7 +95,7 @@ If a certain date like 'fifth_saturday' is not possible, nil is returned.
77
95
  # self.last_monday .. self.last_sunday
78
96
  if count == COUNTWORDS.index(COUNTWORDS[-1])+1
79
97
  args[1] = 12 if args[1] == nil # december if no month was given at
80
- # last_xxx(year)
98
+ # last_xxx(year)
81
99
  d = Date.new(args[0],args[1],-1)
82
100
  while not d.wday == wnum do
83
101
  d -= 1
@@ -89,7 +107,7 @@ If a certain date like 'fifth_saturday' is not possible, nil is returned.
89
107
  # at least a year must be given
90
108
  raise ArgumentError, ILLEGAL_ARGUMENTS unless args[0] or args[1]
91
109
  args[1] = 1 if args[1] == nil # january if no month was given at
92
- # first_xxx(year)
110
+ # first_xxx(year)
93
111
  begin
94
112
  d = Date.new(args[0],args[1],1)
95
113
  rescue ArgumentError
@@ -115,5 +133,20 @@ If a certain date like 'fifth_saturday' is not possible, nil is returned.
115
133
  raise NoMethodError, "undefined method `#{m}'"
116
134
  end
117
135
  end
118
- end
119
136
 
137
+ # just another notation if you have to calculate the weekdays programmatically
138
+ # * count: 1..5 first, second; -1 last
139
+ # * weekday: 0: sunday, 1: monday, ... 6: saturday
140
+ # * year
141
+ # * month
142
+ def Weekday.nth(count, weekday, year, month)
143
+ raise ArgumentError unless count >= 1 and count <=5 or count == -1
144
+ if count == -1
145
+ s = COUNTWORDS.last + '_' + DAYNAMES[weekday]
146
+ else
147
+ s = COUNTWORDS[count-1] + '_' + DAYNAMES[weekday]
148
+ end
149
+ self.send(s,year,month)
150
+ end
151
+
152
+ end
data/test/test_wd.rb CHANGED
@@ -3,12 +3,12 @@ require 'test/unit'
3
3
 
4
4
  class TestWd < Test::Unit::TestCase
5
5
  def test_class
6
- assert_equal Date, Weekday.first_saturday(2010,12).class
6
+ assert_equal Date, Weekday.first_saturday(2010,12).class
7
7
  end
8
8
 
9
9
  def test_first_saturday_2010_12
10
10
  assert_equal Date.new(2010,12,4),
11
- Weekday.first_saturday(2010,12)
11
+ Weekday.first_saturday(2010,12)
12
12
  end
13
13
 
14
14
  def test_second_saturday_2010_12
@@ -32,22 +32,22 @@ class TestWd < Test::Unit::TestCase
32
32
 
33
33
  def test_first_sunday
34
34
  assert_equal Date.new(2010,12,5),
35
- Weekday.first_sunday(2010,12)
35
+ Weekday.first_sunday(2010,12)
36
36
  end
37
37
 
38
38
  def test_second_sunday
39
39
  assert_equal Date.new(2010,12,12),
40
- Weekday.second_sunday(2010,12)
40
+ Weekday.second_sunday(2010,12)
41
41
  end
42
42
 
43
43
  def test_third_sunday
44
44
  assert_equal Date.new(2010,12,19),
45
- Weekday.third_sunday(2010,12)
45
+ Weekday.third_sunday(2010,12)
46
46
  end
47
47
 
48
48
  def test_fourth_sunday
49
49
  assert_equal Date.new(2010,12,26),
50
- Weekday.fourth_sunday(2010,12)
50
+ Weekday.fourth_sunday(2010,12)
51
51
  end
52
52
 
53
53
  def test_fifth_sunday
@@ -74,8 +74,8 @@ class TestWd < Test::Unit::TestCase
74
74
 
75
75
  def test_first_saturday_2010_12_deutsch
76
76
  assert_raises(NoMethodError) {
77
- assert_equal Date.new(2010,12,4),
78
- Weekday.erster_samstag(2010,12)
77
+ assert_equal Date.new(2010,12,4),
78
+ Weekday.erster_samstag(2010,12)
79
79
  }
80
80
  end
81
81
 
@@ -111,10 +111,10 @@ class TestWd < Test::Unit::TestCase
111
111
 
112
112
  def test_saturdays
113
113
  assert_equal([Date.new(2010,12,4),
114
- Date.new(2010,12,11),
115
- Date.new(2010,12,18),
116
- Date.new(2010,12,25)],
117
- Weekday.saturdays(2010,12))
114
+ Date.new(2010,12,11),
115
+ Date.new(2010,12,18),
116
+ Date.new(2010,12,25)],
117
+ Weekday.saturdays(2010,12))
118
118
  end
119
119
 
120
120
  def test_illegal_month_should_raise_exception
@@ -134,12 +134,12 @@ class TestWd < Test::Unit::TestCase
134
134
 
135
135
  def test_first_in_a_year
136
136
  assert_equal(Weekday.first_monday(2011,1),
137
- Weekday.first_monday(2011))
137
+ Weekday.first_monday(2011))
138
138
  end
139
139
 
140
140
  def test_last_in_a_year
141
141
  assert_equal(Weekday.last_monday(2011,12),
142
- Weekday.last_monday(2011))
142
+ Weekday.last_monday(2011))
143
143
  end
144
144
 
145
145
  def test_all_mondays_in_a_year
@@ -151,5 +151,31 @@ class TestWd < Test::Unit::TestCase
151
151
  assert_equal(mondays, Weekday.mondays(2011))
152
152
  end
153
153
 
154
+ # def test_second_in_year
155
+ # assert_equal(Date.new(2011,1,8), Weekday.second_saturday(2011))
156
+ # end
157
+
158
+ def test_third_sunday_with_nth
159
+ assert_equal(Date.new(2010,12,19),
160
+ Weekday.nth(3, # third
161
+ 0, # sunday = 0, monday = 1 ... saturday = 6
162
+ 2010, # year
163
+ 12 # month
164
+ ))
165
+ end
166
+
167
+ def test_last_sunday_with_nth
168
+ assert_equal Date.new(2010,12,26),
169
+ Weekday.nth(-1,0,2010,12)
170
+ end
171
+
172
+ def test_first_thursdays
173
+ expected = []
174
+ y = 2011
175
+ 1.upto 12 do |m|
176
+ expected << Weekday.first_thursday(y,m)
177
+ end
178
+ assert_equal expected, Weekday.first_thursdays(2011)
179
+ end
154
180
  end
155
181
 
data/version.txt CHANGED
@@ -1 +1 @@
1
- 1.0.2
1
+ 1.0.3
metadata CHANGED
@@ -1,44 +1,33 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: weekday
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 0
8
- - 2
9
- version: 1.0.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.3
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Thomas Preymesser
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-02-23 00:00:00 +01:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2011-04-06 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: bones
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &19667736 !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 3
30
- - 6
31
- - 5
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
32
21
  version: 3.6.5
33
22
  type: :development
34
- version_requirements: *id001
35
- description: This gem calculates date like 'first tuesday in december 2010' or 'third friday in may 2011' or 'last Saturday in december 2010'
23
+ prerelease: false
24
+ version_requirements: *19667736
25
+ description: This gem calculates date like 'first tuesday in december 2010' or 'third
26
+ friday in may 2011' or 'last Saturday in december 2010'
36
27
  email: thopre@gmail.com
37
28
  executables: []
38
-
39
29
  extensions: []
40
-
41
- extra_rdoc_files:
30
+ extra_rdoc_files:
42
31
  - History.txt
43
32
  - lib/doc/Object.html
44
33
  - lib/doc/Weekday.html
@@ -70,7 +59,7 @@ extra_rdoc_files:
70
59
  - lib/doc/js/thickbox-compressed.js
71
60
  - lib/doc/rdoc.css
72
61
  - lib/doc/weekday_rb.html
73
- files:
62
+ files:
74
63
  - History.txt
75
64
  - README.md
76
65
  - Rakefile
@@ -109,38 +98,32 @@ files:
109
98
  - lib/weekday.rb
110
99
  - test/test_wd.rb
111
100
  - version.txt
112
- has_rdoc: true
113
101
  homepage: http://weekday.rubyforge.net/
114
102
  licenses: []
115
-
116
103
  post_install_message:
117
- rdoc_options:
104
+ rdoc_options:
118
105
  - --main
119
106
  - README.md
120
- require_paths:
107
+ require_paths:
121
108
  - lib
122
- required_ruby_version: !ruby/object:Gem::Requirement
109
+ required_ruby_version: !ruby/object:Gem::Requirement
123
110
  none: false
124
- requirements:
125
- - - ">="
126
- - !ruby/object:Gem::Version
127
- segments:
128
- - 0
129
- version: "0"
130
- required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
116
  none: false
132
- requirements:
133
- - - ">="
134
- - !ruby/object:Gem::Version
135
- segments:
136
- - 0
137
- version: "0"
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
138
121
  requirements: []
139
-
140
122
  rubyforge_project: weekday
141
- rubygems_version: 1.3.7
123
+ rubygems_version: 1.7.1
142
124
  signing_key:
143
125
  specification_version: 3
144
- summary: This gem calculates date like 'first tuesday in december 2010' or 'third friday in may 2011' or 'last Saturday in december 2010'
145
- test_files:
126
+ summary: This gem calculates date like 'first tuesday in december 2010' or 'third
127
+ friday in may 2011' or 'last Saturday in december 2010'
128
+ test_files:
146
129
  - test/test_wd.rb