Class: Rralph::FileUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/rralph/file_updater.rb

Instance Method Summary collapse

Constructor Details

#initialize(todo_path: 'todo.md', learnings_path: 'learnings.md') ⇒ FileUpdater

Returns a new instance of FileUpdater.



3
4
5
6
# File 'lib/rralph/file_updater.rb', line 3

def initialize(todo_path: 'todo.md', learnings_path: 'learnings.md')
  @todo_path = todo_path
  @learnings_path = learnings_path
end

Instance Method Details

#append_learnings(new_learnings) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rralph/file_updater.rb', line 20

def append_learnings(new_learnings)
  return if new_learnings.empty?

  existing_content = File.exist?(@learnings_path) ? File.read(@learnings_path) : ''
  existing_learnings = existing_content.lines.map(&:strip).reject(&:empty?)

  unique_learnings = new_learnings.reject do |learning|
    existing_learnings.any? { |existing| existing.include?(learning) || learning.include?(existing) }
  end

  return if unique_learnings.empty?

  timestamp = Time.now.iso8601
  new_section = "\n\n## Learnings - #{timestamp}\n\n"
  unique_learnings.each do |learning|
    new_section += "- #{learning}\n"
  end

  File.write(@learnings_path, existing_content.rstrip + new_section + "\n")
end

#mark_task_completed(task_index) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rralph/file_updater.rb', line 8

def mark_task_completed(task_index)
  content = File.read(@todo_path)
  lines = content.lines

  line = lines[task_index]
  return unless line

  updated_line = line.gsub(/^([-*]) \[ \]/, '\1 [x]')
  lines[task_index] = updated_line
  File.write(@todo_path, lines.join)
end

#todo_empty?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
# File 'lib/rralph/file_updater.rb', line 41

def todo_empty?
  return true unless File.exist?(@todo_path)

  content = File.read(@todo_path)
  content.lines.none? { |line| line.match?(/^[-*] \[ \]/i) }
end