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.
-
#update_time_field(item, field, new_time) ⇒ Time
Updates a time field (start or end) of a tracking item with a formatted date value.
-
#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
The method formats the date value and checks if it is valid.
If the date value is valid, it updates the time field with the new value.
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 = 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 |
#update_time_field(item, field, new_time) ⇒ Time
Updates a time field (start or end) of a tracking item with a formatted date value.
60 61 62 63 64 65 66 |
# File 'lib/timet/time_update_helper.rb', line 60 def update_time_field(item, field, new_time) field_index = Timet::Application::FIELD_INDEX[field] = item[field_index] edit_time = Time.at( || item[1]).to_s.split edit_time[1] = new_time DateTime.strptime(edit_time.join(' '), '%Y-%m-%d %H:%M:%S %z').to_time end |
#valid_time_value?(*args) ⇒ Boolean
Validates if a new time value is valid for a specific time field (start or end).
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/timet/time_update_helper.rb', line 79 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 |