-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtodo_list.rb
More file actions
48 lines (45 loc) · 771 Bytes
/
todo_list.rb
File metadata and controls
48 lines (45 loc) · 771 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class TodoItem
attr_accessor :description
def initialize(description, done=false)
@description = description
@done = done
end
def done?
@done
end
def done!
@done = true
end
end
class TodoList
attr_reader :items, :color, :name
def initialize(name, opts = {})
@name = name
@color = opts[:color]
@items = []
end
def add(item)
if item.is_a? String
@items << TodoItem.new(item)
else
@items << item
end
end
def items_pending
items.select {|e| not e.done?}
end
def items_done
items.select {|e| e.done?}
end
def find_by_description(description)
items.find {|e| e.description == description}
end
def set_as_done(description)
item = self.find_by_description(description)
if item
item.done!
else
false
end
end
end