Class: Tot::TodoManager

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tot.rb

Overview

}}}

Instance Method Summary collapse

Constructor Details

#initializeTodoManager

Returns a new instance of TodoManager.



31
32
33
# File 'lib/tot.rb', line 31

def initialize
  load_file
end

Instance Method Details

#add(new_todo) ⇒ Object



51
52
53
# File 'lib/tot.rb', line 51

def add(new_todo)
  @tasks.push new_todo
end

#delete_at(at) ⇒ Object



63
64
65
# File 'lib/tot.rb', line 63

def delete_at(at)
  @tasks.delete_at at
end

#delete_by_title(title) ⇒ Object



67
68
69
70
# File 'lib/tot.rb', line 67

def delete_by_title(title)
  @tasks.delete_at(@tasks.find_index{|obj| obj['title'] == title})
  @tasks
end

#eachObject



55
56
57
58
59
60
61
# File 'lib/tot.rb', line 55

def each
  @tasks = load_file
  @tasks.each do |todo|
    yield todo
  end
  self
end

#find_all!(&block) ⇒ Object



72
73
74
# File 'lib/tot.rb', line 72

def find_all!(&block)
  @tasks = self.find_all(&block)
end

#load_fileObject



34
35
36
37
38
# File 'lib/tot.rb', line 34

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

{{{



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/tot.rb', line 75

def print_color(with_index = false) #{{{
  @tasks.each_with_index do |todo,idx|
    #https://github.com/flori/term-ansicolor/blob/master/examples/example.rb
    case (Date.parse(todo['date'].to_s) - Date.parse(Time.now.to_s)).to_i
    when -10 .. -1
      print Term::ANSIColor.blue
    when 0..1
      print Term::ANSIColor.bold, Term::ANSIColor.red
    when 2..3
      print Term::ANSIColor.bold, Term::ANSIColor.yellow
    when 4..7
      print Term::ANSIColor.dark, Term::ANSIColor.cyan
    when 7..30
      print Term::ANSIColor.white
    else
      print Term::ANSIColor.magenta
    end

    puts [("<<#{idx}>>" if with_index),
          todo['date'].strftime("%Y/%m/%d %H:%M"),
          todo['title'],
          '['+todo['tag'].flatten.join(',')+']'].keep_if{|i| not i.nil?}.join(' | ')
    print Term::ANSIColor.reset
  end
  self
end

#refreshObject



40
41
42
43
# File 'lib/tot.rb', line 40

def refresh
  load_file
  self
end

#saveObject



45
46
47
48
49
# File 'lib/tot.rb', line 45

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) ⇒ Object

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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/tot.rb', line 103

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