business_days 0.0.1 → 0.0.2
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/README.rdoc +8 -1
- data/business_days.gemspec +1 -1
- data/lib/business_days.rb +1 -0
- data/lib/business_days/date.rb +42 -0
- data/spec/business_days_spec.rb +53 -22
- metadata +4 -3
data/README.rdoc
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
= business_days
|
2
2
|
|
3
|
-
Business Days is a sort-of extension to ActiveSupport to allow adding business days to time objects.
|
3
|
+
Business Days is a sort-of extension to ActiveSupport to allow adding business days to time objects.
|
4
4
|
|
5
5
|
== Installation
|
6
6
|
|
@@ -29,3 +29,10 @@ Or add it to your Gemfile.
|
|
29
29
|
(Time.parse("Friday") - 1.business_day).strftime("%A")
|
30
30
|
# => "Thursday"
|
31
31
|
|
32
|
+
== Current Limitations
|
33
|
+
|
34
|
+
* Holidays are completely ignored.
|
35
|
+
|
36
|
+
* Time.now + 123512.business_days is done iteratively, even though it would be fairly straightforward to add (5/7 * n) + or - some small number based on where in the week you started.
|
37
|
+
|
38
|
+
* saturday or sunday + 0.business_days is Monday. There are strong arguments both ways for making this result either monday or saturday. I think Monday is the more helpful. Write me hatemail if you disagree.
|
data/business_days.gemspec
CHANGED
data/lib/business_days.rb
CHANGED
@@ -0,0 +1,42 @@
|
|
1
|
+
module BusinessDays
|
2
|
+
module Date
|
3
|
+
module Calculations
|
4
|
+
def self.included(base) #:nodoc:
|
5
|
+
|
6
|
+
base.class_eval do
|
7
|
+
alias_method :plus_without_business_days, :+
|
8
|
+
alias_method :+, :plus_with_business_days
|
9
|
+
|
10
|
+
alias_method :minus_without_business_days, :-
|
11
|
+
alias_method :-, :minus_with_business_days
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def is_business_day?
|
16
|
+
# TODO: Holidays.
|
17
|
+
! [0,6].include?(self.wday)
|
18
|
+
end
|
19
|
+
|
20
|
+
def plus_with_business_days(other)
|
21
|
+
if BusinessDays::BusinessDayDuration === other
|
22
|
+
other.calculate_duration(self, :+)
|
23
|
+
else
|
24
|
+
plus_without_business_days(other)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def minus_with_business_days(other)
|
29
|
+
if BusinessDays::BusinessDayDuration === other
|
30
|
+
other.calculate_duration(self, :-)
|
31
|
+
else
|
32
|
+
minus_without_business_days(other)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class Date
|
41
|
+
include BusinessDays::Date::Calculations
|
42
|
+
end
|
data/spec/business_days_spec.rb
CHANGED
@@ -3,30 +3,61 @@ require File.join(File.dirname(__FILE__),'..','lib','business_days')
|
|
3
3
|
|
4
4
|
describe BusinessDays do
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
6
|
+
describe "with Time" do
|
7
|
+
before do
|
8
|
+
@friday = Time.parse("Friday, September 10th, 2010")
|
9
|
+
@thursday = @friday - 1.day
|
10
|
+
@saturday = @friday + 1.day
|
11
|
+
@monday = @friday + 3.days
|
12
|
+
@tuesday = @friday + 4.days
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should add properly" do
|
16
|
+
(@friday + 1.business_day).should == @monday
|
17
|
+
(@friday + 2.business_days).should == @tuesday
|
18
|
+
(@saturday + 1.business_days).should == @tuesday
|
19
|
+
(@saturday + 0.business_days).should == @monday
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should subtract properly" do
|
23
|
+
(@saturday - 0.business_days).should == @friday
|
24
|
+
(@saturday - 1.business_day).should == @thursday
|
25
|
+
(@monday - 1.business_days).should == @friday
|
26
|
+
(@monday - 0.business_days).should == @monday
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should add with the params reversed" do
|
30
|
+
(1.business_day + @friday).should == @monday
|
31
|
+
end
|
26
32
|
end
|
27
33
|
|
28
|
-
|
29
|
-
|
34
|
+
describe "with Date" do
|
35
|
+
before do
|
36
|
+
@friday = Date.parse("Friday, September 10th, 2010")
|
37
|
+
@thursday = @friday - 1.day
|
38
|
+
@saturday = @friday + 1.day
|
39
|
+
@monday = @friday + 3.days
|
40
|
+
@tuesday = @friday + 4.days
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should add properly" do
|
44
|
+
(@friday + 1.business_day).should == @monday
|
45
|
+
(@friday + 2.business_days).should == @tuesday
|
46
|
+
(@saturday + 1.business_days).should == @tuesday
|
47
|
+
(@saturday + 0.business_days).should == @monday
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should subtract properly" do
|
51
|
+
(@saturday - 0.business_days).should == @friday
|
52
|
+
(@saturday - 1.business_day).should == @thursday
|
53
|
+
(@monday - 1.business_days).should == @friday
|
54
|
+
(@monday - 0.business_days).should == @monday
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should add with the params reversed" do
|
58
|
+
(1.business_day + @friday).should == @monday
|
59
|
+
end
|
60
|
+
|
30
61
|
end
|
31
62
|
|
32
63
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: business_days
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Burke Libbey
|
@@ -41,6 +41,7 @@ extensions: []
|
|
41
41
|
extra_rdoc_files: []
|
42
42
|
|
43
43
|
files:
|
44
|
+
- lib/business_days/date.rb
|
44
45
|
- lib/business_days/time.rb
|
45
46
|
- lib/business_days.rb
|
46
47
|
- spec/business_days_spec.rb
|