Module: Main

Defined in:
lib/main.rb

Overview

This module decides which flow to initiate depending on the first argument (add, list, etc)

Class Method Summary collapse

Class Method Details

.handle_add(description) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/main.rb', line 33

def handle_add(description)
  if description.empty?
    WarningHelper.print_add_description_is_required
  else
    todos_controller.add_todo description.join(' ')
  end
end

.handle_delete(index) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/main.rb', line 59

def handle_delete(index)
  return WarningHelper.print_task_index_is_required if index.nil?
  if index.to_i <= 0
    return WarningHelper.print_task_index_not_positive_integer
  end

  todos_controller.delete_todo index.to_i
end

.handle_done(index) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/main.rb', line 50

def handle_done(index)
  return WarningHelper.print_task_index_is_required if index.nil?
  if index.to_i <= 0
    return WarningHelper.print_task_index_not_positive_integer
  end

  todos_controller.complete_todo index.to_i
end

.handle_list(flag) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/main.rb', line 41

def handle_list(flag)
  case flag
  when nil then todos_controller.list_pending_todos
  when '--all' then todos_controller.list_all_todos
  else
    WarningHelper.print_flag_not_recognized(flag)
  end
end

.process_argv(arguments) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/main.rb', line 14

def process_argv(arguments)
  method, *parameters = arguments

  case method
  when 'add' then handle_add parameters
  when 'list' then handle_list parameters[0]
  when 'done' then handle_done parameters[0]
  when 'delete' then handle_delete parameters[0]
  when nil then WarningHelper.print_method_is_required
  else
    # TODO: create --help, -h option/flag
    WarningHelper.print_command_not_recognized_or_missing method
  end
end

.todos_controllerObject



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

def todos_controller
  TodosController.new
end