Module: Timet::TimeUpdateHelper
- Defined in:
- lib/timet/time_update_helper.rb
Overview
Helper methods for processing and updating time fields.
Instance Method Summary collapse
-
#print_error(message) ⇒ void
Prints an error message for an invalid date.
-
#process_and_update_time_field(*args) ⇒ void
Processes and updates a time field (start or end) of a tracking item.
-
#valid_time_value?(*args) ⇒ Boolean
Validates if a new time value is valid for a specific time field (start or end).
Instance Method Details
#print_error(message) ⇒ void
This method returns an undefined value.
Prints an error message for an invalid date.
46 47 48 |
# File 'lib/timet/time_update_helper.rb', line 46 def print_error() puts "Invalid date: #{}".red end |
#process_and_update_time_field(*args) ⇒ void
Note:
The method formats the date value and checks if it is valid.
Note:
If the date value is valid, it updates the time field with the new value.
Note:
If the date value is invalid, it prints an error message.
This method returns an undefined value.
Processes and updates a time field (start or end) of a tracking item.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/timet/time_update_helper.rb', line 22 def process_and_update_time_field(*args) item, field, date_value, id = args formatted_date = TimeHelper.format_time_string(date_value) return print_error(date_value) unless formatted_date new_date = TimeHelper.update_time_field(item, field, formatted_date) new_value_epoch = new_date.to_i if valid_time_value?(item, field, new_value_epoch, id) @db.update_item(id, field, new_value_epoch) else print_error(new_date) end end |
#valid_time_value?(*args) ⇒ Boolean
Validates if a new time value is valid for a specific time field (start or end).
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/timet/time_update_helper.rb', line 61 def valid_time_value?(*args) item, field, new_value_epoch, id = args item_start = ItemDataHelper.fetch_item_start(item) item_end = ItemDataHelper.fetch_item_end(item) item_before_end = ItemDataHelper.fetch_item_before_end(@db, id, item_start) item_after_start = ItemDataHelper.fetch_item_after_start(@db, id) if field == 'start' new_value_epoch.between?(item_before_end, item_end) else new_value_epoch.between?(item_start, item_after_start) end end |