todo-void 0.0.1 → 0.0.2

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.
data/bin/t CHANGED
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
+ require 'require_relative'
2
3
  require_relative '../lib/todo_void'
3
4
 
4
- puts TodoVoid.execute(ARGV)
5
+ app = TodoVoid.new(ARGV)
6
+ puts app.execute
@@ -1,23 +1,30 @@
1
1
  class StatusChangesInteractor
2
- def initialize(store = TodoStore.new)
3
- @store = store
4
- @list = @store.read
2
+ class ConflictingIdsError < StandardError
3
+ attr_reader :conflicting_todos
4
+
5
+ def initialize(conflicting_todos)
6
+ @conflicting_todos = conflicting_todos
7
+ end
5
8
  end
6
9
 
7
- def finish_todo(hash)
8
- change_todo_status(hash, :finished)
10
+ def initialize(store = TodoStore.new)
11
+ @store = store
12
+ @list = @store.todos
9
13
  end
10
14
 
11
- def start_todo(hash)
12
- change_todo_status(hash, :started)
15
+ def change_status(hash, status)
16
+ change_todo_status(hash, status)
13
17
  end
14
18
 
15
19
  private
16
20
  def change_todo_status(hash, status)
17
- todo = @list.find(hash)
18
- @list.remove(todo)
19
- todo.status = status
20
- @list.add(todo)
21
- @store.save(@list)
21
+ todos = @list.find(hash).to_array
22
+
23
+ if todos.length > 1
24
+ raise ConflictingIdsError.new(todos)
25
+ elsif todos.length == 1
26
+ todos[0].status = status
27
+ @store.update(todos[0])
28
+ end
22
29
  end
23
- end
30
+ end
@@ -3,27 +3,13 @@ require_relative '../lib/todo_store'
3
3
  class TodoInteractor
4
4
  def initialize(store = TodoStore.new)
5
5
  @store = store
6
- @list = @store.read
7
6
  end
8
7
 
9
8
  def add_todo(description)
10
- @list.add(Todo.new(description))
11
- save_list
12
- end
13
-
14
- def delete_todo(hash)
15
- @list.remove(hash)
16
- save_list
9
+ @store.save(Todo.new(description))
17
10
  end
18
11
 
19
12
  def list_all
20
- todos = []
21
- @list.todo.each do |id, todo| todos << todo end
22
- todos
23
- end
24
-
25
- private
26
- def save_list
27
- @store.save(@list)
13
+ @store.todos.to_array
28
14
  end
29
15
  end
data/lib/todo_list.rb CHANGED
@@ -10,13 +10,29 @@ class TodoList
10
10
  end
11
11
 
12
12
  def find(search)
13
+ list = TodoList.new
13
14
  @todo.each do |id, todo|
14
- return todo if /^#{search}/.match id
15
+ list.add todo if /^#{search}/.match id
15
16
  end
16
- nil
17
+ list
17
18
  end
18
19
 
19
- def remove(hash)
20
- @todo.delete(hash)
20
+ def length
21
+ @todo.length
22
+ end
23
+
24
+ def empty?
25
+ @todo.length == 0
26
+ end
27
+
28
+ def update(todo)
29
+ @todo.delete(todo.id)
30
+ @todo[todo.id] = todo
31
+ end
32
+
33
+ def to_array
34
+ todos = []
35
+ @todo.each do |id, todo| todos << todo end
36
+ todos
21
37
  end
22
38
  end
@@ -1,6 +1,10 @@
1
1
  require_relative "../lib/todo_view"
2
2
 
3
3
  class TodoListView
4
+ def self.render(todos)
5
+ self.new(todos).render
6
+ end
7
+
4
8
  def initialize(todos)
5
9
  @todos = todos
6
10
  end
data/lib/todo_store.rb CHANGED
@@ -3,7 +3,26 @@ require_relative './todo'
3
3
  require 'fileutils'
4
4
 
5
5
  class TodoStore
6
- def read
6
+ def initialize
7
+ read
8
+ end
9
+
10
+ def todos
11
+ @list
12
+ end
13
+
14
+ def update(todo)
15
+ @list.update(todo)
16
+ write
17
+ end
18
+
19
+ def save(todo)
20
+ @list.add(todo)
21
+ write
22
+ end
23
+
24
+ private
25
+ def read
7
26
  todo_list = TodoList.new
