Class: KhoinguyenEhTodoList::Services::UndoRedoService

Inherits:
Object
  • Object
show all
Defined in:
lib/khoinguyen_eh_todo_list/services/undo_redo_service.rb

Overview

Service object to handle undo and redo operations

Class Method Summary collapse

Class Method Details

.call(tasks, caretaker, action) ⇒ Hash

Handles undo or redo operation based on the action passed

Parameters:

  • tasks (Array<Task>)
  • caretaker (Caretaker)
  • action (Symbol) —
    • :undo or :redo

Returns:

  • (Hash)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/khoinguyen_eh_todo_list/services/undo_redo_service.rb', line 12

def self.call(tasks, caretaker, action) # rubocop:disable Metrics/MethodLength
  memento = case action
            when :undo
              caretaker.undo
            when :redo
              caretaker.redo
            else
              return { success: false, message: "Invalid action. Use :undo or :redo." }
            end

  return { success: true, message: "Nothing to #{action}." } unless memento && memento.action != :blank

  service = determine_service(memento.action)
  if service
    action == :undo ? service.revert(tasks, memento) : service.apply(tasks, memento)
    { success: true, message: "#{action.capitalize} successful." }
  else
    { success: false, message: "Action not supported." }
  end
end

.determine_service(action) ⇒ Class?

Determines the appropriate service for the given action

Parameters:

  • action (Symbol)

Returns:

  • (Class, nil)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/khoinguyen_eh_todo_list/services/undo_redo_service.rb', line 36

def self.determine_service(action) # rubocop:disable Metrics/MethodLength
  case action
  when :add
    KhoinguyenEhTodoList::Services::AddTaskService
  when :remove
    KhoinguyenEhTodoList::Services::RemoveTaskService
  when :move
    KhoinguyenEhTodoList::Services::MoveTaskService
  when :toggle
    KhoinguyenEhTodoList::Services::ToggleCompletionService
  when :clear
    KhoinguyenEhTodoList::Services::ClearTasksService
  end
end