Class: Taskmeister::TaskList

Inherits:
Object
  • Object
show all
Defined in:
lib/taskmeister/task_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tasks, file_path) ⇒ TaskList

Returns a new instance of TaskList.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/taskmeister/task_list.rb', line 7

def initialize(tasks, file_path)
  @file_path = Pathname.new file_path

  @shortIdToTask = {}

  tasks.each do |t|
    add(t)
  end

  @dirty = false
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



5
6
7
# File 'lib/taskmeister/task_list.rb', line 5

def file_path
  @file_path
end

Instance Method Details

#[](key) ⇒ Object



31
32
33
# File 'lib/taskmeister/task_list.rb', line 31

def [](key)
  @shortIdToTask[key]
end

#add(task) ⇒ Object



43
44
45
46
47
# File 'lib/taskmeister/task_list.rb', line 43

def add(task)
  prefix = assign_short_code_to_task(task)
  @shortIdToTask[prefix] = task
  @dirty = true
end

#complete(short_id) ⇒ Object



49
50
51
52
# File 'lib/taskmeister/task_list.rb', line 49

def complete(short_id)
  removed_val = @shortIdToTask.delete(short_id)
  @dirty = true if removed_val
end

#dirty?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/taskmeister/task_list.rb', line 39

def dirty?
  @dirty
end

#empty?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/taskmeister/task_list.rb', line 35

def empty?
  @shortIdToTask.empty?
end

#markdown_for(short_id) ⇒ Object



62
63
64
65
66
# File 'lib/taskmeister/task_list.rb', line 62

def markdown_for(short_id)
  return [] unless @shortIdToTask.has_key?(short_id)

  self[short_id].to_markdown
end

#replace(short_id, new_text) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/taskmeister/task_list.rb', line 54

def replace(short_id, new_text)
  task = self[short_id]
  return unless task

  @shortIdToTask[short_id] = Task.new(new_text, task.id, task.notes)
  @dirty = true
end

#tasksObject



27
28
29
# File 'lib/taskmeister/task_list.rb', line 27

def tasks
  @shortIdToTask.values
end

#to_short_listObject



19
20
21
22
23
24
25
# File 'lib/taskmeister/task_list.rb', line 19

def to_short_list
  longest_id = @shortIdToTask.keys.max_by(&:length)
  @shortIdToTask.map { |id, task|
    marker = task.notes? ? " »" : ""
    "%-#{longest_id.length}s - %s%s" % [id, task.text, marker]
  }
end