Class: TodoLists::ListsController

Inherits:
Object
  • Object
show all
Defined in:
lib/todo_lists/controllers/lists_controller.rb

Constant Summary collapse

List =
TodoLists::List

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#last_inputObject

Returns the value of attribute last_input.



3
4
5
# File 'lib/todo_lists/controllers/lists_controller.rb', line 3

def last_input
  @last_input
end

Instance Method Details

#deleteObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/todo_lists/controllers/lists_controller.rb', line 26

def delete
  list_index = last_input.split(' ')[1]
  list = List.find_by_index(list_index)
  if list
    print 'Are you sure? Enter \'y/n\': '
    get_input

    if last_input.downcase == 'y'
      list.items.destroy_all
      list.destroy
    end

  end
end

#editObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/todo_lists/controllers/lists_controller.rb', line 15

def edit
  list_index = last_input.split(' ')[1]
  list = List.find_by_index(list_index)
  if list
    print 'Enter new title: '
    get_input
    list.title = last_input
    list.save
  end
end

#get_inputObject



66
67
68
# File 'lib/todo_lists/controllers/lists_controller.rb', line 66

def get_input
  @last_input = gets.strip
end

#helpObject



7
8
9
10
11
12
13
# File 'lib/todo_lists/controllers/lists_controller.rb', line 7

def help
  puts "\nOptions available"
  puts '/new (for a new list)'
  puts '/edit {list_number}'
  puts '/delete {list_number}'
  puts
end

#indexObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/todo_lists/controllers/lists_controller.rb', line 41

def index
  if List.count == 0
    puts 'You have no TodoList, create one or type \'/exit\''
  else
    puts "\n---------------TodoLists---------------"
    puts '#   title'
    puts '--   -----'
    List.all.each.with_index(1) do |list, index|
      puts "#{index}: #{list.title.capitalize}"
    end
  end
end

#newObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/todo_lists/controllers/lists_controller.rb', line 54

def new

  print 'Enter Title: '
  get_input

  return if last_input == '/exit'

  list = List.new(title: last_input)

  list.save
end