Class: Ralph::Storage::Tasks

Inherits:
Object
  • Object
show all
Defined in:
lib/ralph/storage/tasks.rb

Overview

Manages task tracking and workflow coordination.

Always instantiate with Tasks.new — it represents the tasks file on disk. Follows the same instance-based pattern as Context and History.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTasks

Returns a new instance of Tasks.



12
13
14
# File 'lib/ralph/storage/tasks.rb', line 12

def initialize
  FileUtils.mkdir_p(dir)
end

Class Method Details

.dirObject



16
17
18
# File 'lib/ralph/storage/tasks.rb', line 16

def self.dir
  File.join(Dir.pwd, ".ralph")
end

.pathObject



22
23
24
# File 'lib/ralph/storage/tasks.rb', line 22

def self.path
  File.join(dir, "ralph-tasks.md")
end

Instance Method Details

#add_task(description) ⇒ Object

Add a new task by description string. Creates the tasks file if it doesn’t exist.



63
64
65
66
67
68
69
# File 'lib/ralph/storage/tasks.rb', line 63

def add_task(description)
  tasks = load_tasks || TasksCollection.new
  task = Task.new(text: description, status: :todo)
  tasks.add(task)
  save_tasks(tasks)
  task
end

#clear_tasksObject



49
50
51
52
53
# File 'lib/ralph/storage/tasks.rb', line 49

def clear_tasks
  File.delete(path) if File.exist?(path)
rescue StandardError
  # ignore
end

#dirObject



20
# File 'lib/ralph/storage/tasks.rb', line 20

def dir = self.class.dir

#initialize_tasks_fileObject

— Task Initialization —



85
86
87
88
89
# File 'lib/ralph/storage/tasks.rb', line 85

def initialize_tasks_file
  content = "# Ralph Tasks\n\nAdd your tasks below using: `ralph --add-task \"description\"`\n"
  File.write(path, content)
  path
end

#load_tasksObject

— Tasks Management —



29
30
31
32
33
34
35
36
# File 'lib/ralph/storage/tasks.rb', line 29

def load_tasks
  if File.exist?(path)
    content = File.read(path)
    TasksCollection.parse(content)
  end
rescue StandardError
  nil
end

#pathObject



26
# File 'lib/ralph/storage/tasks.rb', line 26

def path = self.class.path

#remove_task(index) ⇒ Object

Remove a task by 1-based index. Returns the removed Task. Raises IndexError if index is out of range. Raises RuntimeError if no tasks file exists.



75
76
77
78
79
80
81
82
# File 'lib/ralph/storage/tasks.rb', line 75

def remove_task(index)
  raise "No tasks file found" unless tasks_exist?

  tasks = load_tasks
  removed = tasks.remove_at(index)
  save_tasks(tasks)
  removed
end

#save_tasks(tasks) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/ralph/storage/tasks.rb', line 38

def save_tasks(tasks)
  content = "# Ralph Tasks\n\n"
  tasks.each do |task|
    content << task.to_s << "\n"
    task.subtasks.each do |subtask|
      content << "  #{subtask}\n"
    end
  end
  File.write(path, content)
end

#tasks_exist?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/ralph/storage/tasks.rb', line 55

def tasks_exist?
  File.exist?(path)
end