Class: Ecom::Core::CrewTime
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Ecom::Core::CrewTime
- Defined in:
- app/models/ecom/core/crew_time.rb
Constant Summary collapse
- MORNING =
Time Ranges
'Morning'.freeze
- AFTERNOON =
'Afternoon'.freeze
- BOTH =
'Both'.freeze
- TIME_RANGE =
{ MORNING => {start: Time.zone.parse('5:00 AM'), end: Time.zone.parse('9:00 AM')}, AFTERNOON => {start: Time.zone.parse('10:00 AM'), end: Time.zone.parse('2:00 PM')}, BOTH => {start: Time.zone.parse('5:00 AM'), end: Time.zone.parse('2:00 PM')} }.freeze
Instance Method Summary collapse
- #calculate_hours ⇒ Object
-
#compute_hours ⇒ Object
A method to adjust time ranges by trimming any time value outside of the defined morning and afternoon ranges.
-
#find_range(start, finish) ⇒ Object
A method to check if checkin and checkout range falls in the morning, afternoon, or both.
Instance Method Details
#calculate_hours ⇒ Object
24 25 26 |
# File 'app/models/ecom/core/crew_time.rb', line 24 def calculate_hours self.hours = checkout_time.nil? || checkin_time.nil? ? 0 : compute_hours end |
#compute_hours ⇒ Object
A method to adjust time ranges by trimming any time value outside of the defined morning and afternoon ranges
42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/models/ecom/core/crew_time.rb', line 42 def compute_hours # Reparse time to avoid errors caused by date differences start = Time.zone.parse(checkin_time.strftime('%I:%M%p')) finish = Time.zone.parse(checkout_time.strftime('%I:%M%p')) range = find_range(start, finish) left = start.before?(TIME_RANGE[range][:start]) ? TIME_RANGE[range][:start] : start right = finish.after?(TIME_RANGE[range][:end]) ? TIME_RANGE[range][:end] : finish time = (right - left) / 1.hour time -= 1 if range == BOTH time end |
#find_range(start, finish) ⇒ Object
A method to check if checkin and checkout range falls in the morning, afternoon, or both.
30 31 32 33 34 35 36 37 38 |
# File 'app/models/ecom/core/crew_time.rb', line 30 def find_range(start, finish) if start.before?(TIME_RANGE[MORNING][:end]) && finish.before?(TIME_RANGE[AFTERNOON][:start]) MORNING elsif start.after?(TIME_RANGE[MORNING][:end]) && finish.after?(TIME_RANGE[AFTERNOON][:start]) AFTERNOON else BOTH end end |