todo-void 0.0.4 → 0.1pre

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.
@@ -1,3 +1,5 @@
1
+ require_relative '../lib/todo_filter'
2
+
1
3
  class StatusChangesInteractor
2
4
  class StatusChangesInteractor::NoTodoWithIdError < StandardError ; end
3
5
  class ConflictingIdsError < StandardError
@@ -20,7 +22,8 @@ class StatusChangesInteractor
20
22
  private
21
23
  def change_todo_status(hash, status)
22
24
  todos = @list.find(hash).to_array
23
-
25
+ todos = TodoFilter.new(todos).with_status([:finished, :started, :pending]).recent.execute
26
+
24
27
  case todos.length
25
28
  when 0
26
29
  raise StatusChangesInteractor::NoTodoWithIdError
@@ -1,4 +1,5 @@
1
1
  require_relative '../lib/todo_store'
2
+ require_relative '../lib/todo_filter'
2
3
 
3
4
  class TodoInteractor
4
5
  def initialize(store = TodoStore.new)
@@ -10,6 +11,13 @@ class TodoInteractor
10
11
  end
11
12
 
12
13
  def list_all
13
- @store.todos.to_array
14
+ todos = @store.todos.to_array
15
+ filter_todos(todos)
16
+ end
17
+
18
+ private
19
+ def filter_todos(todos)
20
+ filter = TodoFilter.new(todos)
21
+ filter.with_status([:started, :pending, :finished]).recent.execute
14
22
  end
15
23
  end
data/lib/todo.rb CHANGED
@@ -2,7 +2,7 @@ require 'openssl'
2
2
 
3
3
  class Todo
4
4
  attr_reader :description
5
- attr_accessor :status
5
+ attr_accessor :status, :finished_at
6
6
 
7
7
  def initialize(description)
8
8
  @description = description
@@ -14,4 +14,11 @@ class Todo
14
14
  description_hash = description_hash.hexdigest
15
15
  description_hash.slice(0..5)
16
16
  end
17
+
18
+ def status=(status)
19
+ @status = status
20
+ if status == :finished && finished_at.nil?
21
+ @finished_at = Time.now
22
+ end
23
+ end
17
24
  end
@@ -0,0 +1,35 @@
1
+ class TodoFilter
2
+ def initialize(todos)
3
+ @todos = todos.dup
4
+ @todos.reject! { |todo| todo.status == :deleted }
5
+ end
6
+
7
+ def with_status(statuses)
8
+ result = []
9
+ statuses.each do |status|
10
+ todos_with_status(status).each do |todo|
11
+ result << todo
12
+ end
13
+ end
14
+ @todos = result
15
+ self
16
+ end
17
+
18
+ def recent
19
+ @todos.reject! do |todo|
20
+ todo.status == :finished &&
21
+ todo.finished_at < (Time.now - 86400)
22
+ end
23
+ self
24
+ end
25
+
26
+ def execute
27
+ @todos
28
+ end
29
+
30
+ private
31
+
32
+ def todos_with_status(status)
33
+ @todos.reject { |todo| todo.status != status }
34
+ end
35
+ end
@@ -2,33 +2,12 @@ require_relative "../lib/todo_view"
2
2
 
3
3
  class TodoListView
4
4
  def self.render(todos)
5
- self.new(todos).render
6
- end
7
-
8
- def initialize(todos)
9
- @todos = todos
10
- end
11
-
12
- def render
13
- @output = ""
14
- render_todo_by_status(:started)
15
- render_todo_by_status(:pending)
16
- render_todo_by_status(:finished)
17
- @output
18
- end
19
-
20
- private
21
- def render_todo_by_status(status)
22
- todos_by_status(status).each { |todo| render_todo(todo) }
23
- end
24
-
25
- def todos_by_status(status)
26
- @todos.reject {|todo| todo.status != status}
27
- end
28
-
29
- def render_todo(todos)
30
- @output += TodoView.new(todos).render
31
- @output += "\n"
5
+ output = ""
6
+ todos.each do |todo|
7
+ output += TodoView.new(todo).render
8
+ output += "\n"
9
+ end
10
+ output
32
11
  end
33
12
  end
34
13
 
data/lib/todo_store.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require_relative "./todo_list"
2
2
  require_relative './todo'
3
3
  require 'fileutils'
4
+ require 'csv'
4
5
 
5
6
  class TodoStore
6
7
  def initialize
@@ -23,31 +24,26 @@ class TodoStore
23
24
 
24
25
  private
25
26
  def read
26
- todo_list = TodoList.new
27
-
27
+ @list = TodoList.new
28
+
28
29
  FileUtils.touch(todo_file)
29
- file_content = File.readlines(todo_file)
30
-
31
- file_content.each do |line|
32
- line = line.gsub("\n", "")
33
- status, description = line.split "||"
34
- todo = Todo.new(description)
35
- todo.status = status.to_sym
36
- todo_list.add todo
30
+ data = CSV.read(todo_file)
31
+ data.each do |raw_todo|
32
+ todo = Todo.new raw_todo[0]
33
+ unless raw_todo[2].nil?
34
+ todo.finished_at = DateTime.parse(raw_todo[2]).to_time
35
+ end
36
+ todo.status = raw_todo[1].to_sym
37
+ @list.add todo
37
38
  end
38
-
39
- @list = todo_list
40
39
  end
41
-
40
+
42
41
  def write
43
- serialized_todos = ""
44
-
45
- todos = @list.todo
46
- todos.each do |id, todo|
47
- serialized_todos += "#{todo.status}||#{todo.description}\n"
42
+ CSV.open(todo_file, "w") do |csv|
43
+ @list.to_array.each do |task|
44
+ csv << [task.description, task.status, task.finished_at]
45
+ end
48
46
  end
49
-
50
- File.open(todo_file, "w") {|f| f.write serialized_todos }
51
47
  end
52
48
 
53
49
  def todo_file
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: todo-void
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
5
- prerelease:
4
+ version: 0.1pre
5
+ prerelease: 3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Robert Curth
@@ -99,6 +99,7 @@ extensions: []
99
99
  extra_rdoc_files: []
100
100
  files:
101
101
  - lib/todo.rb
102
+ - lib/todo_filter.rb
102
103
  - lib/todo_list.rb
103
104
  - lib/todo_list_view.rb
104
105
  - lib/todo_store.rb
@@ -123,9 +124,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
123
124
  required_rubygems_version: !ruby/object:Gem::Requirement
124
125
  none: false
125
126
  requirements:
126
- - - ! '>='
127
+ - - ! '>'
127
128
  - !ruby/object:Gem::Version
128
- version: '0'
129
+ version: 1.3.1
129
130
  requirements: []
130
131
  rubyforge_project:
131
132
  rubygems_version: 1.8.24