Class: KhoinguyenEhTodoList::Services::AddTaskService

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

Overview

Service object to add a task to the list

Class Method Summary collapse

Class Method Details

.apply(tasks, memento) ⇒ Object

Re-applies the addition of the task



25
26
27
# File 'lib/khoinguyen_eh_todo_list/services/add_task_service.rb', line 25

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

.call(tasks, title) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/khoinguyen_eh_todo_list/services/add_task_service.rb', line 7

def self.call(tasks, title)
  task = Task.new(title)
  tasks << task
  {
    success: true,
    message: "Task added successfully.",
    memento: Memento.new(:add, { index: tasks.size - 1, task: task })
  }
rescue StandardError => e
  { success: false, error: e.message }
end

.revert(tasks, memento) ⇒ Object

Reverts the addition of the task



20
21
22
# File 'lib/khoinguyen_eh_todo_list/services/add_task_service.rb', line 20

def self.revert(tasks, memento)
  tasks.delete_at(memento.data[:index])
end