-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto_do_list.rb
51 lines (51 loc) · 1.84 KB
/
to_do_list.rb
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
49
50
51
require_relative 'to_do'
require_relative 'list_manager'
class ToDoList
include ListManager
attr_accessor :name, :list
def initialize(name="To-Do")
@name = name
@list = []
end
class << self
# search ToDoList names and ToDo parameters
def find(identifier)
all_to_do_lists = []
found_list_names = []
found_lists = []
found_to_dos = []
# populate 'all_to_do_lists' array w/ all to-do lists
ObjectSpace.each_object(self) do |instance|
all_to_do_lists << instance
end
all_to_do_lists.each do |to_do_list|
# populate 'found_list_names' array w/ list names that match 'identifier'
found_list_names << to_do_list if to_do_list.name == identifier
to_do_list.list.each do |to_do|
# populate 'found_lists' array w/ to-do lists that match 'identifier'
found_lists << to_do_list if to_do.values.include?(identifier)
# populate 'found_to_dos' array of to-do's that match 'identifier'
found_to_dos << to_do if to_do.values.include?(identifier)
end
end
# output user message(s) based on results
case identifier
when found_list_names.each do |to_do_list|
# output when a list name matches 'identifier'
puts "A list named '#{identifier}' "\
"was found." if to_do_list.name == identifier
end
when found_lists.each do |to_do_list|
# output when any item within a list matches 'identifier'
puts "An item matching '#{identifier}' "\
"was found in a list named "\
"'#{to_do_list.name}.'" if to_do_list.list.each do |to_do|
to_do.values.include?(identifier)
end
end
end
# output when nothing is found
puts "'#{identifier}' not found." if (found_list_names + found_lists + found_to_dos).count == 0
end
end
end