Class: KhoinguyenEhTodoList::Services::MoveTaskService

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

Overview

Service object to move a task in the list

Class Method Summary collapse

Class Method Details

.apply(tasks, memento) ⇒ void

This method returns an undefined value.

Re-applies the move action

Parameters:

  • tasks (Array<Task>) —

    the list of tasks

  • memento (Memento) —

    the memento to apply



36
37
38
39
# File 'lib/khoinguyen_eh_todo_list/services/move_task_service.rb', line 36

def self.apply(tasks, memento)
  task = tasks.delete_at(memento.data[:from])
  tasks.insert(memento.data[:to], task)
end

.call(tasks, from, to) ⇒ Object



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

def self.call(tasks, from, to)
  from_idx = from.to_i - 1
  to_idx = to.to_i - 1

  if from_idx.negative? || to_idx.negative? || from_idx >= tasks.size || to_idx >= tasks.size
    raise "Invalid indices"
  end

  task = tasks.delete_at(from_idx)
  tasks.insert(to_idx, task)

  { success: true, memento: Memento.new(:move, { from: from_idx, to: to_idx }) }
rescue StandardError => e
  { success: false, error: e.message }
end

.revert(tasks, memento) ⇒ void

This method returns an undefined value.

Reverts the move action

Parameters:

  • tasks (Array<Task>) —

    the list of tasks

  • memento (Memento) —

    the memento to revert



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

def self.revert(tasks, memento)
  task = tasks.delete_at(memento.data[:to])
  tasks.insert(memento.data[:from], task)
end