weekdays 1.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.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Matt Darby
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,36 @@
1
+ h1. weekdays
2
+
3
+ A Ruby on Rails gem that provides methods to work with 'weekdays' (ie. 5.weekdays_ago)
4
+
5
+ h2. What it does
6
+
7
+ weekdays extends Rails' Date, Time, DateTime, and Numeric ActiveSupport Core Extensions and gives you a handful of helpful methods that calculate spans of weekdays.
8
+
9
+ h2. Requirements
10
+
11
+ This gem ;)
12
+
13
+ h2. How to Install
14
+
15
+ <pre>sudo gem install weekdays --source 'http://gemcutter.com'</pre>
16
+
17
+ Then add it to your environment.rb:
18
+ <pre>config.gem "weekdays"</pre>
19
+
20
+ h2. How to Use
21
+
22
+ You can now do cool things like:
23
+
24
+ <pre>
25
+ Date.today.weekday? # => true
26
+ 5.weekdays_ago # => Tue Mar 24 10:46:42 -0400 2009
27
+ 11.weekdays_from_now # => Wed Apr 15 10:46:54 -0400 2009
28
+ 20.weekdays_from(Date.yesterday) # => Mon, 27 Apr 2009
29
+ Date.today.weekdays_until(Date.tomorrow) # => 1
30
+ </pre>
31
+
32
+ h2. About the Author
33
+
34
+ My name is "Matt Darby.":http://blog.matt-darby.com I’m an IT Manager and pro-web-dev at for "Dynamix Engineering":http://dynamix-ltd.com and hold a Master’s Degree in Computer Science from "Franklin University":http://www.franklin.edu in sunny "Columbus, OH.":http://en.wikipedia.org/wiki/Columbus,_Ohio
35
+
36
+ Feel free to check out my "blog":http://blog.matt-darby.com or "recommend me":http://www.workingwithrails.com/person/10908-matt-darby
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), "lib", "weekdays")
data/lib/weekdays.rb ADDED
@@ -0,0 +1,92 @@
1
+ module ActiveSupport #:nodoc:
2
+ module CoreExtensions #:nodoc:
3
+ module Date #:nodoc:
4
+ # Enables the use of time calculations within Date itself
5
+ module Calculations
6
+ # Tells whether the Date object is a weekday
7
+ def weekday?
8
+ (1..5).include?(wday)
9
+ end
10
+
11
+ # Returns the number of weekdays until a future Date
12
+ def weekdays_until(date)
13
+ return 0 if date <= self
14
+ (self...date).select{|day| day.weekday?}.size
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ module ActiveSupport #:nodoc:
22
+ module CoreExtensions #:nodoc:
23
+ module Time #:nodoc:
24
+ # Enables the use of time calculations within Time itself
25
+ module Calculations
26
+ def weekday?
27
+ (1..5).include?(wday)
28
+ end
29
+
30
+ def weekdays_until(date)
31
+ return 0 if date <= self.to_date
32
+ (self.to_date...date).select{|day| day.weekday?}.size
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ module ActiveSupport #:nodoc:
40
+ module CoreExtensions #:nodoc:
41
+ module DateTime #:nodoc:
42
+ # Enables the use of time calculations within DateTime itself
43
+ module Calculations
44
+ # Tells whether the Date object is a weekday
45
+ def weekday?
46
+ (1..5).include?(wday)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ module ActiveSupport #:nodoc:
54
+ module CoreExtensions
55
+ module Numeric
56
+ module Time
57
+ # Returns a Time object that is n number of weekdays in the future of a given Date
58
+ def weekdays_from(time = ::Time.now)
59
+ # -5.weekdays_from(time) == 5.weekdays_ago(time)
60
+ return self.abs.weekdays_ago(time) if self < 0
61
+
62
+ x = 0
63
+ curr_date = time
64
+
65
+ until x == self
66
+ curr_date += 1.days
67
+ x += 1 if curr_date.weekday?
68
+ end
69
+
70
+ curr_date
71
+ end
72
+ alias :weekdays_from_now :weekdays_from
73
+
74
+ # Returns a Time object that is n number of weekdays in the past from a given Date
75
+ def weekdays_ago(time = ::Time.now)
76
+ # -5.weekdays_ago(time) == 5.weekdays_from(time)
77
+ return self.abs.weekdays_from(time) if self < 0
78
+
79
+ x = 0
80
+ curr_date = time
81
+
82
+ until x == self
83
+ curr_date -= 1.days
84
+ x += 1 if curr_date.weekday?
85
+ end
86
+
87
+ curr_date
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'activesupport'
3
+
4
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'weekdays.rb')
5
+
6
+ Spec::Runner.configure do |config|
7
+ end
@@ -0,0 +1,127 @@
1
+ require File.join(File.dirname(__FILE__), %w[spec_helper])
2
+
3
+ describe "Date Extension" do
4
+
5
+ describe "Checking a Date for weekday status" do
6
+
7
+ it "should know that Mon-Fri are weekdays" do
8
+ (1...5).each do |i|
9
+ Date.new(2000, 1, (3 + i)).weekday?.should be_true
10
+ end
11
+ end
12
+
13
+ it "should know that Sunday is not a weekday" do
14
+ Date.new(2000, 1, 2).weekday?.should be_false
15
+ end
16
+
17
+ it "should know that Saturday is not a weekday" do
18
+ Date.new(2000, 1, 1).weekday?.should be_false
19
+ end
20
+
21
+ end
22
+
23
+ describe "Calculating weekdays between Dates" do
24
+ it "should return 2086" do
25
+ Date.new(2000, 1, 1).weekdays_until(Date.new(2008, 1, 1)).should == 2086
26
+ end
27
+ end
28
+
29
+ end
30
+
31
+ describe "Time Extension" do
32
+
33
+ describe "Checking a Time for weekday status" do
34
+
35
+ it "should know that Mon-Fri are weekdays" do
36
+ (1...5).each do |i|
37
+ Date.new(2000, 1, (3 + i)).beginning_of_day.weekday?.should be_true
38
+ end
39
+ end
40
+
41
+ it "should know that Sunday is not a weekday" do
42
+ Date.new(2000, 1, 2).beginning_of_day.weekday?.should be_false
43
+ end
44
+
45
+ it "should know that Saturday is not a weekday" do
46
+ Date.new(2000, 1, 1).beginning_of_day.weekday?.should be_false
47
+ end
48
+
49
+ end
50
+
51
+ describe "Calculating weekdays between Times" do
52
+ it "should return 521" do
53
+ Date.new(2000, 1, 1).beginning_of_day.weekdays_until(Date.new(2002, 1, 1)).should == 521
54
+ end
55
+ end
56
+
57
+ end
58
+
59
+ describe "DateTime Extension" do
60
+
61
+ describe "Checking a DateTime for weekday status" do
62
+
63
+ it "should know that Mon-Fri are weekdays" do
64
+ (1...5).each do |i|
65
+ DateTime.new(2000, 1, (3 + i), 1, 1, 1).weekday?.should be_true
66
+ end
67
+ end
68
+
69
+ it "should know that Sunday is not a weekday" do
70
+ DateTime.new(2000, 1, 2, 1, 1, 1).weekday?.should be_false
71
+ end
72
+
73
+ it "should know that Saturday is not a weekday" do
74
+ DateTime.new(2000, 1, 1, 1, 1, 1).weekday?.should be_false
75
+ end
76
+
77
+ end
78
+
79
+ end
80
+
81
+ describe "Numeric::Time Extension" do
82
+
83
+ describe "#weekdays_from" do
84
+ it "should return 5" do
85
+ 5.weekdays_from(Date.new(2000, 1, 1).beginning_of_day).should == Date.new(2000, 1, 7).beginning_of_day
86
+ end
87
+ end
88
+
89
+ describe "#weekdays_from_now" do
90
+ it "should return 5" do
91
+ Time.stub!(:now => Date.new(2000, 1, 1).beginning_of_day)
92
+ 5.weekdays_from_now.should == Date.new(2000, 1, 7).beginning_of_day
93
+ end
94
+
95
+ it "should be able to handle zero cases" do
96
+ Time.stub!(:now => Date.new(2000, 1, 1).beginning_of_day)
97
+ 0.weekdays_from_now.should == Date.new(2000, 1, 1).beginning_of_day
98
+ end
99
+
100
+ it "should be able to handle negative spans" do
101
+ Time.stub!(:now => Date.new(2000, 1, 1).beginning_of_day)
102
+ -5.weekdays_from_now.should == Date.new(1999, 12, 27).beginning_of_day
103
+ end
104
+
105
+ end
106
+
107
+
108
+ describe "#weekdays_ago" do
109
+ it "should return 5" do
110
+ Time.stub!(:now => Date.new(2000, 1, 1).beginning_of_day)
111
+ 5.weekdays_ago.should == Date.new(1999, 12, 27).beginning_of_day
112
+ 5.weekdays_ago(Date.new(1999, 12, 27).beginning_of_day).should == Date.new(1999, 12, 20).beginning_of_day
113
+ end
114
+
115
+ it "should handle zero spans" do
116
+ Time.stub!(:now => Date.new(2000, 1, 1).beginning_of_day)
117
+ 0.weekdays_ago.should == Date.new(2000, 1, 1).beginning_of_day
118
+ end
119
+
120
+ it "should handle negative spans" do
121
+ Time.stub!(:now => Date.new(2000, 1, 1).beginning_of_day)
122
+ -5.weekdays_ago.should == Date.new(2000, 1, 7).beginning_of_day
123
+ end
124
+
125
+ end
126
+
127
+ end
data/weekdays.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "weekdays"
3
+ s.version = "1.0"
4
+ s.date = "2009-03-31"
5
+ s.summary = "Provides methods to work with 'weekdays'"
6
+ s.email = "matt@matt-darby.com"
7
+ s.homepage = "http://github.com/mdarby/weekdays/tree/master"
8
+ s.description = "Provides methods to work with 'weekdays' (ie. 5.weekdays_ago)"
9
+ s.has_rdoc = false
10
+ s.authors = ["Matt Darby"]
11
+ s.files = [
12
+ 'MIT-LICENSE',
13
+ 'README.textile',
14
+ 'init.rb',
15
+ 'lib/weekdays.rb',
16
+ 'spec/weekdays_spec.rb',
17
+ 'spec/spec_helper.rb',
18
+ 'weekdays.gemspec',
19
+ ]
20
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weekdays
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
5
+ platform: ruby
6
+ authors:
7
+ - Matt Darby
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-31 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Provides methods to work with 'weekdays' (ie. 5.weekdays_ago)
17
+ email: matt@matt-darby.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - MIT-LICENSE
26
+ - README.textile
27
+ - init.rb
28
+ - lib/weekdays.rb
29
+ - spec/weekdays_spec.rb
30
+ - spec/spec_helper.rb
31
+ - weekdays.gemspec
32
+ has_rdoc: true
33
+ homepage: http://github.com/mdarby/weekdays/tree/master
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.3.5
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: Provides methods to work with 'weekdays'
60
+ test_files: []
61
+