do_stuff 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,10 +12,9 @@ module DoStuff
12
12
  FileUtils.mkdir_p(File.dirname(todofile))
13
13
  FileUtils.touch(todofile)
14
14
 
15
-
16
15
  opts = OptionParser.new do |opts|
17
16
  opts.on('-e [TASK NUM]') do |task_num|
18
- TodoList.edit(todofile)
17
+ Tasklist.edit(todofile)
19
18
  exit
20
19
  end
21
20
 
@@ -42,8 +41,8 @@ module DoStuff
42
41
  end
43
42
 
44
43
  begin
45
- todolist = TodoList.new(todofile)
46
- rescue TodoList::ParseError => e
44
+ todolist = Tasklist.new(todofile)
45
+ rescue Tasklist::ParseError => e
47
46
  abort "Error parsing #{e.file}: #{e.message}"
48
47
  end
49
48
 
@@ -53,9 +52,9 @@ module DoStuff
53
52
  end
54
53
  elsif argv.length == 1 && argv[0] =~ /^\d+$/
55
54
  task_num = argv[0].to_i
56
- task = todolist.get(task_num)
57
- abort "There is no task ##{task_num}." unless task
58
- todolist.erase(task_num)
55
+ abort "There is no task ##{task_num}." unless todolist.tasks.key?(task_num)
56
+ task = todolist[task_num]
57
+ todolist.delete(task_num)
59
58
  todolist.write!
60
59
  puts "Erased ##{task_num}: #{task}"
61
60
  else
@@ -0,0 +1,67 @@
1
+ module DoStuff
2
+ class Tasklist
3
+ attr_reader :tasks
4
+
5
+ def initialize(file)
6
+ @file = file
7
+ @tasks = {}
8
+ parse
9
+ end
10
+
11
+ def add(task)
12
+ # Find the first unused task number in the list.
13
+ task_num = 1
14
+ task_num += 1 while @tasks.key?(task_num)
15
+
16
+ @tasks[task_num] = task
17
+
18
+ task_num
19
+ end
20
+
21
+ def [](task_num)
22
+ @tasks[task_num]
23
+ end
24
+
25
+ def delete(task_num)
26
+ @tasks.delete(task_num)
27
+ end
28
+
29
+ def write!
30
+ File.open(@file, 'w') do |f|
31
+ tasks.each{|num, task| f.puts("#{num}. #{task}") }
32
+ end
33
+ end
34
+
35
+ def self.edit(file)
36
+ # TODO: Use task_num to jump to a line
37
+ system(ENV['EDITOR'], file)
38
+ end
39
+
40
+ class ParseError < ::StandardError
41
+ attr_accessor :file
42
+ def initialize(file, msg)
43
+ @file = file
44
+ super(msg)
45
+ end
46
+ end
47
+
48
+ private
49
+ def parse
50
+ File.readlines(@file).each do |line|
51
+ line.chomp!
52
+ if line =~ /^(\d+)\.\s+(.+)$/
53
+ task_num, task = $1.to_i, $2
54
+
55
+ if @tasks[task_num]
56
+ raise ParseError.new(@file, "Two definitions for task " +
57
+ "#{task_num}:\n\t#{task_num}. #{@tasks[task_num]}\n\t#{line}")
58
+ end
59
+
60
+ @tasks[task_num] = task
61
+ else
62
+ raise ParseError.new(@file, "Ill-formed line encountered:\n\t#{line}")
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
data/lib/do_stuff.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  require 'do_stuff/standalone'
2
- require 'do_stuff/todolist'
2
+ require 'do_stuff/tasklist'
3
3
  require 'do_stuff/runner'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: do_stuff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-25 00:00:00.000000000 Z
12
+ date: 2012-02-26 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A minimalistic command-line todo list
15
15
  email: scott@scott-olson.org
@@ -20,7 +20,7 @@ extra_rdoc_files: []
20
20
  files:
21
21
  - lib/do_stuff/standalone.rb
22
22
  - lib/do_stuff/runner.rb
23
- - lib/do_stuff/todolist.rb
23
+ - lib/do_stuff/tasklist.rb
24
24
  - lib/do_stuff.rb
25
25
  - bin/do_stuff
26
26
  - README.md
@@ -1,75 +0,0 @@
1
- module DoStuff
2
- class TodoList
3
- def initialize(file)
4
- @file = file
5
- parse
6
- end
7
-
8
- def add(task)
9
- # Try to fill a hole in the task list, otherwise append to the end.
10
- i = @tasks.find_index(nil)
11
- i ||= @tasks.length
12
-
13
- @tasks[i] = task
14
-
15
- # Return the task number
16
- i + 1
17
- end
18
-
19
- def erase(task_num)
20
- raise "No such task ##{task_num}." unless task_num >= 0 && @tasks[task_num - 1]
21
- @tasks[task_num - 1] = nil
22
- end
23
-
24
- def get(task_num)
25
- return nil if task_num <= 0
26
- @tasks[task_num - 1]
27
- end
28
-
29
- def tasks
30
- # Group each task with its number and remove all nils
31
- @tasks.map.with_index{|task, i| [i + 1, task] if task }.compact
32
- end
33
-
34
- def write!
35
- File.open(@file, 'w') do |f|
36
- tasks.each{|num, task| f.puts("#{num}. #{task}") }
37
- end
38
- end
39
-
40
- def self.edit(file)
41
- # TODO: Use task_num to jump to a line
42
- system(ENV['EDITOR'], file)
43
- end
44
-
45
- class ParseError < StandardError
46
- attr_accessor :file
47
- def initialize(file, msg)
48
- @file = file
49
- super(msg)
50
- end
51
- end
52
-
53
- private
54
- def parse
55
- @tasks = []
56
-
57
- File.read(@file).each_line do |line|
58
- line.chomp!
59
- if line =~ /^(\d+)\.\s+(.+)$/
60
- task_num, task = $1.to_i, $2
61
- i = task_num - 1
62
-
63
- if @tasks[i]
64
- raise ParseError.new(@file, "Two definitions for task " +
65
- "#{task_num}:\n\t#{task_num}. #{@tasks[i]}\n\t#{line}")
66
- end
67
-
68
- @tasks[i] = task
69
- else
70
- raise ParseError.new(@file, "Ill-formed line encountered:\n\t#{line}")
71
- end
72
- end
73
- end
74
- end
75
- end