class Tot::TodoManager

Public Class Methods

new() click to toggle source
# File lib/tot.rb, line 30
def initialize
  load_file
end

Public Instance Methods

add(new_todo) click to toggle source
# File lib/tot.rb, line 50
def add(new_todo)
  @tasks.push new_todo
end
delete_at(at) click to toggle source
# File lib/tot.rb, line 62
def delete_at(at)
  @tasks.delete_at at
end
delete_by_title(title) click to toggle source
# File lib/tot.rb, line 66
def delete_by_title(title)
  @tasks.delete_at(@tasks.find_index{|obj| obj['title'] == title})
  @tasks
end
each() { |todo| ... } click to toggle source
# File lib/tot.rb, line 54
def each
  @tasks = load_file
  @tasks.each do |todo|
    yield todo
  end
  self
end
find_all!(&block) click to toggle source
# File lib/tot.rb, line 71
def find_all!(&block)
  @tasks = self.find_all(&block)
end
load_file() click to toggle source
# File lib/tot.rb, line 33
def load_file
  todo_path = Config.todo_path
  File.open(todo_path,'w'){|file| YAML.dump([],file)} unless File.exists? todo_path
  @tasks = YAML.load_file(todo_path).sort_by{|i| i['date']}
end
print_color(with_index = false) click to toggle source
refresh() click to toggle source
# File lib/tot.rb, line 39
def refresh
  load_file
  self
end
save() click to toggle source
# File lib/tot.rb, line 44
def save
  #File.open(Config.todo_path,'w'){|file| file.puts todos.ya2yaml} #YAML.dump(todos, file)}
  # ya2yamlだとhashの順番が変わる
  File.open(Config.todo_path,'w'){|file| YAML.dump(@tasks, file)}
end
stdin_parser(lines) click to toggle source

This method is incomplete, returns array of title for now.

# File lib/tot.rb, line 102
def stdin_parser(lines) #{{{
  lines = lines.split(/\n/) unless lines.class == Array
  lines.map {|line|
    line.chomp.gsub(/\e\[\d+m/,"").split('|').map(&:strip)
  }.keep_if{|i| i != []}
  .map {|l| 
    task = {}
    task[:date] = Time.parse(l[0])
    task[:title] = l[1]
    task[:tag] = YAML.load(l[2])
    task
  }
rescue
  raise RuntimeError, 'Stdin lines are invalid.'
end