Class: OperatingHours::Calculator
- Inherits:
-
Object
- Object
- OperatingHours::Calculator
- Defined in:
- lib/operating_hours/calculator.rb
Instance Method Summary collapse
- #add_days_to_date(days, date) ⇒ Object
- #days_between_dates(first_date, second_date) ⇒ Object
- #holiday?(time) ⇒ Boolean
-
#initialize(schedule:, holidays: []) ⇒ Calculator
constructor
A new instance of Calculator.
- #seconds_between_times(first_time, last_time) ⇒ Object
- #seconds_since_beginning_of_workday(time) ⇒ Object
- #seconds_until_end_of_workday(time) ⇒ Object
Constructor Details
#initialize(schedule:, holidays: []) ⇒ Calculator
Returns a new instance of Calculator.
3 4 5 6 |
# File 'lib/operating_hours/calculator.rb', line 3 def initialize(schedule:, holidays: []) @schedule = Schedule.new(schedule) @holidays = HolidayCollection.new(collection: holidays, schedule: @schedule) end |
Instance Method Details
#add_days_to_date(days, date) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/operating_hours/calculator.rb', line 36 def add_days_to_date(days, date) holiday_count = 0 loop do last_date = schedule.add_days_to_date(days + holiday_count, date) return last_date if holiday_count == holidays.days_in_date_range(date, last_date) holiday_count = holidays.days_in_date_range(date, last_date) end end |
#days_between_dates(first_date, second_date) ⇒ Object
26 27 28 29 30 |
# File 'lib/operating_hours/calculator.rb', line 26 def days_between_dates(first_date, second_date) return 0 if first_date == second_date schedule.days_in_date_range(first_date, second_date - 1) - holidays.days_in_date_range(first_date, second_date - 1) end |
#holiday?(time) ⇒ Boolean
32 33 34 |
# File 'lib/operating_hours/calculator.rb', line 32 def holiday?(time) holidays.include?(time.to_date) end |
#seconds_between_times(first_time, last_time) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/operating_hours/calculator.rb', line 8 def seconds_between_times(first_time, last_time) return -1 * seconds_between_times(last_time, first_time) if last_time < first_time schedule.seconds_in_date_range(first_time.to_date, last_time.to_date) - holidays.seconds_in_date_range(first_time.to_date, last_time.to_date) - seconds_since_beginning_of_workday(first_time) - seconds_until_end_of_workday(last_time) end |
#seconds_since_beginning_of_workday(time) ⇒ Object
16 17 18 19 |
# File 'lib/operating_hours/calculator.rb', line 16 def seconds_since_beginning_of_workday(time) return 0 if holiday?(time) schedule.seconds_since_beginning_of_day(time) end |
#seconds_until_end_of_workday(time) ⇒ Object
21 22 23 24 |
# File 'lib/operating_hours/calculator.rb', line 21 def seconds_until_end_of_workday(time) return 0 if holiday?(time) schedule.seconds_until_end_of_day(time) end |