Module: Timet::ValidationEditHelper
- Includes:
- TimeValidationHelper
- Included in:
- Application
- Defined in:
- lib/timet/validation_edit_helper.rb
Overview
Validates and updates a specific field of an item based on certain conditions. If the field is ‘start’ or ‘end’, it checks and updates the value accordingly. Otherwise, it directly updates the field with the new value.
Constant Summary collapse
- TIME_FIELDS =
Constants for time fields.
%w[start end].freeze
Instance Method Summary collapse
-
#check_collision_with_next_item(field, new_epoch, next_item) ⇒ Object
Checks for collision with the next item.
-
#check_collision_with_previous_item(field, new_epoch, prev_item) ⇒ Object
Checks for collision with the previous item.
-
#validate_and_update(item, field, new_value) ⇒ Array
Validates and updates an item’s attribute based on the provided field and new value.
-
#validate_time(item, field, time_str) ⇒ Integer?
Validates if a given time string is in a valid format.
-
#validate_time_collisions(item, field, new_epoch) ⇒ Object
Validates that the new start or end time does not collide with existing entries.
Methods included from TimeValidationHelper
#adjust_end_datetime, #create_new_datetime, #determine_base_date_time, #parse_time_string, #validate_end_time, #validate_future_date, #validate_start_time
Instance Method Details
#check_collision_with_next_item(field, new_epoch, next_item) ⇒ Object
Checks for collision with the next item.
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/timet/validation_edit_helper.rb', line 106 def check_collision_with_next_item(field, new_epoch, next_item) return unless next_item if field == 'start' && new_epoch > next_item[1] raise ArgumentError, 'New start time collides with next item (starts at ' \ "#{Time.at(next_item[1]).strftime('%Y-%m-%d %H:%M:%S')})." elsif field == 'end' && new_epoch > next_item[1] raise ArgumentError, 'New end time collides with next item (starts at ' \ "#{Time.at(next_item[1]).strftime('%Y-%m-%d %H:%M:%S')})." end end |
#check_collision_with_previous_item(field, new_epoch, prev_item) ⇒ Object
Checks for collision with the previous item.
97 98 99 100 101 102 103 |
# File 'lib/timet/validation_edit_helper.rb', line 97 def check_collision_with_previous_item(field, new_epoch, prev_item) return unless prev_item && field == 'start' && new_epoch < prev_item[2] raise ArgumentError, 'New start time collides with previous item (ends at ' \ "#{Time.at(prev_item[2]).strftime('%Y-%m-%d %H:%M:%S')})." end |
#validate_and_update(item, field, new_value) ⇒ Array
Validates and updates an item’s attribute based on the provided field and new value.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/timet/validation_edit_helper.rb', line 27 def validate_and_update(item, field, new_value) case field when 'notes' item[4] = new_value when 'tag' item[3] = new_value when 'start' item[1] = validate_time(item, 'start', new_value) when 'end' item[2] = validate_time(item, 'end', new_value) else raise ArgumentError, "Invalid field: #{field}" end item end |
#validate_time(item, field, time_str) ⇒ Integer?
Validates if a given time string is in a valid format.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/timet/validation_edit_helper.rb', line 55 def validate_time(item, field, time_str) # If time_str is nil or empty, user pressed Enter, meaning no change to this field. return field == 'start' ? item[1] : item[2] if time_str.nil? || time_str.strip.empty? parsed_time_component = parse_time_string(time_str) = item[1] = item[2] new_datetime = determine_and_create_datetime(item, field, , parsed_time_component) new_epoch = new_datetime.to_i perform_validation( item: item, field: field, new_epoch: new_epoch, start_timestamp: , end_timestamp: , new_datetime: new_datetime ) new_epoch end |
#validate_time_collisions(item, field, new_epoch) ⇒ Object
Validates that the new start or end time does not collide with existing entries.
87 88 89 90 91 92 93 94 |
# File 'lib/timet/validation_edit_helper.rb', line 87 def validate_time_collisions(item, field, new_epoch) item_id = item[0] prev_item = @db.find_item(item_id - 1) next_item = @db.find_item(item_id + 1) check_collision_with_previous_item(field, new_epoch, prev_item) check_collision_with_next_item(field, new_epoch, next_item) end |