r2do 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
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
+
20
+
21
+ end
22
+ end
@@ -0,0 +1,40 @@
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
+ # Initializes the applications and resets the datafile
22
+ #
23
+ # @param [Array] args the arguments passed to the app by the user
24
+ # @return [void]
25
+ def handle_init(args)
26
+ UI.status("Initialize new session?")
27
+ UI.new_line()
28
+ value = UI.input("Any previous session will be lost. Continue? [Yn]")
29
+ if value == YES
30
+ @state = State.new()
31
+ @modified = true
32
+ UI.status("Initialized a new session of r2do.")
33
+ else
34
+ UI.status("Continuing with current session.")
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,122 @@
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
+
28
+ # Creates a new task or makes a task current in the current category if a task with the
29
+ # same name already exists
30
+ #
31
+ # @param [Array] args the arguments passed to the app by the user
32
+ # @raise [ArgumentError] if the command does not contain a name for the task
33
+ # @return [void]
34
+ def handle_task(args)
35
+ if args.length < 2
36
+ raise ArgumentError, "The 'task' command requires additional arguments."
37
+ end
38
+
39
+ if not @state.current_category
40
+ raise CategoryNotSelectedError, "You need to select a category to create a new task."
41
+ end
42
+
43
+ option = args[1]
44
+
45
+ if option.eql?(DISPLAY)
46
+ require_selected_task()
47
+ show_current_task(args)
48
+ elsif option.eql?(COMPLETED)
49
+ require_selected_task()
50
+ mark_as_complete(args)
51
+ elsif option.eql?(DELETE)
52
+ require_selected_task()
53
+ delete_task(args)
54
+ elsif option.start_with?("--")
55
+ raise InvalidOptionError
56
+ else
57
+ parse_task(args)
58
+ end
59
+ end
60
+
61
+
62
+ def delete_task(args)
63
+ UI.status("Are you sure you want to delete the task:")
64
+ UI.status(" #{@state.current_category.current_task.description}")
65
+ UI.new_line()
66
+ value = UI.input("This action cannot be undone. Continue? [Yn]")
67
+ if value == YES
68
+ task = @state.current_category.current_task
69
+ @state.current_category.remove(task)
70
+ @state.current_category.clear_current_task()
71
+ @modified = true
72
+
73
+ UI.status("Task '#{task.description}' has been deleted.")
74
+ end
75
+ end
76
+
77
+
78
+ def show_current_task(args)
79
+ task = @state.current_category.current_task
80
+ UI.status(task.display())
81
+ end
82
+
83
+ # Marks a task as completed
84
+ #
85
+ # @param [Array] args the arguments passed to the app by the user
86
+ # @return [void]
87
+ def mark_as_complete(args)
88
+ task = @state.current_category.current_task
89
+ task.completed()
90
+ @modified = true
91
+
92
+ UI.status("Task '%s' has been marked as completed." % task.description)
93
+ end
94
+
95
+
96
+ def parse_task(args)
97
+ extra = ''
98
+ task_description = args[1]
99
+ task = @state.current_category.find_by_description(task_description)
100
+ if task.nil?
101
+ task = Task.new(task_description)
102
+ @state.current_category.add(task)
103
+
104
+ UI.status("Created new task.")
105
+ UI.new_line()
106
+ end
107
+
108
+ @state.current_category.set_current(task)
109
+ @modified = true
110
+
111
+ UI.status("Selected task '#{task_description}'")
112
+ end
113
+
114
+ def require_selected_task()
115
+ if not @state.current_category.current_task
116
+ raise TaskNotSelectedError, "This action requires a selected task."
117
+ end
118
+ end
119
+
120
+ end
121
+
122
+ end
data/lib/r2do/state.rb ADDED
@@ -0,0 +1,82 @@
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
+
19
+ class State
20
+ # @return [Hash] the collection of categories created by the user.
21
+ attr_accessor :categories
22
+ # @return [Category] the current category the user is working on.
23
+ attr_accessor :current_category
24
+
25
+ # Creates a new instance of the State
26
+ #
27
+ def initialize()
28
+ @categories = Hash.new
29
+ @current_category = nil
30
+ end
31
+
32
+ # Sets a Category as the current one.
33
+ #
34
+ # @param [Category] category the category to be set as current.
35
+ # @return [void]
36
+ def set_current(category)
37
+ @current_category = category
38
+ end
39
+
40
+ # Clears the current category
41
+ #
42
+ # return [void]
43
+ def clear_current_category()
44
+ @current_category = nil
45
+ end
46
+
47
+ # Checks if a category with a specific name already exists.
48
+ #
49
+ # @param [String] category_name the name of the category to check.
50
+ # @return [bool] true if the category is already present.
51
+ def contains?(category_name)
52
+ @categories.has_key?(category_name)
53
+ end
54
+
55
+ # Retrieves a specific Category.
56
+ #
57
+ # @param [String] category_name the name of the category to retrieve.
58
+ # @return [Category] the category identified by category_name.
59
+ def get(category_name)
60
+ @categories[category_name]
61
+ end
62
+
63
+ # Adds a category.
64
+ #
65
+ # @param [Category] category the category to add.
66
+ # @return [void]
67
+ def add(category)
68
+ @categories[category.name] = category
69
+ end
70
+
71
+ # Removes the category from the state.
72
+ #
73
+ # @param [Category] category the category to remove.
74
+ # @raise [Exceptions::CategoryNotFoundError] if category is not found.
75
+ # @return [void]
76
+ def remove(category)
77
+ @categories.delete(category.name) { raise CategoryNotFoundError.new() }
78
+ end
79
+
80
+ end
81
+
82
+ end
data/lib/r2do/task.rb ADDED
@@ -0,0 +1,91 @@
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
+
19
+ class Task
20
+ # @return [String] the description for this task.
21
+ attr_accessor :description
22
+ # @return [DateTime] the date and time of completion.
23
+ attr_accessor :date_done
24
+ # @return [DateTime] the date and time of creation
25
+ attr_accessor :date_created
26
+
27
+ # Creates a new instance of a Task
28
+ #
29
+ # @param [String] desctiption the description for this task
30
+ def initialize(description)
31
+ @description = description
32
+ @done = false
33
+ @date_created = DateTime.now
34
+ @date_done = nil
35
+ end
36
+
37
+ # Gets the completed status of the specific task.
38
+ #
39
+ # @return [bool] true if the task is completed.
40
+ def done?()
41
+ return @done
42
+ end
43
+
44
+ # Flags the specific task as completed.
45
+ #
46
+ # @return [DateTime] the date and time of completion.
47
+ def completed()
48
+ @done = true
49
+ @date_done = DateTime.now
50
+ end
51
+
52
+ # Returns a string representation of this Task
53
+ #
54
+ # @return [String] the representation of this Task
55
+ def to_s()
56
+ completed = ' '
57
+ date = ''
58
+
59
+ if done?
60
+ completed = 'x'
61
+ date = format_date(@date_done)
62
+ end
63
+
64
+ return "[%s] %-30s %s" % [completed, @description, date]
65
+ end
66
+
67
+ def display()
68
+ date = format_date(@date_created)
69
+
70
+ result = StringIO.new
71
+
72
+ result << "Selected task:\n"
73
+ result << " %s\n\n" % @description
74
+ result << "Created:\n"
75
+ result << " %s" % date
76
+
77
+ if done?
78
+ result << "\nCompleted:\n"
79
+ result << " %s" % format_date(@date_done)
80
+ end
81
+
82
+ return result.string
83
+ end
84
+
85
+
86
+ def format_date(date)
87
+ date.strftime('%a %b %e, %Y')
88
+ end
89
+ end
90
+
91
+ end
data/lib/r2do/ui.rb ADDED
@@ -0,0 +1,47 @@
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
+
19
+ class UI
20
+
21
+ # Displays a status message to the user
22
+ #
23
+ # @param [String] message the message to display to the user
24
+ # @return [void]
25
+ def self.status(message)
26
+ puts message
27
+ end
28
+
29
+ # Adds a new empty line on the display
30
+ #
31
+ # @return [void]
32
+ def self.new_line()
33
+ puts
34
+ end
35
+
36
+ # Accepts user input after displaying a message
37
+ #
38
+ # @return [String] the value the user inserted
39
+ def self.input(message)
40
+ print "#{message} "
41
+ $stdout.flush
42
+ value = $stdin.gets.chomp
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,89 @@
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
+
19
+ module Utility
20
+ module_function
21
+
22
+ # Loads the data file and deserializes the State.
23
+ #
24
+ # @param [String] file_name the name of the file to load.
25
+ # @return [State] the application State.
26
+ def load_state(file_name)
27
+ file_path = calculate_path(file_name)
28
+
29
+ if File.exists?(file_path)
30
+ file = File.open(file_path, "rb")
31
+ state = YAML::load(file.read)
32
+ else
33
+ state = State.new
34
+ end
35
+
36
+ state
37
+ end
38
+
39
+ # Saves the data file and serializes the State.
40
+ #
41
+ # @param [String] file_name the name of the file to save.
42
+ # @param [State] the application state to save.
43
+ # @return [void]
44
+ def save_state(file_name, state)
45
+ file_path = calculate_path(file_name)
46
+
47
+ file = File.new(file_path, 'w')
48
+ file.write(YAML.dump(state))
49
+ file.close()
50
+ end
51
+
52
+ # Calculates the path location for the data file.
53
+ #
54
+ # @param [String] file_name the name of the file to load.
55
+ # @return [String] the full destination path including the filename.
56
+ def calculate_path(file_name)
57
+ data_path = File.expand_path("~/")
58
+ file_path = File.join(data_path, file_name)
59
+ end
60
+
61
+ # Show the help command
62
+ #
63
+ # @param [Array] args the list of args the user passed the application
64
+ # @return [void]
65
+ def show_help(args)
66
+ UI.status("Usage:")
67
+ UI.status(" r2do <command> [<args>] [options]")
68
+ UI.new_line()
69
+ UI.status("Commands:")
70
+
71
+ @commands.each do |value|
72
+ UI.status(" %s" % value.to_s())
73
+ end
74
+
75
+ UI.new_line()
76
+ UI.status("See 'r2do help <command>' for more information on a specific command.")
77
+ end
78
+
79
+ # Show the version number of the application
80
+ #
81
+ # @param [Array] args the list of args the user passed the application
82
+ # @return [void]
83
+ def show_version(args)
84
+ UI.status(R2do::VERSION)
85
+ end
86
+
87
+ end
88
+
89
+ end
@@ -0,0 +1,19 @@
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
+ VERSION = '0.0.6'
19
+ end
data/lib/r2do.rb ADDED
@@ -0,0 +1,117 @@
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 'yaml'
18
+
19
+ require 'r2do/ui'
20
+ require 'r2do/category'
21
+ require 'r2do/task'
22
+ require 'r2do/exceptions'
23
+ require 'r2do/command'
24
+ require 'r2do/state'
25
+ require 'r2do/version'
26
+ require 'r2do/handlers/handle_category'
27
+ require 'r2do/handlers/handle_task'
28
+ require 'r2do/handlers/handle_init'
29
+ require 'r2do/handlers/handle_categories'
30
+ require 'r2do/handlers/handle_current'
31
+ require 'r2do/utility'
32
+
33
+
34
+ module R2do
35
+ class App
36
+ include R2do
37
+ include Utility
38
+ include Handlers
39
+
40
+ # Creates an instance of the application.
41
+ #
42
+ # @param [Array] args the command line args.
43
+ def initialize(args)
44
+ @args = args
45
+ @commands = create_commands()
46
+ @modified = false
47
+ @file_name = ".r2do.yml"
48
+
49
+ @state = load_state(@file_name)
50
+ end
51
+
52
+ # Evaluates the command passed by the user and calls the corresponding application command.
53
+ #
54
+ # @return [void]
55
+ def run()
56
+ option = @args[0]
57
+
58
+ if @args.length > 0
59
+ cmd = find_command(option)
60
+ if not cmd.nil?
61
+ cmd.execute(@args)
62
+ else
63
+ invalid_command(option)
64
+ end
65
+ else
66
+ show_help(@args)
67
+ end
68
+ end
69
+
70
+ # Saves the state of the application
71
+ #
72
+ # @return [void]
73
+ def save()
74
+ if @modified
75
+ save_state(@file_name, @state)
76
+ end
77
+ end
78
+
79
+ private
80
+
81
+ # Creates the list of commands the application responds to.
82
+ #
83
+ # @return [Array] the collection of commands.
84
+ def create_commands()
85
+ cmd_list = Array.new
86
+ cmd_list << Command.new('i', 'initialize', nil, 'Initializes a new clean session.', method(:handle_init))
87
+ cmd_list << Command.new('c', 'category', 'NAME', 'Creates a new category', method(:handle_category))
88
+ cmd_list << Command.new('t', 'task', 'NAME', 'Adds a new task to the current category.', method(:handle_task))
89
+ cmd_list << Command.new('d', 'display', nil, 'Displays all the categories', method(:handle_categories))
90
+ cmd_list << Command.new('n', 'now', nil, 'Displays the information for the current category', method(:display_current_category))
91
+ cmd_list << Command.new('h', 'help', nil, 'Displays the help for a command', method(:handle_help))
92
+
93
+ cmd_list << Command.new('-v', '--version', nil, 'Prints the application version.', method(:show_version))
94
+ cmd_list << Command.new('-h', '--help', nil, 'You are looking at it.', method(:show_help))
95
+
96
+ cmd_list
97
+ end
98
+
99
+ # Finds the command based on the option value passed by the user
100
+ #
101
+ # @param [String] option the option the user passed the application
102
+ # @return [Command] the command identified by option, else nil
103
+ def find_command(option)
104
+ @commands.find { |cmd| cmd.short == option or cmd.extended == option }
105
+ end
106
+
107
+ # Invalid command handler
108
+ #
109
+ # @param [String] option the option the user passed the application
110
+ # @return [void]
111
+ def invalid_command(option)
112
+ UI.status("r2do: '#{option}' is not an r2do command. See 'r2do -h'.")
113
+ end
114
+
115
+ end
116
+
117
+ end
data/r2do.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/r2do/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["cgiacomi"]
6
+ gem.email = ["christiangiacomi@gmail.com"]
7
+ gem.description = %q{A simple todo gem}
8
+ gem.summary = %q{A simple todo gem}
9
+ gem.homepage = ""
10
+
11
+ gem.add_development_dependency "rspec"
12
+ gem.add_development_dependency "rake"
13
+ gem.add_development_dependency "yard"
14
+ gem.add_development_dependency "simplecov"
15
+ gem.add_development_dependency "redcarpet"
16
+
17
+ gem.files = `git ls-files`.split($\)
18
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
+ gem.name = "r2do"
21
+ gem.require_paths = ["lib"]
22
+ gem.version = R2do::VERSION
23
+ end