Class: MMToDo::ToDoApp

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/mm_todo/todo_app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



8
9
10
# File 'lib/mm_todo/todo_app.rb', line 8

def file
  @file
end

Instance Method Details

#add_itemObject



53
54
55
56
57
58
59
# File 'lib/mm_todo/todo_app.rb', line 53

def add_item
  @input.clear
  puts "Description (leave blank to cancel):"
  desc = $stdin.gets.chomp
  @list.add desc
  @error = "(Added: #{desc})"
end

#app_loopObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mm_todo/todo_app.rb', line 28

def app_loop
  while @status != :quit
    case @input
    when "q","Q"
      @status = :quit
    when "a", "A"
      add_item
    when "d", "D"
      mark_done
    when "p", "P"
      purge_done
    else 
      main_menu
    end
  end
end

#check_params(params, dest) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/mm_todo/todo_app.rb', line 20

def check_params(params, dest)
  unless params.size == 1
    puts "Wrong number of arguments."
    puts "Usage: ruby todo.rb list_name\n"
    exit
  end
end

#check_selection(selection) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mm_todo/todo_app.rb', line 68

def check_selection(selection)
  if selection !~ /^\d+$/
    @error = "[Invalid selection] Just the number, please."
    return
  elsif selection.to_i == 0 || selection.to_i > @list.size
    @error = "[Invalid selection] No todo with that number."
    return
  end
  
  index = selection.to_i
  @error = "(Item completed: #{@list.select(index)})"
  @list.select(index).do
end


45
46
47
48
49
50
51
# File 'lib/mm_todo/todo_app.rb', line 45

def main_menu
  @list.show_all
  puts @error
  @error = nil
  puts "[Commands: q=quit, a=add, d=mark done, p=purge done items]"
  @input = $stdin.gets.chomp
end

#mark_doneObject



61
62
63
64
65
66
# File 'lib/mm_todo/todo_app.rb', line 61

def mark_done
  @input.clear
  puts "Which number?"
  selection = $stdin.gets.chomp
  check_selection(selection)
end

#purge_doneObject



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mm_todo/todo_app.rb', line 82

def purge_done
  @input.clear
  puts "Are you sure?(y/n)"
  answer = $stdin.gets.chomp
  case answer
  when "y", "Y"
    @list.purge_done
    @error = "(Done items purged)"
  else
    @error = "(purge cancelled)"
  end
end

#start(params = ARGV, dest = STDOUT) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/mm_todo/todo_app.rb', line 10

def start (params=ARGV, dest = STDOUT)
  check_params(params,dest)
  @file = params.shift
  @list = ToDoList.new @file
  #@list.load if File.exist? File.join(File.dirname(__FILE__), '\..\bin\\' + @file + '.save')

  @list.load if File.exist? File.join(File.expand_path("../../bin/", File.dirname(__FILE__)), @file + '.save')
  @status = :menu
  app_loop()
end