weekday 1.0.0

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.
@@ -0,0 +1,54 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4
+
5
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
+ <head>
7
+ <meta content="text/html; charset=CP850" http-equiv="Content-Type" />
8
+
9
+ <title>File: weekday.rb [RDoc Documentation]</title>
10
+
11
+ <link type="text/css" media="screen" href="./rdoc.css" rel="stylesheet" />
12
+
13
+ <script src="./js/jquery.js" type="text/javascript"
14
+ charset="utf-8"></script>
15
+ <script src="./js/thickbox-compressed.js" type="text/javascript"
16
+ charset="utf-8"></script>
17
+ <script src="./js/quicksearch.js" type="text/javascript"
18
+ charset="utf-8"></script>
19
+ <script src="./js/darkfish.js" type="text/javascript"
20
+ charset="utf-8"></script>
21
+ </head>
22
+
23
+ <body class="file file-popup">
24
+ <div id="metadata">
25
+ <dl>
26
+ <dt class="modified-date">Last Modified</dt>
27
+ <dd class="modified-date">2011-02-01 11:12:46 +0100</dd>
28
+
29
+
30
+ <dt class="requires">Requires</dt>
31
+ <dd class="requires">
32
+ <ul>
33
+
34
+ <li>date</li>
35
+
36
+ </ul>
37
+ </dd>
38
+
39
+
40
+
41
+ </dl>
42
+ </div>
43
+
44
+ <div id="documentation">
45
+
46
+ <div class="description">
47
+ <h2>Description</h2>
48
+
49
+ </div>
50
+
51
+ </div>
52
+ </body>
53
+ </html>
54
+
data/lib/wd.rb ADDED
@@ -0,0 +1,65 @@
1
+
2
+ module Weekday
3
+
4
+ # :stopdoc:
5
+ LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
6
+ PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
7
+ # :startdoc:
8
+
9
+ # Returns the version string for the library.
10
+ #
11
+ def self.version
12
+ @version ||= File.read(path('version.txt')).strip
13
+ end
14
+
15
+ # Returns the library path for the module. If any arguments are given,
16
+ # they will be joined to the end of the libray path using
17
+ # <tt>File.join</tt>.
18
+ #
19
+ def self.libpath( *args, &block )
20
+ rv = args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
21
+ if block
22
+ begin
23
+ $LOAD_PATH.unshift LIBPATH
24
+ rv = block.call
25
+ ensure
26
+ $LOAD_PATH.shift
27
+ end
28
+ end
29
+ return rv
30
+ end
31
+
32
+ # Returns the lpath for the module. If any arguments are given,
33
+ # they will be joined to the end of the path using
34
+ # <tt>File.join</tt>.
35
+ #
36
+ def self.path( *args, &block )
37
+ rv = args.empty? ? PATH : ::File.join(PATH, args.flatten)
38
+ if block
39
+ begin
40
+ $LOAD_PATH.unshift PATH
41
+ rv = block.call
42
+ ensure
43
+ $LOAD_PATH.shift
44
+ end
45
+ end
46
+ return rv
47
+ end
48
+
49
+ # Utility method used to require all files ending in .rb that lie in the
50
+ # directory below this file that has the same name as the filename passed
51
+ # in. Optionally, a specific _directory_ name can be passed in such that
52
+ # the _filename_ does not have to be equivalent to the directory.
53
+ #
54
+ def self.require_all_libs_relative_to( fname, dir = nil )
55
+ dir ||= ::File.basename(fname, '.*')
56
+ search_me = ::File.expand_path(
57
+ ::File.join(::File.dirname(fname), dir, '**', '*.rb'))
58
+
59
+ Dir.glob(search_me).sort.each {|rb| require rb}
60
+ end
61
+
62
+ end # module Weekday
63
+
64
+ Weekday.require_all_libs_relative_to(__FILE__)
65
+
data/lib/weekday.rb ADDED
@@ -0,0 +1,100 @@
1
+ #encoding: utf-8
2
+
3
+ require 'date'
4
+
5
+ class Weekday
6
+
7
+ =begin
8
+ The Class Weekday can create date objects by calling class methods like
9
+ * Weekday.first_monday(2011, 2) # first monday in february 2011
10
+ * Weekday.third_thursday(2011, 3) # third_thursday in march 2011
11
+ * Weekday.last_sunday(2011, 8) # last sunday in august 2011
12
+ * Weekday.fridays(2011, 9) # an array of date objects with all fridays in 9/2011
13
+
14
+ Every combination from 'first' to 'fifth' and 'last' and the a name of a weekday from
15
+ 'monday' to 'sunday' is allowed.
16
+
17
+ If a certain date like 'fifth_saturday' is not possible, nil is returned.
18
+ =end
19
+
20
+
21
+ COUNTWORDS = %w(first second third fourth fifth last)
22
+ DAYNAMES = %w(sunday monday tuesday wednesday thursday friday saturday)
23
+ DAYNAMESS = %w(sundays mondays tuesdays wednesdays thursdays fridays saturdays)
24
+ PREV = %w(nil nil) + COUNTWORDS
25
+ ILLEGAL_ARGUMENTS = "illegal month or year number"
26
+ DAYS_PER_WEEK = 7
27
+
28
+ # Weekday.method_missing implements the methods like
29
+ # * Weekday.second_tuesday(2010,12) or
30
+ # * Weekday.last_monday(2010,11)
31
+ # The return value is a date object or nil if there is no Nth Xday in this month
32
+ def self.method_missing(m, *args)
33
+ # self.mondays .. self.sundays
34
+ if DAYNAMESS.include?(m.to_s)
35
+ wnum = DAYNAMESS.index(m.to_s)
36
+ begin
37
+ d = Date.new(args[0],args[1])
38
+ rescue ArgumentError
39
+ raise ArgumentError, ILLEGAL_ARGUMENTS
40
+ end
41
+ result = []
42
+ while d.month == args[1] do
43
+ if d.wday == wnum
44
+ result << d
45
+ loop do
46
+ d += DAYS_PER_WEEK
47
+ break if not d.month == args[1]
48
+ result << d
49
+ end
50
+ end
51
+ d += 1
52
+ end
53
+ return result
54
+ end
55
+ count,weekday = m.to_s.split('_')
56
+ unless COUNTWORDS.include?(count)
57
+ raise NoMethodError, "undefined method `#{m}'"
58
+ end
59
+ count = COUNTWORDS.index(count)+1
60
+ wnum = DAYNAMES.index(weekday)
61
+ unless wnum
62
+ raise NoMethodError, "undefined method `#{m}'"
63
+ end
64
+ # self.last_monday .. self.last_sunday
65
+ if count == COUNTWORDS.index(COUNTWORDS[-1])+1
66
+ d = Date.new(args[0],args[1],-1)
67
+ while not d.wday == wnum do
68
+ d -= 1
69
+ end
70
+ return d
71
+ end
72
+ case count
73
+ when 1
74
+ begin
75
+ d = Date.new(args[0],args[1],1)
76
+ rescue ArgumentError
77
+ raise ArgumentError, ILLEGAL_ARGUMENTS
78
+ end
79
+ while not d.wday == wnum do
80
+ d += 1
81
+ end
82
+ return d
83
+ when 2..5
84
+ m_name = (PREV[count] + '_' + weekday).to_sym
85
+ prev = self.send((PREV[count] + '_' + weekday), args[0], args[1])
86
+ result = if prev
87
+ prev + DAYS_PER_WEEK
88
+ else
89
+ nil
90
+ end
91
+ unless result and result.month == args[1]
92
+ result = nil
93
+ end
94
+ return result
95
+ else
96
+ raise NoMethodError, "undefined method `#{m}'"
97
+ end
98
+ end
99
+ end
100
+
data/test/test_wd.rb ADDED
@@ -0,0 +1,126 @@
1
+ require './lib/weekday'
2
+ require 'test/unit'
3
+
4
+ class TestSamstag < Test::Unit::TestCase
5
+ def test_class
6
+ assert_equal Date, Weekday.first_saturday(2010,12).class
7
+ end
8
+
9
+ def test_first_saturday_2010_12
10
+ assert_equal Date.new(2010,12,4),
11
+ Weekday.first_saturday(2010,12)
12
+ end
13
+
14
+ def test_second_saturday_2010_12
15
+ assert_equal Date.new(2010,12,11),
16
+ Weekday.second_saturday(2010,12)
17
+ end
18
+
19
+ def test_third_saturday_2010_12
20
+ assert_equal Date.new(2010,12,18),
21
+ Weekday.third_saturday(2010,12)
22
+ end
23
+
24
+ def test_fourth_saturday_2010_12
25
+ assert_equal Date.new(2010,12,25),
26
+ Weekday.fourth_saturday(2010,12)
27
+ end
28
+
29
+ def test_fifth_saturday_2010_12
30
+ assert_nil Weekday.fifth_saturday(2010,12)
31
+ end
32
+
33
+ def test_first_sunday
34
+ assert_equal Date.new(2010,12,5),
35
+ Weekday.first_sunday(2010,12)
36
+ end
37
+
38
+ def test_second_sunday
39
+ assert_equal Date.new(2010,12,12),
40
+ Weekday.second_sunday(2010,12)
41
+ end
42
+
43
+ def test_third_sunday
44
+ assert_equal Date.new(2010,12,19),
45
+ Weekday.third_sunday(2010,12)
46
+ end
47
+
48
+ def test_fourth_sunday
49
+ assert_equal Date.new(2010,12,26),
50
+ Weekday.fourth_sunday(2010,12)
51
+ end
52
+
53
+ def test_fifth_sunday
54
+ assert_nil Weekday.fifth_sunday(2010,12)
55
+ end
56
+
57
+ def test_sixth_sunday
58
+ assert_raises(NoMethodError) {
59
+ assert_nil Weekday.sixth_sunday(2010,12)
60
+ }
61
+ end
62
+
63
+ # falscher Methodenname muss zu Fehler fuehren
64
+ def test_first_birthday
65
+ assert_raises(NoMethodError) {
66
+ assert_nil Weekday.first_birthday(2010,12)
67
+ }
68
+ end
69
+
70
+ def test_last_sunday
71
+ assert_equal Date.new(2010,12,26),
72
+ Weekday.last_sunday(2010,12)
73
+ end
74
+
75
+ def test_first_saturday_2010_12_deutsch
76
+ assert_raises(NoMethodError) {
77
+ assert_equal Date.new(2010,12,4),
78
+ Weekday.erster_samstag(2010,12)
79
+ }
80
+ end
81
+
82
+ def test_1582_10
83
+ assert_equal Date.new(1582,10,1),
84
+ Weekday.first_monday(1582,10)
85
+ assert_equal Date.new(1582,10,2),
86
+ Weekday.first_tuesday(1582,10)
87
+ assert_equal Date.new(1582,10,3),
88
+ Weekday.first_wednesday(1582,10)
89
+ assert_equal Date.new(1582,10,4),
90
+ Weekday.first_thursday(1582,10)
91
+ assert_equal Date.new(1582,10,15),
92
+ Weekday.first_friday(1582,10)
93
+ assert_equal Date.new(1582,10,16),
94
+ Weekday.first_saturday(1582,10)
95
+ assert_equal Date.new(1582,10,17),
96
+ Weekday.first_sunday(1582,10)
97
+ assert_equal Date.new(1582,10,18),
98
+ Weekday.second_monday(1582,10)
99
+ assert_equal Date.new(1582,10,25),
100
+ Weekday.third_monday(1582,10)
101
+ assert_nil Weekday.fourth_monday(1582,10)
102
+ assert_equal Date.new(1582,10,29),
103
+ Weekday.last_friday(1582,10)
104
+ end
105
+
106
+ def test_bug_fifth_saturday_1582
107
+ assert_nothing_raised {
108
+ result = Weekday.fifth_saturday(1582,10)
109
+ }
110
+ end
111
+
112
+ def test_saturdays
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))
118
+ end
119
+
120
+ def test_illegal_month_should_raise_exception
121
+ assert_raises(ArgumentError) {
122
+ assert_nil Weekday.first_monday(2010,20)
123
+ }
124
+ end
125
+ end
126
+
data/version.txt ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weekday
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Thomas Preymesser
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-14 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: bones
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 6
31
+ - 5
32
+ version: 3.6.5
33
+ 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'
36
+ email: thopre@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - History.txt
43
+ - lib/doc/Object.html
44
+ - lib/doc/Weekday.html
45
+ - lib/doc/created.rid
46
+ - lib/doc/images/brick.png
47
+ - lib/doc/images/brick_link.png
48
+ - lib/doc/images/bug.png
49
+ - lib/doc/images/bullet_black.png
50
+ - lib/doc/images/bullet_toggle_minus.png
51
+ - lib/doc/images/bullet_toggle_plus.png
52
+ - lib/doc/images/date.png
53
+ - lib/doc/images/find.png
54
+ - lib/doc/images/loadingAnimation.gif
55
+ - lib/doc/images/macFFBgHack.png
56
+ - lib/doc/images/package.png
57
+ - lib/doc/images/page_green.png
58
+ - lib/doc/images/page_white_text.png
59
+ - lib/doc/images/page_white_width.png
60
+ - lib/doc/images/plugin.png
61
+ - lib/doc/images/ruby.png
62
+ - lib/doc/images/tag_green.png
63
+ - lib/doc/images/wrench.png
64
+ - lib/doc/images/wrench_orange.png
65
+ - lib/doc/images/zoom.png
66
+ - lib/doc/index.html
67
+ - lib/doc/js/darkfish.js
68
+ - lib/doc/js/jquery.js
69
+ - lib/doc/js/quicksearch.js
70
+ - lib/doc/js/thickbox-compressed.js
71
+ - lib/doc/rdoc.css
72
+ - lib/doc/weekday_rb.html
73
+ files:
74
+ - History.txt
75
+ - README.md
76
+ - Rakefile
77
+ - examples/termine.rb
78
+ - lib/doc/Object.html
79
+ - lib/doc/Weekday.html
80
+ - lib/doc/created.rid
81
+ - lib/doc/images/brick.png
82
+ - lib/doc/images/brick_link.png
83
+ - lib/doc/images/bug.png
84
+ - lib/doc/images/bullet_black.png
85
+ - lib/doc/images/bullet_toggle_minus.png
86
+ - lib/doc/images/bullet_toggle_plus.png
87
+ - lib/doc/images/date.png
88
+ - lib/doc/images/find.png
89
+ - lib/doc/images/loadingAnimation.gif
90
+ - lib/doc/images/macFFBgHack.png
91
+ - lib/doc/images/package.png
92
+ - lib/doc/images/page_green.png
93
+ - lib/doc/images/page_white_text.png
94
+ - lib/doc/images/page_white_width.png
95
+ - lib/doc/images/plugin.png
96
+ - lib/doc/images/ruby.png
97
+ - lib/doc/images/tag_green.png
98
+ - lib/doc/images/wrench.png
99
+ - lib/doc/images/wrench_orange.png
100
+ - lib/doc/images/zoom.png
101
+ - lib/doc/index.html
102
+ - lib/doc/js/darkfish.js
103
+ - lib/doc/js/jquery.js
104
+ - lib/doc/js/quicksearch.js
105
+ - lib/doc/js/thickbox-compressed.js
106
+ - lib/doc/rdoc.css
107
+ - lib/doc/weekday_rb.html
108
+ - lib/wd.rb
109
+ - lib/weekday.rb
110
+ - test/test_wd.rb
111
+ - version.txt
112
+ has_rdoc: true
113
+ homepage: http://weekday.rubyforge.net/
114
+ licenses: []
115
+
116
+ post_install_message:
117
+ rdoc_options:
118
+ - --main
119
+ - README.md
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ 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
131
+ none: false
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ segments:
136
+ - 0
137
+ version: "0"
138
+ requirements: []
139
+
140
+ rubyforge_project: weekday
141
+ rubygems_version: 1.3.7
142
+ signing_key:
143
+ 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:
146
+ - test/test_wd.rb