Class: Rudoo::List

Inherits:
Object
  • Object
show all
Defined in:
lib/rudoo/list.rb

Instance Method Summary collapse

Constructor Details

#initializeList

create a new list object by loading the yaml file



5
6
7
8
9
10
# File 'lib/rudoo/list.rb', line 5

def initialize
  @list = ".rudoo/list.yml"
  @config = ".rudoo/config.yml"
  @tasks = {}
  load
end

Instance Method Details

#add(todo, priority = 0) ⇒ Object

method for adding task objects to the list



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rudoo/list.rb', line 13

def add(todo, priority = 0)
  if todo.class == Rudoo::Task
    task = todo
  else
    task = Rudoo::Task.new(todo, priority)
  end

  task = convert_date(task)
  @tasks[task.id] = task
  write

  return task 
end

#commitObject

runs git commit with the current task as the



226
227
228
229
230
# File 'lib/rudoo/list.rb', line 226

def commit
  current_todo = strip_tags(current.values[0].todo)

  `git commit -am "#{current_todo}"`
end

#complete(task_id) ⇒ Object

complete an existing task



53
54
55
56
57
58
59
60
61
# File 'lib/rudoo/list.rb', line 53

def complete(task_id)
  previous = @tasks[task_id]
  previous.todo = previous.todo.gsub(" :wkng", "")
  previous.todo = previous.todo.gsub(" :done", "")
  
  edit(task_id, "#{previous.todo} :done")

  return true
end

#currentObject

returns the task (marked with :wkng)



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/rudoo/list.rb', line 179

def current
  current = @tasks.dup.select do |id, task|
    task.todo.include?(":wkng")
  end 

  if current == {}
    return false
  else
    return current
  end
end

#delete(task_id) ⇒ Object

method for deleting task objects from the list



28
29
30
31
32
33
34
# File 'lib/rudoo/list.rb', line 28

def delete(task_id)
  if @tasks.delete(task_id)
    write
  else
    raise StandardError.new("ERROR: Unable to delete objective from the hash!")
  end
end

#demote(task_id) ⇒ Object

Method to decrease the priority of a task



155
156
157
158
159
160
161
# File 'lib/rudoo/list.rb', line 155

def demote(task_id)
  previous = @tasks[task_id]
  previous.priority = previous.priority.to_i - 1 unless previous.priority.to_i <= 0
  write

  return true
end

#done?(task) ⇒ Boolean

check if a task is finished or not

Returns:

  • (Boolean)


76
77
78
# File 'lib/rudoo/list.rb', line 76

def done?(task)
  task.todo.include?(":done")
end

#edit(task_id, message = nil, priority = nil) ⇒ Object

method for editing existing tasks given their id



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rudoo/list.rb', line 37

def edit(task_id, message = nil, priority = nil)
  old = @tasks[task_id]

  message = old.todo if message.nil?
  priority = old.priority if priority.nil?

  if old
    new_task = Task.new(message, priority)
    delete(task_id)
    add(new_task)
  else
    return false
  end
end

#gather(args = [], state = "undone") ⇒ Object

returns a list of tasks that match the given argument criteria



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rudoo/list.rb', line 81

