class TodoLists::CLI

Attributes

items_controller[R]
lists_controller[R]

Public Class Methods

new() click to toggle source
# File lib/todo_lists/controllers/cli.rb, line 4
def initialize
  @lists_controller = TodoLists::ListsController.new
  @items_controller = TodoLists::ItemsController.new
end

Public Instance Methods

call() click to toggle source
# 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_menu() click to toggle source
# 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_menu() click to toggle source
# 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