8
27
 
9
28
  FileUtils.touch(todo_file)
@@ -17,20 +36,20 @@ class TodoStore
17
36
  todo_list.add todo
18
37
  end
19
38
 
20
- todo_list
39
+ @list = todo_list
21
40
  end
22
41
 
23
- def save(list)
24
- seriaized_todos = ""
25
- todos = list.todo
42
+ def write
43
+ serialized_todos = ""
44
+
45
+ todos = @list.todo
26
46
  todos.each do |id, todo|
27
- seriaized_todos += "#{todo.status}||#{todo.description}\n"
47
+ serialized_todos += "#{todo.status}||#{todo.description}\n"
28
48
  end
29
49
 
30
- File.write(todo_file, seriaized_todos)
50
+ File.open(todo_file, "w") {|f| f.write serialized_todos }
31
51
  end
32
52
 
33
- private
34
53
  def todo_file
35
54
  Dir.home + '/.todos'
36
55
  end
data/lib/todo_void.rb CHANGED
@@ -1,26 +1,47 @@
1
+ require 'require_relative'
1
2
  require_relative '../interactors/todo_interactor.rb'
2
3
  require_relative '../interactors/status_changes_interactor.rb'
4
+ require_relative './todo_list_view'
3
5
 
4
6
  class TodoVoid
5
- def self.execute(args)
6
- output = ""
7
+ def initialize(args=[])
8
+ @args = args
9
+ @output = ""
10
+ end
7
11
 
8
- if args[0] == "-d"
9
- TodoInteractor.new.delete_todo(args[1])
10
- elsif args[0] == "-f"
11
- StatusChangesInteractor.new.finish_todo(args[1])
12
- elsif args[0] == "-s"
13
- StatusChangesInteractor.new.start_todo(args[1])
14
- elsif args[0] == "--help"
15
- output = "Help:\nt 'Mowing the lawn' in order to create a todo\n-f ID to finish a todo\n-d ID to delete a todo"
16
- elsif args[0]
17
- TodoInteractor.new.add_todo(args[0])
12
+ def execute
13
+ if flag?('-d')
14
+ change_status(:deleted)
15
+ elsif flag?('-f')
16
+ change_status(:finished)
17
+ elsif flag?('-s')
18
+ change_status(:started)
19
+ elsif flag?('--help')
20
+ @output = "Help:\nt 'Mowing the lawn' in order to create a todo\n-f ID to finish a todo\n-d ID to delete a todo"
21
+ elsif @args[0]
22
+ TodoInteractor.new.add_todo(@args[0])
18
23
  else
19
- require_relative './todo_list_view.rb'
20
24
  todos = TodoInteractor.new.list_all
21
- view = TodoListView.new(todos)
22
- output = view.render
25
+ @output = TodoListView.render(todos)
26
+ end
27
+ @output
28
+ end
29
+
30
+ private
31
+ def flag?(flag)
32
+ @args[0] == flag
33
+ end
34
+
35
+ def hash
36
+ @args[1]
37
+ end
38
+
39
+ def change_status(status)
40
+ begin
41
+ StatusChangesInteractor.new.change_status(hash, status)
42
+ rescue StatusChangesInteractor::ConflictingIdsError => e
43
+ @output += "Conflicting part of an id provided please be more specific:\n"
44
+ @output += TodoListView.render(e.conflicting_todos)
23
45
  end
24
- output
25
46
  end
26
47
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: todo-void
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -27,6 +27,54 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: cucumber
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: pry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
30
78
  description: A simple app to manage todos
31
79
  email: robert@rocu.de
32
80
  executables:
@@ -44,7 +92,8 @@ files:
44
92
  - interactors/status_changes_interactor.rb
45
93
  - interactors/todo_interactor.rb
46
94
  homepage: http://rubygems.org/gems/todo-void
47
- licenses: []
95
+ licenses:
96
+ - MIT
48
97
  post_install_message:
49
98
  rdoc_options: []
50
99
  require_paths: