lib/roxbury/business_calendar.rb in roxbury-0.1.1 vs lib/roxbury/business_calendar.rb in roxbury-0.2.1

- old
+ new

@@ -36,11 +36,11 @@ rolling_timestamp = roll_forward(to) remaining_hours = number_of_hours until (bday = business_day(rolling_timestamp)).include?(rolling_timestamp + remaining_hours.hours) remaining_hours -= bday.number_of_working_hours(from: rolling_timestamp) - rolling_timestamp = at_beginning_of_next_business_day(rolling_timestamp) + rolling_timestamp = next_working_day(rolling_timestamp) end rolling_timestamp + remaining_hours.hours end @@ -62,11 +62,11 @@ add_working_hours(to, number_of_days * max_working_hours_in_a_day) else remaining_days = number_of_days rolling_date = to loop do - remaining_days -= business_day(rolling_date).number_of_working_hours * 1.0 / max_working_hours_in_a_day + remaining_days -= working_hours_percentage(rolling_date) break if remaining_days < 0 rolling_date = roll_forward rolling_date.next end rolling_date end @@ -84,12 +84,24 @@ else roll_forward(date.is_a?(Date) ? date.tomorrow : date.tomorrow.beginning_of_day) end end - # Snaps the date to the beginning of the next business day. - def at_beginning_of_next_business_day date - roll_forward date.tomorrow.beginning_of_day + # If a Date is given, returns then next business day. + # Otherwise if a Time is given, snaps the date to the beginning of the next business day. + def next_working_day date + case date + when Time + roll_forward date.tomorrow.beginning_of_day + when Date + roll_forward date.tomorrow + else + raise ArgumentError, 'only Date or Time instances are allowed' + end + end + + def working_hours_percentage date + business_day(date).number_of_working_hours * 1.0 / max_working_hours_in_a_day end def holiday? date_or_time @holidays.include?(date_or_time.to_date) end