opening_hours 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -26,6 +26,7 @@ hours = OpeningHours.new("9:00 AM", "3:00 PM", "Europe/Berlin")
26
26
  Methods to set the hours, closed times or holidays:
27
27
  ```ruby
28
28
  hours.update :fri, "10:00 AM", "5:00 PM"
29
+ hours.break :fri, "01:00 PM", "1:30 PM"
29
30
  hours.update "Dec 24, 2010", "8:00 AM", "1:00 PM"
30
31
  hours.closed :sun, :wed, "Dec 25, 2010"
31
32
  ```
@@ -62,6 +63,15 @@ hours.week[:mon].close.to_human_readable_hours
62
63
  => "03:00 PM"
63
64
  ```
64
65
 
66
+ You can fetch breaks also. Sorry for confusion... but i used same OpeningHour class for breaks as for the original hours.. thats why the attributes here are open and close too. But actually .open and .close means when break starts and ends.
67
+ ```ruby
68
+ hours.breaks[:fri].open.to_human_readable_hours
69
+ => "01:00 AM"
70
+
71
+ hours.breaks[:fri].close.to_human_readable_hours
72
+ => "01:30 PM"
73
+ ```
74
+
65
75
  ## Contributing
66
76
 
67
77
  1. Fork it
@@ -1,3 +1,3 @@
1
1
  class OpeningHours
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/lib/opening_hours.rb CHANGED
@@ -54,7 +54,7 @@ class OpeningHours
54
54
  end
55
55
 
56
56
  # makes it possible to access each day like: b.week[:sun].open, or iterate through all weekdays
57
- attr_reader :week
57
+ attr_reader :week, :breaks
58
58
 
59
59
  WEEK_DAYS = Time::RFC2822_DAY_NAME.map { |m| m.downcase.to_sym }
60
60
 
@@ -69,6 +69,7 @@ class OpeningHours
69
69
  end
70
70
 
71
71
  @specific_days = {}
72
+ @breaks = {}
72
73
  end
73
74
 
74
75
  def update(day, start_time, end_time)
@@ -81,6 +82,10 @@ class OpeningHours
81
82
  end
82
83
  end
83
84
 
85
+ def break(day, start_time, end_time)
86
+ set_break_hours day, OpenHours.parse(start_time, end_time)
87
+ end
88
+
84
89
  def now_open?
85
90
  calculate_deadline(0, Time.zone.now.to_s) == Time.zone.now.to_formatted_s(:rfc822)
86
91
  end
@@ -90,11 +95,17 @@ class OpeningHours
90
95
 
91
96
  today = Date.civil(start_date_time.year, start_date_time.month, start_date_time.day)
92
97
  open_hours = get_open_hours(today).offset(TimeUtils::seconds_from_midnight(start_date_time))
93
-
98
+ break_hours_duration = get_break_duration(today)
99
+ break_hours = get_break_hours(today)
94
100
  # here is possible to use strict greater operator if you want to stop on edge of previous business day.
95
101
  # see "BusinessHours schedule without exceptions should flip the edge" spec
96
- while job_duration >= open_hours.duration
97
- job_duration -= open_hours.duration
102
+
103
+ if open_hours.open + job_duration >= break_hours.open
104
+ job_duration += break_hours_duration
105
+ end
106
+
107
+ while job_duration >= (open_hours.duration - break_hours_duration)
108
+ job_duration -= open_hours.duration + break_hours_duration
98
109
 
99
110
  today = today.next
100
111
  open_hours = get_open_hours(today)
@@ -118,6 +129,24 @@ class OpeningHours
118
129
  end
119
130
  end
120
131
 
132
+ def get_break_hours(date)
133
+ @breaks[WEEK_DAYS[date.wday]] ||= OpenHours.new(0, 0)
134
+ end
135
+
136
+ def set_break_hours(day, break_hours)
137
+ case day
138
+ when Symbol
139
+ @breaks[day] = break_hours
140
+ end
141
+ end
142
+
143
+ def get_break_duration(date)
144
+ bhours = get_break_hours(date)
145
+ if bhours
146
+ bhours.close - bhours.open
147
+ end
148
+ end
149
+
121
150
  end
122
151
 
123
152
  Fixnum.class_eval do
@@ -223,4 +223,58 @@ describe OpeningHours do
223
223
  end
224
224
  end
225
225
 
226
+ context "with breaks" do
227
+
228
+ before do
229
+ @hours = OpeningHours.new("9:00 AM", "4:00 PM", "Europe/Moscow")
230
+ @hours.break :fri, "01:00 PM", "01:30 PM"
231
+ end
232
+
233
+ it "should open at 1:30 pm after the break" do
234
+ @hours.calculate_deadline(0, "Mar 22, 2013 01:00 PM +0400").should == Time.zone.parse("Mar 22, 2013 1:30 PM").to_formatted_s(:rfc822)
235
+ end
236
+
237
+ it "should open at 9:00 am next day" do
238
+ @hours.calculate_deadline(3*60*60, "Mar 22, 2013 01:00 PM +0400").should == Time.zone.parse("Mar 23, 2013 9:00 AM").to_formatted_s(:rfc822)
239
+ end
240
+
241
+ it "should open at 2:00 pm next day" do
242
+ @hours.calculate_deadline(8*60*60, "Mar 22, 2013 01:00 PM +0400").should == Time.zone.parse("Mar 23, 2013 2:00 PM").to_formatted_s(:rfc822)
243
+ end
244
+ end
245
+
246
+ context "with breaks and now open" do
247
+
248
+ context "with currently not in break" do
249
+ before do
250
+ open_time = Time.local(Date.today.year, Date.today.month, Date.today.day, Time.now.hour-2).strftime("%I:%M %p")
251
+ close_time = Time.local(Date.today.year, Date.today.month, Date.today.day, Time.now.hour+5).strftime("%I:%M %p")
252
+ break_start_time = Time.local(Date.today.year, Date.today.month, Date.today.day, Time.now.hour+1).strftime("%I:%M %p")
253
+ break_end_time = Time.local(Date.today.year, Date.today.month, Date.today.day, Time.now.hour+2).strftime("%I:%M %p")
254
+ @hours = OpeningHours.new(open_time, close_time, "Europe/Berlin")
255
+ @hours.break OpeningHours::WEEK_DAYS[Date.today.wday], break_start_time, break_end_time
256
+ end
257
+
258
+ it "should should be open" do
259
+ @hours.now_open?.should == true
260
+ end
261
+ end
262
+
263
+ context "with currently in break" do
264
+ before do
265
+ open_time = Time.local(Date.today.year, Date.today.month, Date.today.day, Time.now.hour-2).strftime("%I:%M %p")
266
+ close_time = Time.local(Date.today.year, Date.today.month, Date.today.day, Time.now.hour+5).strftime("%I:%M %p")
267
+ break_start_time = Time.local(Date.today.year, Date.today.month, Date.today.day, Time.now.hour).strftime("%I:%M %p")
268
+ break_end_time = Time.local(Date.today.year, Date.today.month, Date.today.day, Time.now.hour+1).strftime("%I:%M %p")
269
+ @hours = OpeningHours.new(open_time, close_time, "Europe/Berlin")
270
+ @hours.break OpeningHours::WEEK_DAYS[Date.today.wday], break_start_time, break_end_time
271
+ end
272
+
273
+ it "should be closed" do
274
+ @hours.now_open?.should == false
275
+ end
276
+ end
277
+
278
+ end
279
+
226
280
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opening_hours
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-15 00:00:00.000000000 Z
12
+ date: 2013-03-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec