class Tudu::Tasks
Tudu::Tasks
¶ ↑
Public Class Methods
add(*task_names)
click to toggle source
== add task to todos === Params
-
task_names : add task name list
# File lib/tasks.rb, line 13 def add(*task_names) task_names.each do |task_name| if find_tasks(task_name) puts "#{task_name} is already exists." next end File.open(TuduPaths::TUDU_TODOS_FILE_PATH, 'a:UTF-8') do |f| f.puts task_name end puts "complete add todo '#{task_name}' to tudu/todos" end end
choose(task_name)
click to toggle source
== choose todo => doing task === Params
-
task_name : target task name
# File lib/tasks.rb, line 36 def choose(task_name) return if when_choose_no_todos? return unless when_choose_no_doings? task_name = get_first_todo_name_if_nil_or_empty task_name task = find_tasks(task_name) return if when_choose_no_task?(task, task_name) return unless when_choose_type_is_todo?(task, task_name) remove task_name write_doing(task_name) puts "complete add doings '#{task_name}'" end
doings()
click to toggle source
done()
click to toggle source
== doing to done
-
if doings size == 0, nothing todo.
-
after move doing to done, next todo move to doing.
# File lib/tasks.rb, line 51 def done return unless doings_to_dones todos_to_doings end
dones()
click to toggle source
filter_tasks(tasks, search_word)
click to toggle source
== filter tasklist by search_word. === Params
-
tasks : task list.
-
search_word : filtering word.
=== Returns return filterd task list
# File lib/tasks.rb, line 101 def filter_tasks(tasks, search_word) return tasks if search_word.nil? tasks.select { |task|task.name.match(/.*#{search_word}.*/) } end
find_tasks(task_name)
click to toggle source
== find task from all tasks. === Params
-
task_name : task name.
=== Returns return task
# File lib/tasks.rb, line 111 def find_tasks(task_name) tasks = get_tasks tasks.select { |task|task.name == task_name }.first end
get_tasks(type = nil)
click to toggle source
== get each type tasks. === Params
-
type : task type.if use nil, you can get all types task.
=== Returns return Array[Tasks]
# File lib/tasks.rb, line 82 def get_tasks(type = nil) type.nil? ? all_tasks : get_each_tasks(type) end
get_tasks_from_file(file_name)
click to toggle source
== get each type tasks from file. === Params
-
type : task type.
=== Returns return Array[String]
# File lib/tasks.rb, line 91 def get_tasks_from_file(file_name) File.read("./#{TuduPaths::TUDU_DIR}/#{file_name}").split("\n") end
progress()
click to toggle source
display tasks progress¶ ↑
Returns¶ ↑
return progress
# File lib/tasks.rb, line 119 def progress total_count = get_tasks.size dones_count = dones.size percent = total_count == 0 ? 0 : percentage(dones_count, total_count) prefix = "#{dones_count}/#{total_count}|" done_bar = '=' * (percent / 10) rest_bar = ' ' * (10 - (percent / 10)) progress_bar = "#{done_bar}>#{rest_bar}" sufix = "|#{percent}%" "#{prefix}#{progress_bar}#{sufix}" end
remove(*task_names)
click to toggle source
== remove task to todo === Params
-
task_names : remove task name list
# File lib/tasks.rb, line 29 def remove(*task_names) task_names.each { |task_name|remove_each_task(task_name) } end
Private Class Methods
all_tasks()
click to toggle source
# File lib/tasks.rb, line 147 def all_tasks tasks = [] TuduPaths::TASK_FILES.each_value do |each_type| tasks += get_each_tasks(each_type) end tasks end
doings_to_dones()
click to toggle source
# File lib/tasks.rb, line 202 def doings_to_dones tmp_doings = doings if tmp_doings.size == 0 puts 'there is no doing task.before done, you must choose task.' return false end write_doing('') write_done(tmp_doings.first.name) true end
finish?(tmp_todos)
click to toggle source
# File lib/tasks.rb, line 242 def finish?(tmp_todos) return false unless tmp_todos.size == 0 puts 'All Tasks Finish!!' if doings.size == 0 true end
get_each_tasks(type)
click to toggle source
# File lib/tasks.rb, line 141 def get_each_tasks(type) tasks = [] get_tasks_from_file(type).each { |task|tasks << Task.new(type, task) } tasks end
get_first_todo_name_if_nil_or_empty(task_name)
click to toggle source
# File lib/tasks.rb, line 137 def get_first_todo_name_if_nil_or_empty(task_name) task_name.nil? || task_name.empty? ? todos.first.name : task_name end
include_task?(tasks, task_name)
click to toggle source
# File lib/tasks.rb, line 155 def include_task?(tasks, task_name) tasks.include? task_name end
percentage(base, total)
click to toggle source
# File lib/tasks.rb, line 133 def percentage(base, total) (base.to_f / total.to_f * 100).round end
remove_each_task(task_name)
click to toggle source
# File lib/tasks.rb, line 159 def remove_each_task(task_name) can_find = false TuduPaths::TASK_FILES.each_value do |rf| tasks = get_tasks_from_file(rf) next unless include_task?(tasks, task_name) remove_task(tasks, task_name, "./#{TuduPaths::TUDU_DIR}/#{rf}") can_find = true break end puts "no such todo '#{task_name}'" unless can_find end
remove_task(tasks, task_name, file_path)
click to toggle source
# File lib/tasks.rb, line 171 def remove_task(tasks, task_name, file_path) tasks.delete(task_name) contents = tasks.size == 0 ? '' : tasks.join("\n") + "\n" File.open(file_path, 'w:UTF-8') { |wf|wf.print contents } puts "complete remove todo '#{task_name}' from #{file_path}" end
todos_to_doings()
click to toggle source
# File lib/tasks.rb, line 229 def todos_to_doings tmp_todos = todos return if finish?(tmp_todos) deleted_todos = tmp_todos.dup deleted_todos.delete_at 0 File.open(TuduPaths::TUDU_TODOS_FILE_PATH, 'w:UTF-8') do |f| deleted_todos.each { |task|f.puts task.name } end File.open(TuduPaths::TUDU_DOINGS_FILE_PATH, 'w:UTF-8') do |f| f.puts tmp_todos.first.name end end
when_choose_no_doings?()
click to toggle source
# File lib/tasks.rb, line 184 def when_choose_no_doings? return true if doings.size == 0 puts 'todos is empty.' false end
when_choose_no_task?(task, task_name)
click to toggle source
# File lib/tasks.rb, line 190 def when_choose_no_task?(task, task_name) return true if task.nil? puts "#{task_name} not exists" false end
when_choose_no_todos?()
click to toggle source
# File lib/tasks.rb, line 178 def when_choose_no_todos? return false unless todos.size == 0 puts 'todos is empty.' true end
when_choose_type_is_todo?(task, task_name)
click to toggle source
# File lib/tasks.rb, line 196 def when_choose_type_is_todo?(task, task_name) return true if task.todo? puts "#{task_name} is not exists in todos. #{task_name} in #{task.type}" # rubocop:disable LineLength false end
write_doing(text)
click to toggle source
# File lib/tasks.rb, line 213 def write_doing(text) File.open(TuduPaths::TUDU_DOINGS_FILE_PATH, 'w:UTF-8') do |f| if text.empty? f.print(text) else f.puts text end end end
write_done(text)
click to toggle source
# File lib/tasks.rb, line 223 def write_done(text) File.open(TuduPaths::TUDU_DONES_FILE_PATH, 'a:UTF-8') do |f| f.puts text end end