r2do 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,111 +0,0 @@
1
- #
2
- # Copyright 2012 Christian Giacomi http://www.christiangiacomi.com
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
- #
16
-
17
- module R2do
18
- module Handlers
19
- module_function
20
-
21
- # Creates a new category or makes a category current in the state if a category with the
22
- # same name already exists
23
- #
24
- # @param [Array] args the arguments passed to the app by the user
25
- # @raise [ArgumentError] if the command does not contain a name for the category
26
- # @return [void]
27
- def handle_category(args)
28
- if args.length < 2
29
- raise ArgumentError, "The 'category' command requires additional arguments."
30
- end
31
-
32
- option = args[1]
33
-
34
- if option.eql?(DISPLAY)
35
- display_current_category(args)
36
- elsif option.eql?(DELETE)
37
- require_selected_category()
38
- delete_category(args)
39
- elsif option.start_with?("--")
40
- raise InvalidOptionError
41
- else
42
- parse_category(args)
43
- end
44
- end
45
-
46
- # Shows the detailed information for the current category, including the tasks contained
47
- #
48
- # @param [Array] args the arguments passed to the app by the user
49
- # @return [void]
50
- def display_current_category(args)
51
- if not @state.current_category
52
- UI.status("No category is currently selected.")
53
- else
54
- UI.status(@state.current_category.to_s)
55
- UI.new_line()
56
- end
57
- end
58
-
59
- # Deletes the currently selected category
60
- #
61
- # @param [Array] args the argumets passed to the app by the user
62
- # @return [void]
63
- def delete_category(args)
64
- UI.status("Are you sure you want to delete the category:")
65
- UI.status(" #{@state.current_category.name}")
66
- UI.new_line()
67
- UI.status("All tasks contained in this category will be lost.")
68
- value = UI.input("This action cannot be undone. Continue? [Yn]")
69
- if value == YES
70
- cat = @state.current_category
71
- @state.remove(cat)
72
- @state.clear_current_category()
73
- @modified = true
74
-
75
- UI.status("Category '#{cat.name}' has been deleted.")
76
- end
77
- end
78
-
79
- # Creates a new Category or selects an already existing one.
80
- #
81
- # @param [Array] args the argumets passed to the app by the user.
82
- # @return [void]
83
- def parse_category(args)
84
- extra = ''
85
- category_name = args[1]
86
- if @state.contains?(category_name)
87
- cat = @state.get(category_name)
88
- else
89
- extra = 'new '
90
- cat = Category.new(category_name)
91
- @state.add(cat)
92
- end
93
-
94
- @state.set_current(cat)
95
- @modified = true
96
-
97
- UI.status("Switched to #{extra}category '#{category_name}'")
98
- end
99
-
100
- # Ensures that a category is selected.
101
- #
102
- # @return [void]
103
- def require_selected_category()
104
- if not @state.current_category
105
- raise CategoryNotSelectedError, "This action requires a selected category."
106
- end
107
- end
108
-
109
- end
110
-
111
- end
@@ -1,157 +0,0 @@
1
- #
2
- # Copyright 2012 Christian Giacomi http://www.christiangiacomi.com
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
- #
16
-
17
- module R2do
18
- module Handlers
19
- module_function
20
-
21
- YES = "Y"
22
-
23
- #options
24
- COMPLETED = "--done"
25
- DISPLAY = "--display"
26
- DELETE = "--delete"
27
- EDIT = "--edit"
28
-
29
- # Creates a new task or makes a task current in the current category if a task with the
30
- # same name already exists
31
- #
32
- # @param [Array] args the arguments passed to the app by the user
33
- # @raise [ArgumentError] if the command does not contain a name for the task
34
- # @return [void]
35
- def handle_task(args)
36
- if args.length < 2
37
- raise ArgumentError, "The 'task' command requires additional arguments."
38
- end
39
-
40
- if not @state.current_category
41
- raise CategoryNotSelectedError, "You need to select a category to create a new task."
42
- end
43
-
44
- option = args[1]
45
-
46
- if option.eql?(DISPLAY)
47
- require_selected_task()
48
- show_current_task(args)
49
- elsif option.eql?(EDIT)
50
- require_selected_task()
51
- edit_current_task(args)
52
- elsif option.eql?(COMPLETED)
53
- require_selected_task()
54
- mark_as_complete(args)
55
- elsif option.eql?(DELETE)
56
- require_selected_task()
57
- delete_task(args)
58
- elsif option.start_with?("--")
59
- raise InvalidOptionError, "Invalid argument for the command. See 'r2do -h'."
60
- else
61
- parse_task(args)
62
- end
63
- end
64
-
65
- # Edit the current task.
66
- #
67
- # @param [Array] args the arguments passed to the app by the user.
68
- # @return [void]
69
- def edit_current_task(args)
70
- UI.status("Are you sure you want to edit the task:")
71
- UI.status(" #{@state.current_category.current_task.description}")
72
- UI.new_line()
73
- value = UI.input("Continue? [Yn]")
74
- if value == YES
75
- desc = UI.input("Enter new description:")
76
- task = @state.current_category.current_task
77
- task.rename(desc)
78
- @modified = true
79
-
80
- UI.status("The task as been modified.")
81
- end
82
- end
83
-
84
- # Delete the currently selected task.
85
- #
86
- # @param [Array] args the arguments passed to the app by the user.
87
- # @return [void]
88
- def delete_task(args)
89
- UI.status("Are you sure you want to delete the task:")
90
- UI.status(" #{@state.current_category.current_task.description}")
91
- UI.new_line()
92
- value = UI.input("This action cannot be undone. Continue? [Yn]")
93
- if value == YES
94
- task = @state.current_category.current_task
95
- @state.current_category.remove(task)
96
- @state.current_category.clear_current_task()
97
- @modified = true
98
-
99
- UI.status("Task '#{task.description}' has been deleted.")
100
- end
101
- end
102
-
103
- # Displays the information of the currently selected task.
104
- #
105
- # @param [Array] args the arguments passed to the app by the user.
106
- # @return [void]
107
- def show_current_task(args)
108
- task = @state.current_category.current_task
109
- UI.status(task.display())
110
- end
111
-
112
- # Marks a task as completed.
113
- #
114
- # @param [Array] args the arguments passed to the app by the user.
115
- # @return [void]
116
- def mark_as_complete(args)
117
- task = @state.current_category.current_task
118
- task.completed()
119
- @modified = true
120
-
121
- UI.status("Task '%s' has been marked as completed." % task.description)
122
- end
123
-
124
- # Creates a new task or select an already existing one.
125
- #
126
- # @param [Array] args the arguments passed to the app by the user.
127
- # @return [void]
128
- def parse_task(args)
129
- extra = ''
130
- task_description = args[1]
131
- task = @state.current_category.find_by_description(task_description)
132
- if task.nil?
133
- task = Task.new(task_description)
134
- @state.current_category.add(task)
135
-
136
- UI.status("Created new task.")
137
- UI.new_line()
138
- end
139
-
140
- @state.current_category.set_current(task)
141
- @modified = true
142
-
143
- UI.status("Selected task '#{task_description}'")
144
- end
145
-
146
- # Checks that a task is currently selected.
147
- #
148
- # @return [void]
149
- def require_selected_task()
150
- if not @state.current_category.current_task
151
- raise TaskNotSelectedError, "This action requires a selected task."
152
- end
153
- end
154
-
155
- end
156
-
157
- end
@@ -1,85 +0,0 @@
1
- #
2
- # Copyright 2012 Christian Giacomi http://www.christiangiacomi.com
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
- #
16
-
17
- require 'spec_helper'
18
-
19
- module R2do
20
-
21
- describe Command do
22
- before(:each) do
23
- @callback_invoked = false
24
- end
25
-
26
- def callback(args)
27
- @callback_invoked = true
28
- end
29
-
30
- describe "#new" do
31
- context "valid arguments" do
32
- it "returns an instance of Command" do
33
- command = Command.new('c', 'category', nil, 'description for this command', method(:callback))
34
- command.should be_an_instance_of Command
35
- end
36
- end
37
-
38
- context "null args" do
39
- it "raises an error if the swtich is null" do
40
- expect{ Command.new(nil, 'category', nil, 'description for this command', method(:callback)) }.to raise_error(ArgumentError)
41
- end
42
-
43
- it "raises an error if the name is null" do
44
- expect{ Command.new('c', nil, nil, 'description for this command', method(:callback)) }.to raise_error(ArgumentError)
45
- end
46
-
47
- it "raises an error if the description is null" do
48
- expect{ Command.new('c', 'category', nil, nil, method(:callback)) }.to raise_error(ArgumentError)
49
- end
50
-
51
- it "raises an error if the callback is null" do
52
- expect{ Command.new('c', 'category', nil, 'desctiption', nil) }.to raise_error(ArgumentError)
53
- end
54
- end
55
- end
56
-
57
- describe "#to_s" do
58
- it "returns the correct representation" do
59
- short = 'c'
60
- extended = 'category'
61
- description = 'description for this command'
62
-
63
- result = "%2s, %-10s \t# %s" % [short, extended, description]
64
- command = Command.new(short, extended, nil, description, method(:callback))
65
- command.to_s.should eql result
66
- end
67
- end
68
-
69
- describe "#execute" do
70
- it "exectutes the callback" do
71
- @callback_invoked.should eql false
72
-
73
- short = 'c'
74
- extended = 'category'
75
- description = 'description for this command'
76
- args = Array.new
77
- command = Command.new(short, extended, nil, description, method(:callback))
78
- command.execute(args)
79
-
80
- @callback_invoked.should eql true
81
- end
82
- end
83
- end
84
-
85
- end