Class: Ralph::Storage::TasksCollection

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

Overview

Collection of tasks with enumerable interface

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tasks = []) ⇒ TasksCollection

Returns a new instance of TasksCollection.



138
139
140
# File 'lib/ralph/storage/tasks.rb', line 138

def initialize(tasks = [])
  @tasks = tasks
end

Class Method Details

.parse(content) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/ralph/storage/tasks.rb', line 165

def self.parse(content)
  tasks = []
  current_task = nil

  content.each_line do |line|
    # Top-level task: starts with "- [" at beginning (no leading whitespace)
    if (match = line.match(/^- \[([ x\/])\]\s*(.+)/))
      tasks << current_task if current_task
      status_char = match[1]
      text = match[2]
      status = case status_char
               when "x" then :complete
               when "/" then :in_progress
               else :todo
               end
      current_task = Task.new(text: text, status: status, subtasks: [], original_line: line.chomp)
      next
    end

    # Subtask: starts with whitespace followed by "- ["
    if (match = line.match(/^\s+- \[([ x\/])\]\s*(.+)/)) && current_task
      status_char = match[1]
      text = match[2]
      status = case status_char
               when "x" then :complete
               when "/" then :in_progress
               else :todo
               end
      current_task.subtasks << Task.new(text: text, status: status, subtasks: [], original_line: line.chomp)
    end
  end

  tasks << current_task if current_task
  new(tasks)
end

.status_icon(status) ⇒ Object



235
236
237
238
239
240
241
# File 'lib/ralph/storage/tasks.rb', line 235

def self.status_icon(status)
  case status
  when :complete    then "βœ…"
  when :in_progress then "πŸ”„"
  else "⏸️"
  end
end

Instance Method Details

#add(task) ⇒ Object

Add a task to the collection



150
151
152
153
# File 'lib/ralph/storage/tasks.rb', line 150

def add(task)
  @tasks << task
  self
end

#all_complete?Boolean

Returns:

  • (Boolean)


223
224
225
# File 'lib/ralph/storage/tasks.rb', line 223

def all_complete?
  !empty? && all? { |t| t.status == :complete }
end

#any?Boolean

Returns:

  • (Boolean)


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

def any?   = @tasks.any?

#count(&block) ⇒ Object



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

def count(&block) = @tasks.count(&block)

#currentObject



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

def current = find { |t| t.status == :in_progress }

#display_with_indicesObject

Display tasks with numbering for CLI



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/ralph/storage/tasks.rb', line 202

def display_with_indices
  if empty?
    puts "No tasks found."
    return
  end

  puts "Current tasks:"
  each_with_index do |task, i|
    icon = status_icon(task.status)
    puts "#{i + 1}. #{icon} #{task.text}"

    task.subtasks.each do |subtask|
      sub_icon = status_icon(subtask.status)
      puts "   #{sub_icon} #{subtask.text}"
    end
  end
end

#each(&block) ⇒ Object



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

def each(&block)  = @tasks.each(&block)

#empty?Boolean

Returns:

  • (Boolean)


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

def empty? = @tasks.empty?

#lengthObject



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

def length = @tasks.length

#nextObject



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

def next = find { |t| t.status == :todo }

#remove_at(index) ⇒ Object

Remove a task (and its subtasks) by 1-based index. Returns the removed Task, or raises IndexError if out of range.



157
158
159
160
161
162
163
# File 'lib/ralph/storage/tasks.rb', line 157

def remove_at(index)
  if index < 1 || index > @tasks.length
    raise IndexError, "Task index #{index} is out of range (1-#{@tasks.length})"
  end

  @tasks.delete_at(index - 1)
end

#status_icon(status) ⇒ Object



227
228
229
230
231
232
233
# File 'lib/ralph/storage/tasks.rb', line 227

def status_icon(status)
  case status
  when :complete    then "βœ…"
  when :in_progress then "πŸ”„"
  else "⏸️"
  end
end