Class: KhoinguyenEhTodoList::Services::ToggleCompletionService

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

Overview

Service object to toggle the completion status of a task

Class Method Summary collapse

Class Method Details

.apply(tasks, memento) ⇒ Object

Re-applies the toggle action



33
34
35
36
# File 'lib/khoinguyen_eh_todo_list/services/toggle_completion_service.rb', line 33

def self.apply(tasks, memento)
  task = tasks[memento.data[:index]]
  task.toggle_completion
end

.call(tasks, index) ⇒ Object

rubocop:disable Metrics/MethodLength



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/khoinguyen_eh_todo_list/services/toggle_completion_service.rb', line 7

def self.call(tasks, index) # rubocop:disable Metrics/MethodLength
  index = index.to_i - 1
  task = tasks[index]
  raise "Task not found" unless task

  # Toggle the task completion state
  task.toggle_completion

  message = task.completed? ? "Task completed." : "Task marked as incomplete."

  {
    success: true,
    message: message,
    memento: Memento.new(:toggle, { index: index })
  }
rescue StandardError => e
  { success: false, error: e.message }
end

.revert(tasks, memento) ⇒ Object

Reverts the toggle action



27
28
29
30
# File 'lib/khoinguyen_eh_todo_list/services/toggle_completion_service.rb', line 27

def self.revert(tasks, memento)
  task = tasks[memento.data[:index]]
  task.toggle_completion
end