Permalink
Browse files

task encapsulation

  • Loading branch information...
1 parent 0b85538 commit 3400e377585ddef9f714107a71a516a9ffa48130 @cocoa committed Jun 3, 2013
Showing with 71 additions and 1 deletion.
  1. +69 −0 have_a_purpose/todo/bin/todo.rb
  2. +2 −1 have_a_purpose/todo/bin/todo.txt
@@ -0,0 +1,69 @@
+#!/usr/bin/env ruby
+
+TODO_FILE = "todo.txt"
+
+# COMMANDS
+#
+# todo new "this is a brand new task"
+# todo list
+# todo done 1
+
+
+# READ & WRITE Encapsulation of data
+#
+def write_todo(file,task,created=Time.now,completed='')
+ file.puts "#{task},#{created},#{completed}"
+end
+
+def read_todo(line)
+ line.chomp.split(/,/)
+end
+
+
+command = ARGV.shift
+
+case command
+when 'new'
+ new_task = ARGV.shift
+ File.open(TODO_FILE,'a') do |file|
+ write_todo(file,new_task)
+ puts "Task added."
+ end
+
+when 'list'
+ File.open(TODO_FILE,'r') do |file|
+ counter = 1
+ file.readlines.each do |line|
+ name,created,completed = read_todo(line)
+ printf("%3d - %s\n", counter, name)
+ printf(" Created: %s\n", created)
+ unless completed.nil?
+ printf(" Complet: %s\n", completed)
+ end
+ counter += 1
+ end
+ end
+
+when 'done'
+ task_number = ARGV.shift.to_i
+
+ File.open(TODO_FILE,'r') do |file|
+ File.open("#{TODO_FILE}.new",'w') do |new_file|
+ #...
+ counter = 1
+ file.readlines.each do |line|
+ name,created,completed = read_todo(line)
+ if (task_number==counter)
+ write_todo(new_file,name,created,Time.now)
+ else
+ write_todo(new_file,name,created,completed)
+ end
+ counter += 1
+ end
+ end
+ end
+ `mv #{TODO_FILE}.new #{TODO_FILE}`
+end
+
+
+
@@ -1,3 +1,4 @@
-Comprar una nevera nueva,2013-06-01 17:41:41 +0200,2013-06-01 18:05:45 +0200
+Comprar una nevera nueva,2013-06-01 17:41:41 +0200,2013-06-03 21:19:09 +0200
Instalar la batería de repuesto,2013-06-01 17:41:57 +0200,
Tirar al albanil por la borda,2013-06-01 17:52:48 +0200,
+Finiquitar este ejemplo,2013-06-03 21:05:09 +0200,2013-06-03 21:18:58 +0200

0 comments on commit 3400e37

Please sign in to comment.