Module: Timet::TimeValidationHelper
- Included in:
- ValidationEditHelper
- Defined in:
- lib/timet/time_validation_helper.rb
Overview
Helper module for time validation logic.
Instance Method Summary collapse
-
#adjust_end_datetime(field, start_timestamp, new_datetime) ⇒ Time
Adjusts the end datetime if it’s earlier than or same as the start time, assuming it’s for the next day.
-
#create_new_datetime(_base_date_time, parsed_time_component) ⇒ Time
Creates a new datetime object based on the parsed time component.
-
#determine_base_date_time(_item, field, start_timestamp) ⇒ Time
Determines the base date and time for creating a new datetime object.
-
#parse_time_string(time_str) ⇒ Time
Parses the time string and raises an ArgumentError if the format is invalid.
-
#validate_end_time(new_epoch, start_timestamp, new_datetime) ⇒ Object
Validates the end time against the start time.
-
#validate_future_date(new_datetime) ⇒ Object
Validates that the new datetime is not in the future.
-
#validate_start_time(new_epoch, end_timestamp, new_datetime) ⇒ Object
Validates the start time against the end time.
Instance Method Details
#adjust_end_datetime(field, start_timestamp, new_datetime) ⇒ Time
Adjusts the end datetime if it’s earlier than or same as the start time, assuming it’s for the next day.
28 29 30 31 32 33 34 35 |
# File 'lib/timet/time_validation_helper.rb', line 28 def adjust_end_datetime(field, , new_datetime) # If setting 'end' time and the parsed new_datetime (based on start_date) # is earlier than or same as start_time, assume it's for the next calendar day. if field == 'end' && && (new_datetime.to_i <= ) new_datetime += (24 * 60 * 60) # Add one day end new_datetime end |
#create_new_datetime(_base_date_time, parsed_time_component) ⇒ Time
Creates a new datetime object based on the parsed time component.
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/timet/time_validation_helper.rb', line 88 def create_new_datetime(_base_date_time, parsed_time_component) Time.new( parsed_time_component.year, parsed_time_component.month, parsed_time_component.day, parsed_time_component.hour, parsed_time_component.min, parsed_time_component.sec, parsed_time_component.utc_offset # Preserve timezone context ) end |
#determine_base_date_time(_item, field, start_timestamp) ⇒ Time
Determines the base date and time for creating a new datetime object.
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/timet/time_validation_helper.rb', line 46 def determine_base_date_time(_item, field, ) case field when 'start' determine_start_base_date_time() when 'end' determine_end_base_date_time() else raise ArgumentError, "Invalid field: #{field}" end end |
#parse_time_string(time_str) ⇒ Time
Parses the time string and raises an ArgumentError if the format is invalid.
15 16 17 18 19 |
# File 'lib/timet/time_validation_helper.rb', line 15 def parse_time_string(time_str) Time.parse(time_str) rescue ArgumentError raise ArgumentError, "Invalid time format: #{time_str}" end |
#validate_end_time(new_epoch, start_timestamp, new_datetime) ⇒ Object
Validates the end time against the start time.
157 158 159 160 |
# File 'lib/timet/time_validation_helper.rb', line 157 def validate_end_time(new_epoch, , new_datetime) validate_time_order(new_epoch, , new_datetime, 'end') validate_time_difference(, new_epoch) end |
#validate_future_date(new_datetime) ⇒ Object
Validates that the new datetime is not in the future.
105 106 107 108 109 |
# File 'lib/timet/time_validation_helper.rb', line 105 def validate_future_date(new_datetime) return unless new_datetime > Time.now raise ArgumentError, "Cannot set time to a future date: #{new_datetime.strftime('%Y-%m-%d %H:%M:%S')}" end |
#validate_start_time(new_epoch, end_timestamp, new_datetime) ⇒ Object
Validates the start time against the end time.
169 170 171 172 |
# File 'lib/timet/time_validation_helper.rb', line 169 def validate_start_time(new_epoch, , new_datetime) validate_time_order(new_epoch, , new_datetime, 'start') validate_time_difference(new_epoch, ) end |