Class: TodoLists::CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



4
5
6
7
# File 'lib/todo_lists/controllers/cli.rb', line 4

def initialize
  @lists_controller = TodoLists::ListsController.new
  @items_controller = TodoLists::ItemsController.new
end

Instance Attribute Details

#items_controllerObject (readonly)

Returns the value of attribute items_controller.



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

def items_controller
  @items_controller
end

#lists_controllerObject (readonly)

Returns the value of attribute lists_controller.



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

def lists_controller
  @lists_controller
end

Instance Method Details

#callObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/todo_lists/controllers/cli.rb', line 10

def call

  if TodoLists::List.count == 0
    lists_controller.index
    lists_controller.new
    @list = TodoLists::List.last
    if @list
      items_menu
      lists_menu
    end
  else
    lists_menu
  end

end

#items_menuObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/todo_lists/controllers/cli.rb', line 72

def items_menu
  items_controller.list = @list

  until items_controller.last_input == '/exit' || items_controller.last_input == '/main'
    if @help_shown
      @help_shown = false
    else
      items_controller.index
    end

    puts "\nEnter new item or \'/help\' for more options or type /main"

    items_controller.get_input
    last_input = items_controller.last_input.downcase

    if last_input.match?(/\/done\s\d+/)
      items_controller.done

    elsif last_input.match?(/\/edit\s\d+/)
      items_controller.edit

    elsif last_input.match?(/\/delete\s\d+/)
      items_controller.delete

    elsif last_input == '/help'
      items_controller.help
      @help_shown = true

    elsif last_input != '/exit' and last_input != '/main'
      items_controller.new
    end

  end

  case items_controller.last_input.downcase
  when '/exit'
    lists_controller.last_input = '/exit' # exit from lists_menu as well
  end

  items_controller.last_input = nil # reset last_input
end

#lists_menuObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
# File 'lib/todo_lists/controllers/cli.rb', line 26

def lists_menu

  until lists_controller.last_input == '/exit'

    if @help_shown
      @help_shown = false
    else
      lists_controller.index
    end

    puts "\nEnter list number or \'/help\' for more options or type /exit"

    lists_controller.get_input

    last_input = lists_controller.last_input.downcase

    if last_input.to_i > 0
      @list = TodoLists::List.find_by_index(last_input)
      items_menu if @list

    elsif last_input == '/new'
      last_before_new = TodoLists::List.last

      lists_controller.new

      last_after_new = TodoLists::List.last

      if last_before_new != last_after_new # find out if a list was created in new action or not
        @list = last_after_new
        items_menu
      end

    elsif last_input.match?(/\/edit\s\d+/)
      lists_controller.edit

    elsif last_input.match?(/\/delete\s\d+/)
      lists_controller.delete

    elsif last_input == '/help'
      lists_controller.help
      @help_shown = true
    end

  end
end