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.



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

def initialize(tasks, file_path)
  @file_path = file_path

  @hash = {}

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

  @dirty = false
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



3
4
5
# File 'lib/taskmeister/task_list.rb', line 3

def file_path
  @file_path
end

Instance Method Details

#[](key) ⇒ Object



29
30
31
# File 'lib/taskmeister/task_list.rb', line 29

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

#add(task) ⇒ Object



41
42
43
44
45
# File 'lib/taskmeister/task_list.rb', line 41

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

#complete(short_id) ⇒ Object



47
48
49
50
# File 'lib/taskmeister/task_list.rb', line 47

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

#dirty?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/taskmeister/task_list.rb', line 37

def dirty?
  @dirty
end

#empty?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/taskmeister/task_list.rb', line 33

def empty?
  @hash.empty?
end

#markdown_for(short_id) ⇒ Object



60
61
62
63
64
# File 'lib/taskmeister/task_list.rb', line 60

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

  self[short_id].to_markdown
end

#replace(short_id, new_text) ⇒ Object



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

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

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

#tasksObject



25
26
27
# File 'lib/taskmeister/task_list.rb', line 25

def tasks
  @hash.values
end

#to_short_listObject



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

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