Class: R2do::Commands::TaskCommand

Inherits:
R2do::Command show all
Defined in:
lib/r2do/commands/task_command.rb

Constant Summary collapse

YES =
"Y"
COMPLETED =

options

"--done"
DISPLAY =
"--display"
DELETE =
"--delete"
EDIT =
"--edit"

Instance Attribute Summary

Attributes inherited from R2do::Command

#description, #extended, #short

Instance Method Summary collapse

Methods inherited from R2do::Command

#to_s

Constructor Details

#initialize(state) ⇒ TaskCommand

Returns a new instance of TaskCommand.



29
30
31
32
33
# File 'lib/r2do/commands/task_command.rb', line 29

def initialize(state)
  super('t', 'task', 'Adds a new task to the current category.')

  @state = state
end

Instance Method Details

#delete_task(args) ⇒ void

This method returns an undefined value.

Delete the currently selected task.

Parameters:

  • args (Array)

    the arguments passed to the app by the user.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/r2do/commands/task_command.rb', line 94

def delete_task(args)
  UI.status("Are you sure you want to delete the task:")
  UI.status("   #{@state.current_category.current_task.description}")
  UI.new_line()
  value = UI.input("This action cannot be undone. Continue? [Yn]")
  if value == YES
    task = @state.current_category.current_task
    @state.current_category.remove(task)
    @state.current_category.clear_current_task()
    @state.modified = true

    UI.status("Task '#{task.description}' has been deleted.")
  end
end

#edit_current_task(args) ⇒ void

This method returns an undefined value.

Edit the current task.

Parameters:

  • args (Array)

    the arguments passed to the app by the user.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/r2do/commands/task_command.rb', line 75

def edit_current_task(args)
  UI.status("Are you sure you want to edit the task:")
  UI.status("   #{@state.current_category.current_task.description}")
  UI.new_line()
  value = UI.input("Continue? [Yn]")
  if value == YES
    desc = UI.input("Enter new description:")
    task = @state.current_category.current_task
    task.rename(desc)
    @state.modified = true

    UI.status("The task as been modified.")
  end
end

#execute(args) ⇒ void

This method returns an undefined value.

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

Parameters:

  • args (Array)

    the arguments passed to the app by the user

Raises:

  • (ArgumentError)

    if the command does not contain a name for the task



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
# File 'lib/r2do/commands/task_command.rb', line 41

def execute(args)
  if args.length < 2
    raise ArgumentError, "The 'task' command requires additional arguments."
  end

  if @state.current_category.nil?
    raise CategoryNotSelectedError, "You need to select a category to create a new task."
  end

  option = args[1]

  if option.eql?(DISPLAY)
    require_selected_task()
    show_current_task(args)
  elsif option.eql?(EDIT)
    require_selected_task()
    edit_current_task(args)
  elsif option.eql?(COMPLETED)
    require_selected_task()
    mark_as_complete(args)
  elsif option.eql?(DELETE)
    require_selected_task()
    delete_task(args)
  elsif option.start_with?("--")
    raise InvalidOptionError, "Invalid argument for the command. See 'r2do -h'."
  else
    parse_task(args)
  end
end

#helpObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/r2do/commands/task_command.rb', line 163

def help()
  help = <<-EOF
NAME
 r2do #{@extended}

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

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

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

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

  EOF
end

#mark_as_complete(args) ⇒ void

This method returns an undefined value.

Marks a task as completed.

Parameters:

  • args (Array)

    the arguments passed to the app by the user.



122
123
124
125
126
127
128
# File 'lib/r2do/commands/task_command.rb', line 122

def mark_as_complete(args)
  task = @state.current_category.current_task
  task.completed()
  @state.modified = true

  UI.status("Task '%s' has been marked as completed." % task.description)
end

#parse_task(args) ⇒ void

This method returns an undefined value.

Creates a new task or select an already existing one.

Parameters:

  • args (Array)

    the arguments passed to the app by the user.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/r2do/commands/task_command.rb', line 134

def parse_task(args)
  extra = ''
  #args[1..-1].map{|word| word.capitalize}.join(' ')
  task_description = args[1..-1].join(' ')
  task = @state.current_category.find_by_description(task_description)
  if task.nil?
    task = Task.new(task_description)
    @state.current_category.add(task)

    UI.status("Created new task.")
    UI.new_line()
  end

  @state.current_category.set_current(task)
  @state.modified = true

  UI.status("Selected task '#{task_description}'")
end

#require_selected_taskvoid

This method returns an undefined value.

Checks that a task is currently selected.



156
157
158
159
160
# File 'lib/r2do/commands/task_command.rb', line 156

def require_selected_task()
  if @state.current_category.current_task.nil?
    raise TaskNotSelectedError, "This action requires a selected task."
  end
end

#show_current_task(args) ⇒ void

This method returns an undefined value.

Displays the information of the currently selected task.

Parameters:

  • args (Array)

    the arguments passed to the app by the user.



113
114
115
116
# File 'lib/r2do/commands/task_command.rb', line 113

def show_current_task(args)
  task = @state.current_category.current_task
  UI.status(task.display())
end