Class: DoStuff::Tasklist

Inherits:
Object
  • Object
show all
Defined in:
lib/do_stuff/tasklist.rb

Defined Under Namespace

Classes: ParseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Tasklist

Returns a new instance of Tasklist.



5
6
7
8
9
# File 'lib/do_stuff/tasklist.rb', line 5

def initialize(file)
  @file = file
  @tasks = {}
  parse
end

Instance Attribute Details

#tasksObject (readonly)

Returns the value of attribute tasks.



3
4
5
# File 'lib/do_stuff/tasklist.rb', line 3

def tasks
  @tasks
end

Instance Method Details

#[](task_num) ⇒ Object



21
22
23
# File 'lib/do_stuff/tasklist.rb', line 21

def [](task_num)
  @tasks[task_num.to_i]
end

#[]=(task_num, task) ⇒ Object



25
26
27
# File 'lib/do_stuff/tasklist.rb', line 25

def []=(task_num, task)
  @tasks[task_num.to_i] = task
end

#add(task) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/do_stuff/tasklist.rb', line 11

def add(task)
  # Find the first unused task number in the list.
  task_num = 1
  task_num += 1 while @tasks.key?(task_num)

  @tasks[task_num] = task

  task_num
end

#delete(task_num) ⇒ Object



29
30
31
# File 'lib/do_stuff/tasklist.rb', line 29

def delete(task_num)
  @tasks.delete(task_num.to_i)
end

#renumber!Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/do_stuff/tasklist.rb', line 33

def renumber!
  old_tasks = @tasks
  @tasks = {}

  i = 1
  old_tasks.sort.each do |_, task|
    @tasks[i] = task
    i += 1
  end

  @tasks
end

#write!Object



46
47
48
49
50
# File 'lib/do_stuff/tasklist.rb', line 46

def write!
  File.open(@file, 'w') do |f|
    tasks.sort.each{|num, task| f.puts("#{num}. #{task}") }
  end
end