Class: R2do::Commands::CategoryCommand
- Inherits:
-
R2do::Command
- Object
- R2do::Command
- R2do::Commands::CategoryCommand
- Defined in:
- lib/r2do/commands/category_command.rb
Constant Summary collapse
- YES =
"Y"
- DISPLAY =
"--display"
- DELETE =
"--delete"
- EDIT =
"--edit"
Instance Attribute Summary
Attributes inherited from R2do::Command
#description, #extended, #short
Instance Method Summary collapse
-
#delete_category(args) ⇒ void
Deletes the currently selected category.
-
#display_current_category(args) ⇒ void
Shows the detailed information for the current category, including the tasks contained.
-
#edit_current_category(args) ⇒ void
Edit the current task.
-
#execute(args) ⇒ void
Creates a new category or makes a category current in the state if a category with the same name already exists.
- #help ⇒ Object
-
#initialize(state) ⇒ CategoryCommand
constructor
A new instance of CategoryCommand.
-
#parse_category(args) ⇒ void
Creates a new Category or selects an already existing one.
-
#require_selected_category ⇒ void
Ensures that a category is selected.
Methods inherited from R2do::Command
Constructor Details
#initialize(state) ⇒ CategoryCommand
Returns a new instance of CategoryCommand.
27 28 29 30 31 |
# File 'lib/r2do/commands/category_command.rb', line 27 def initialize(state) super('c', 'category', 'Creates a new category.') @state = state end |
Instance Method Details
#delete_category(args) ⇒ void
This method returns an undefined value.
Deletes the currently selected category
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# 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) ⇒ void
This method returns an undefined value.
Shows the detailed information for the current category, including the tasks contained
89 90 91 92 93 94 95 96 97 |
# 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) ⇒ void
This method returns an undefined value.
Edit the current task.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# 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) ⇒ void
This method returns an undefined value.
Creates a new category or makes a category current in the state if a category with the same name already exists
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# 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 ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# 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) ⇒ void
This method returns an undefined value.
Creates a new Category or selects an already existing one.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# 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 ⇒ void
This method returns an undefined value.
Ensures that a category is selected.
143 144 145 146 147 |
# 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 |