Class: Dooby::List

Inherits:
Object
  • Object
show all
Defined in:
lib/dooby/list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location) ⇒ List

Returns a new instance of List.



7
8
9
10
11
# File 'lib/dooby/list.rb', line 7

def initialize(location)
  @location = location
  @tasks = {}
  load!
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



5
6
7
# File 'lib/dooby/list.rb', line 5

def location
  @location
end

Instance Method Details

#add(task = Task.new) {|task| ... } ⇒ Object

Yields:

  • (task)


18
19
20
21
22
# File 'lib/dooby/list.rb', line 18

def add(task = Task.new)
  yield task if block_given?
  @tasks[task.id] = task
  save!
end

#delete!(task_id) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/dooby/list.rb', line 24

def delete!(task_id)
  if @tasks.delete task_id
    save!
  else
    false
  end
end

#edit!(task_id, new_text) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/dooby/list.rb', line 32

def edit!(task_id, new_text)
  old_task = @tasks[task_id]
  delete! task_id
  add do |t|
    t.todo = new_text
    t.priority = old_task.priority
    t.status = old_task.status
  end
end

#flush!Object



13
14
15
16
# File 'lib/dooby/list.rb', line 13

def flush!
  @tasks = {}
  save!
end

#list(what_to_show = []) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dooby/list.rb', line 46

def list (what_to_show=[])
  list = []
  
  if @tasks.empty?
    list = 'No tasks'
  else
    if what_to_show.empty?
      list << 'Showing all items...'.blue_on_white.bold
      @tasks.each do |id, task|
        list << " (#{id.red})  #{task.colorize}"
      end
    else
      @tasks.each do |id, task|
        if what_to_show.all? { |term| task.todo.include? term }
          list << " (#{id.red})  #{task.colorize}"
        end
      end
      
      if list.empty?
        list.unshift 'No items found containing:'.red_on_white.bold + ' ' + what_to_show.join(' and '.blue)
      else
        list.unshift 'Showing items containing:'.blue_on_white.bold + ' ' + what_to_show.join(' and '.blue)
      end
    end
  end
        
  list
end

#tasksObject



42
43
44
# File 'lib/dooby/list.rb', line 42

def tasks
  @tasks.dup
end