business_time 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.2
@@ -14,6 +14,7 @@ module BusinessTime
14
14
  end
15
15
 
16
16
  def after(time)
17
+ time = time.roll_forward
17
18
  @hours.times do
18
19
  time = time + 1.hour #add an hour
19
20
 
@@ -29,8 +30,9 @@ module BusinessTime
29
30
  end
30
31
 
31
32
  def before(time)
33
+ time = time.roll_forward
32
34
  @hours.times do
33
- time = time - 1.hour #add an hour
35
+ time = time - 1.hour #subtract an hour
34
36
 
35
37
  if (time < time.beginning_of_workday)
36
38
  time = time - off_hours # if that pushes us before business hours,
@@ -15,4 +15,40 @@ class Time
15
15
  def weekday?
16
16
  [1,2,3,4,5].include? self.wday
17
17
  end
18
+
19
+
20
+ def outsize_business_hours?
21
+ before_business_hours? || after_business_hours? || !workday?
22
+ end
23
+
24
+ # rolls forward to the next beginning_of_workday
25
+ # when the time is outside of business hours
26
+ def roll_forward
27
+ next_business_time = self
28
+
29
+ if (before_business_hours? || !workday?)
30
+ next_business_time = beginning_of_workday
31
+ end
32
+
33
+ if after_business_hours?
34
+ next_business_time = beginning_of_workday + 1.day
35
+ end
36
+
37
+ while !next_business_time.workday?
38
+ next_business_time = next_business_time + 1.day
39
+ end
40
+
41
+ next_business_time
42
+ end
43
+
44
+ private
45
+
46
+ def before_business_hours?
47
+ (self < self.beginning_of_workday)
48
+ end
49
+
50
+ def after_business_hours?
51
+ (self > self.end_of_workday)
52
+ end
53
+
18
54
  end
@@ -44,4 +44,21 @@ class TestBusinessHours < Test::Unit::TestCase
44
44
  assert expected == later
45
45
  end
46
46
 
47
+ should "roll forward to 9 am if asked in the early morning" do
48
+ crack_of_dawn_monday = Time.parse("Mon Apr 26, 04:30:00, 2010")
49
+ monday_morning = Time.parse("Mon Apr 26, 09:00:00, 2010")
50
+ assert_equal monday_morning, crack_of_dawn_monday.roll_forward
51
+ end
52
+
53
+ should "roll forward to the next morning if aftern business hours" do
54
+ monday_evening = Time.parse("Mon Apr 26, 18:00:00, 2010")
55
+ tuesday_morning = Time.parse("Tue Apr 27, 09:00:00, 2010")
56
+ assert_equal tuesday_morning, monday_evening.roll_forward
57
+ end
58
+
59
+ should "consider any time on a weekend as equivalent to monday morning" do
60
+ sunday = Time.parse("Sun Apr 25 12:06:56, 2010")
61
+ monday = Time.parse("Mon Apr 26, 09:00:00, 2010")
62
+ assert_equal 1.business_hour.before(monday), 1.business_hour.before(sunday)
63
+ end
47
64
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 1
9
- version: 0.2.1
8
+ - 2
9
+ version: 0.2.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - bokmann
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-22 00:00:00 -04:00
17
+ date: 2010-04-25 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency