Module: Timet::TimeUpdateHelper

Defined in:
lib/timet/time_update_helper.rb

Overview

Helper methods for processing and updating time fields.

Instance Method Summary collapse

Instance Method Details

This method returns an undefined value.

Prints an error message for an invalid date.

Examples:

Print an error message for an invalid date

print_error('Invalid date: 2023-13-32')

Parameters:

  • message (String)

    The error message to be printed.



46
47
48
# File 'lib/timet/time_update_helper.rb', line 46

def print_error(message)
  puts "Invalid date: #{message}".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.

Parameters:

  • item (Array)

    The tracking item to be updated.

  • field (String)

    The time field to be updated.

  • date_value (String)

    The new value for the time field.

  • id (Integer)

    The ID of the 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.

Examples:

Update the ‘start’ field of a tracking item with a formatted date value

update_time_field(item, 'start', '11:10:00')

Parameters:

  • item (Array)

    The tracking item to be updated.

  • field (String)

    The time field to be updated.

  • new_time (String)

    The new time value.

Returns:

  • (Time)

    The updated time 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]
  timestamp = item[field_index]
  edit_time = Time.at(timestamp || 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).

Examples:

Validate a new ‘start’ time value

valid_time_value?(item, 'start', 1633072800, 1)

Parameters:

  • item (Array)

    The tracking item to be validated.

  • field (String)

    The time field to be validated.

  • new_value_epoch (Integer)

    The new time value in epoch format.

  • id (Integer)

    The ID of the tracking item.

Returns:

  • (Boolean)

    Returns true if the new time value is valid, otherwise false.



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