Created
June 27, 2010 17:54
-
-
Save tanzeeb/455049 to your computer and use it in GitHub Desktop.
RPCFN #10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'time' | |
class BusinessHours | |
def initialize opening, closing | |
@hours = Hash.new( [opening, Time.parse(closing)-Time.parse(opening)] ) | |
end | |
def calculate_deadline interval, drop_off | |
target = seconds_before_closing( time = Time.parse(drop_off) ) | |
target += open_hours( time += 86400 ).last while target < interval | |
time_after_opening( time, interval - (target - open_hours(time).last) ) | |
end | |
def closed *dates | |
dates.each { |date| @hours[date] = ["12:00 PM", 0] } | |
end | |
def update day, opening, closing | |
@hours[day] = [ opening, Time.parse(closing)-Time.parse(opening) ] | |
end | |
private | |
def time_to_day time | |
Time::RFC2822_DAY_NAME[string_or_time(time).wday].downcase.to_sym | |
end | |
def time_to_date time | |
string_or_time(time).strftime("%b %d, %Y") | |
end | |
def string_or_time time | |
time.is_a?(String) ? Time.parse(time) : time | |
end | |
def open_hours time | |
@hours.fetch(time_to_date time) { @hours[time_to_day time] } | |
end | |
def seconds_before_closing time | |
open_hours(time).last - (time - Time.parse(open_hours(time).first, time) ) | |
end | |
def time_after_opening time, interval | |
Time.parse(open_hours(time).first, time) + interval | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'business_hours' | |
describe BusinessHours do | |
subject { | |
bh = BusinessHours.new "9:00 AM", "3:00 PM" | |
bh.update :fri, "10:00 AM", "5:00 PM" | |
bh.update "Dec 24, 2010", "8:00 AM", "1:00 PM" | |
bh.closed :sun, :wed, "Dec 25, 2010" | |
bh | |
} | |
it { subject.send(:open_hours,"Dec 31, 2010").should == ["10:00 AM", Time.parse("5:00 PM") - Time.parse("10:00 AM")] } | |
it { subject.send(:open_hours,"Dec 24, 2010").should == ["8:00 AM", Time.parse("1:00 PM") - Time.parse("8:00 AM")] } | |
it { subject.send(:open_hours,"Dec 23, 2010").should == ["9:00 AM", Time.parse("3:00 PM") - Time.parse("9:00 AM")] } | |
it { ["Dec 19, 2010","Dec 22, 2010","Dec 25, 2010"].each { |date| subject.send(:open_hours,date).last == 0 } } | |
it { subject.send(:seconds_before_closing,Time.parse("Dec 31, 2010 4:00 PM")).should == 60*60 } | |
it { subject.send(:seconds_before_closing,Time.parse("Dec 31, 2010 10:00 AM")).should == 7*60*60 } | |
it { subject.send(:time_after_opening,Time.parse("Dec 31, 2010"), 3600).should == Time.parse("Dec 31, 2010 11:00 AM") } | |
it { subject.send(:time_after_opening,Time.parse("Dec 31, 2010 6:00 PM"), 3600).should == Time.parse("Dec 31, 2010 11:00 AM") } | |
it { subject.calculate_deadline(2*60*60, "Jun 7, 2010 9:10 AM").should == Time.parse("Mon Jun 07 11:10:00 2010") } | |
it { subject.calculate_deadline(15*60, "Jun 8, 2010 2:48 PM").should == Time.parse("Thu Jun 10 09:03:00 2010") } | |
it { subject.calculate_deadline(15*60, "Jun 8, 2010 2:48 PM").should == Time.parse("Thu Jun 10 09:03:00 2010") } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment