class R2do::Commands::CategoryCommand

Constants

DELETE
DISPLAY
EDIT
YES

Public Class Methods

new(state) click to toggle source
Calls superclass method
# File lib/r2do/commands/category_command.rb, line 27
def initialize(state)
  super('c', 'category', 'Creates a new category.')

  @state = state
end

Public Instance Methods

delete_category(args) click to toggle source

Deletes the currently selected category

@param [Array] args the argumets passed to the app by the user @return [void]

# File lib/r2do/commands/category_command.rb, line 103
def delete_category(args)
  UI.status("Are you sure you want to delete the category:")
  UI.status("   #{@state.current_category.name}")
  UI.new_line()
  UI.status("All tasks contained in this category will be lost.")
  value = UI.input("This action cannot be undone. Continue? [Yn]")
  if value == YES
    cat = @state.current_category
    @state.remove(cat)
    @state.clear_current_category()
    @state.modified = true

    UI.status("Category '#{cat.name}' has been deleted.")
  end
end
display_current_category(args) click to toggle source

Shows the detailed information for the current category, including the tasks contained

@param [Array] args the arguments passed to the app by the user @return [void]

# File lib/r2do/commands/category_command.rb, line 89
def display_current_category(args)
  #TODO: need to refatctor the code to remove the duplication (NowCommand)
  if not @state.current_category
    UI.status("No category is currently selected.")
  else
    UI.status(@state.current_category.to_s)
    UI.new_line()
  end
end
edit_current_category(args) click to toggle source

Edit the current task.

@param [Array] args the arguments passed to the app by the user. @return [void]

# File lib/r2do/commands/category_command.rb, line 65
def edit_current_category(args)
  UI.status("Are you sure you want to edit the category:")
  UI.status("   #{@state.current_category.name}")
  UI.new_line()
  value = UI.input("Continue? [Yn]")
  if value == YES
    cat = @state.current_category

    original_name = cat.name
    name = UI.input("Enter new name:")

    cat.rename(name)

    @state.refresh(original_name, cat)
    @state.modified = true

    UI.status("The category as been modified.")
  end
end
execute(args) click to toggle source

Creates a new category or makes a category current in the state if a category with the same name already exists

@param [Array] args the arguments passed to the app by the user @raise [ArgumentError] if the command does not contain a name for the category @return [void]

# File lib/r2do/commands/category_command.rb, line 39
def execute(args)
  if args.length < 2
    raise ArgumentError, "The 'category' command requires additional arguments."
  end

  option = args[1]

  if option.eql?(DISPLAY)
    display_current_category(args)
  elsif option.eql?(EDIT)
    require_selected_category()
    edit_current_category(args)
  elsif option.eql?(DELETE)
    require_selected_category()
    delete_category(args)
  elsif option.start_with?("--")
    raise InvalidOptionError
  else
    parse_category(args)
  end
end
help() click to toggle source
# File lib/r2do/commands/category_command.rb, line 149
      def help()
        help = <<-EOF
NAME
       r2do #{@extended}

SYNOPSIS
       'r2do #{@extended}' or 'r2do #{@short}' are equivalent

DESCRIPTION
      The #{@extended} lets you interact with a category, create, edit, or delete. Defaults to the active category.

      usage: r2do #{@extended} [NAME] [--edit] [--display] [--delete]

      --edit                Edit the currently selected category
      --display             Displays the details for the selected category
      --delete              Delete the selected category

        EOF
      end
parse_category(args) click to toggle source

Creates a new Category or selects an already existing one.

@param [Array] args the argumets passed to the app by the user. @return [void]

# File lib/r2do/commands/category_command.rb, line 123
def parse_category(args)
  extra = ''
  category_name = args[1..-1].join(' ')
  if @state.contains?(category_name)
    cat = @state.get(category_name)
  else
    extra = 'new '
    cat = Category.new(category_name)
    @state.add(cat)
  end

  @state.set_current(cat)
  @state.modified = true

  UI.status("Switched to #{extra}category '#{category_name}'")
end
require_selected_category() click to toggle source

Ensures that a category is selected.

@return [void]

# File lib/r2do/commands/category_command.rb, line 143
def require_selected_category()
  if @state.current_category.nil?
    raise CategoryNotSelectedError, "This action requires a selected category."
  end
end