Class: Todo::List

Inherits:
Object
  • Object
show all
Defined in:
lib/to-do/old/list.rb

Overview

The Class that represents a list of tasks

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ List

Creates a new list and sets it to be the working list

Parameters:

  • name (String)

    the name of the list



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/to-do/old/list.rb', line 12

def initialize name
  @tasks = Hash.new
  @completed_tasks = Hash.new
  @count = 0
  @completed_count = 0
  @name = name
  if !File.exists? Config[:lists_directory]
    Dir.mkdir(Config[:lists_directory])
  end
  update
  Config[:working_list_name] = name.downcase.gsub(/ /, '_')
  Config[:working_list_exists] = true
  Config.write
  puts "Created List #{name}."
end

Instance Attribute Details

#completed_countObject

Returns the value of attribute completed_count.



7
8
9
# File 'lib/to-do/old/list.rb', line 7

def completed_count
  @completed_count
end

#completed_tasksObject

Returns the value of attribute completed_tasks.



7
8
9
# File 'lib/to-do/old/list.rb', line 7

def completed_tasks
  @completed_tasks
end

#countObject

Returns the value of attribute count.



7
8
9
# File 'lib/to-do/old/list.rb', line 7

def count
  @count
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/to-do/old/list.rb', line 7

def name
  @name
end

#tasksObject

Returns the value of attribute tasks.



7
8
9
# File 'lib/to-do/old/list.rb', line 7

def tasks
  @tasks
end

Class Method Details

.remove(name) ⇒ Object

Class method that removes a list from the your lists.

Parameters:

  • name (string)

    name of the list that you are trying to remove



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/to-do/old/list.rb', line 146

def self.remove name
  underscore_name = name.downcase.gsub(/ /, '_')
  begin
    FileUtils.rm File.join(Config[:lists_directory], underscore_name +'.yml')
    puts "Removed list #{name}"
  rescue
    puts "List doesn't exist"
  end
  if underscore_name == Config[:working_list_name]
    Config[:working_list_exists] = false
  end
end

Instance Method Details

#add(task) ⇒ Object

Adds the tast to the list

Parameters:

  • task (String)

    the task to add to the list



39
40
41
42
43
44
# File 'lib/to-do/old/list.rb', line 39

def add task
  @count+=1
  @tasks[@count] = task
  puts "Added task #{task}."
  update
end

#clear(clear_all) ⇒ Object

clears the task in the list

and resets the count. if false, just clears the completed tasks

Parameters:

  • clear_all (Bool)

    if true, clears all completed and uncompleted tasks



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/to-do/old/list.rb', line 130

def clear clear_all
  clear_completed
  if clear_all
    @tasks = Hash.new
    @completed_count = 0
    @count = 0
    puts "Cleared list."
  else
    puts "Cleared completed tasks."
  end
  update
end

#clear_completedObject

clears just the completed tasks



121
122
123
124
# File 'lib/to-do/old/list.rb', line 121

def clear_completed
  @completed_tasks = Hash.new
  update
end

#finish(task, is_num) ⇒ Object

finish the task. task is either a case insensitive task on the list or the task number. Prints out either the task is not in the list or that i succesfully finished the task

false if it is the task name

Parameters:

  • task

    either a task number or task name to finish

  • is_num (Bool)

    if the task param represents the task number, true.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/to-do/old/list.rb', line 53

def finish task, is_num
  if is_num
    if !@tasks[task.to_i].nil?
      @completed_tasks[task.to_i] = @tasks[task.to_i]
      @tasks.delete(task.to_i)
      @completed_count+=1
      @completed_tasks = Hash[@completed_tasks.sort]
      puts "Finished #{@completed_tasks[task.to_i]}."
    else
      puts "Task \##{task} not in list."
    end
  else
    hash = Hash.new
    @tasks.each do |k,v|
      hash[k] = v.downcase
    end
    if hash.value?(task.downcase)
      num = hash.key(task.downcase)
      @completed_tasks[num] = @tasks[num]
      @tasks.delete(num)
      @completed_count+=1
      @completed_tasks = Hash[@completed_tasks.sort]
      puts "Finished #{@completed_tasks[num]}."
    else
      puts "Task #{task} is not in list."
    end
  end
  update
end

#undo(task, is_num) ⇒ Object

undos finishing a task. task is either a case insensitive task on the list or the task number. Prints out either the task is not in the list or that i succesfully undoed finished the task

false if it is the task name

Parameters:

  • task

    either a task number or task name to finish

  • is_num (Bool)

    if the task param represents the task number, true.



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
# File 'lib/to-do/old/list.rb', line 90

def undo task, is_num
  if is_num
    if !@completed_tasks[task.to_i].nil?
      @tasks[task.to_i] = @completed_tasks[task.to_i]
      @completed_tasks.delete(task.to_i)
      @completed_count-=1
      @tasks = Hash[@tasks.sort]
      puts "Undo completeing #{@tasks[task.to_i]}."
    else
      puts "Task \##{task} not in list."
    end
  else
    hash = Hash.new
    @completed_tasks.each do |k,v|
      hash[k] = v.downcase
    end
    if hash.value?(task.downcase)
      num = hash.key(task.downcase)
      @tasks[num] = @completed_tasks[num]
      @completed_tasks.delete(num)
      @completed_count-=1
      @tasks = Hash[@tasks.sort]
      puts "Undo completeing #{@tasks[num]}."
    else
      puts "Task #{task} is not in list."
    end
  end
  update
end

#updateObject

Updates the yaml file



29
30
31
32
33
34
# File 'lib/to-do/old/list.rb', line 29

def update
  path = File.join(Config[:lists_directory], @name.downcase.gsub(/ /, '_') +'.yml')
  File.open(path, 'w') do |fh|
    fh.puts(self.to_yaml)
  end
end