def gather(args = [], state = "undone")
  list = {}

  if @tasks.empty?
    list = {}
  else
    case args
    when []
      deleteThese = []
      addThese = []

      @tasks.each do |id, task|
        if (state == "undone" && !done?(task)) || (state == "done" && done?(task)) || (state == "all")
          if task.todo.include?("{#{Time.now.strftime('%b/%d/%Y')}}")
            replacedTask = task.dup
            replacedTask.todo.gsub("{#{Time.now.strftime('%b/%d/%Y')}}", "#{@tag_token}today")

            addThese << replacedTask
            deleteThese << id
          else
            list[task.id] = task
          end
        end
    end
      deleteThese.each do |id|
        delete(id)
      end

      addThese.each do |task|
        add(task)
        list[task.id] = task
      end
    when @person_token, @tag_token, @project_token
      tags = Hash.new(0)

      @tasks.each do |id, task|
        if (state == "undone" && !done?(task)) || (state == "done" && done?(task)) || (state == "all")
          task.todo.gsub(/(#{"\\" + args}\w+)/).each do |tag|
            tags[tag] += 1
          end
        end
      end

      tags.each {|tag, number| puts "#{tag} (#{number})"}
    else
      if args.any? {|arg| arg.downcase.include?("tomorrow")}
        args << Chronic.parse("tomorrow").strftime('%b/%d/%Y')
        args.delete("tomorrow")
        args.delete("#{@tag_token}tomorrow")
      end

      @tasks.each do |id, task|
        if (state == "undone" && !done?(task)) || (state == "done" && done?(task)) || (state == "all")
          args.each do |term| 
            list[task.id] = task if task.todo.downcase.include?(term.downcase)
          end
        end
      end
    end
  end

  print_to_screen list unless list == {}
end

#open(task_id) ⇒ Object

re-open a completed task (make it incomplete again)



64
65
66
67
68
69
70
71
72
73
# File 'lib/rudoo/list.rb', line 64

def open(task_id)
  previous = @tasks[task_id]
  
  if previous
    edit(task_id, previous.todo.gsub(" :done", ""))
    return true
  else
    return false
  end
end

#people(type) ⇒ Object

returns all of the people mentioned



164
165
166
# File 'lib/rudoo/list.rb', line 164

def people(type)
  gather(@person_token, type)
end

method to order tasks by priority and arrange them in a table



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/rudoo/list.rb', line 233

def print_to_screen(init_hash)
    priority_hash = {}
    hash = {}
    
    init_hash.values.each do |task|
      priority_hash[task] = task.priority.to_i
    end

    priority_hash = priority_hash.sort_by {|k, v| v}.reverse

    priority_hash.each do |task, priority|
      hash[task.id] = task
    end

    longest_todo = hash.values.sort_by {|x| x.todo.to_s.length}.reverse[0].todo.length
    longest_priority = hash.values.sort_by {|x| x.priority.to_s.length}.reverse[0].priority.to_s.length
    array_length = hash.count

    ids = hash.keys
    tasks = hash.values

    ((array_length*2)+1).times do |x|
      if x%2 == 0
        puts "+--------+-#{"-"*longest_todo}-+-#{"-"*longest_priority}-+"
      else
        task = tasks[(x-1)/2]
        puts "| #{ids[(x-1)/2]} | #{task.todo}#{" "*(longest_todo - task.todo.length)} | #{task.priority}#{" "*(longest_priority - task.priority.to_s.length)} |"
      end
    end
end

#projects(type) ⇒ Object

returns all of the projects



174
175
176
# File 'lib/rudoo/list.rb', line 174

def projects(type)
  gather(@project_token, type)
end

#promote(task_id) ⇒ Object

Method to increase the priority of a task



146
147
148
149
150
151
152
# File 'lib/rudoo/list.rb', line 146

def promote(task_id)
  previous = @tasks[task_id]
  previous.priority = previous.priority.to_i + 1
  write

  return true
end

#set_current(task_id) ⇒ Object

Method to make a task current given its id



192
193
194
195
196
197
198
199
200
201
# File 'lib/rudoo/list.rb', line 192

def set_current(task_id)
  previous = @tasks[task_id]

  previous.todo = previous.todo.gsub(" :wkng", "")
  previous.todo = previous.todo.gsub(" :done", "")
  
  edit(task_id, "#{previous.todo} :wkng")

  return true
end

#tags(type) ⇒ Object

returns all of the tags



169
170
171
# File 'lib/rudoo/list.rb', line 169

def tags(type)
  gather(@tag_token, type)
end

#to_md(file, state = "undone") ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/rudoo/list.rb', line 203

def to_md(file, state="undone")
  file = File.open(File.expand_path(file), "a")

  file.puts ""
  file.puts ""
  file.puts "## TODO LIST"
  file.puts "  *Items in bold are undone. Items in normal font are already completed*"
  file.puts ""

  @tasks.each do |id, task|
    if (state == "undone" && !done?(task)) || (state == "done" && done?(task)) || (state == "all")
      todo = strip_tags(task.todo)

      if done?(task)
        file.puts "* #{todo}"
      else
        file.puts "* **#{todo}**"
      end
    end
  end
end