-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_loader.rb
More file actions
81 lines (71 loc) · 2.17 KB
/
file_loader.rb
File metadata and controls
81 lines (71 loc) · 2.17 KB
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
52
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
class FileLoader
def load_books
if File.exist?('books.json')
file = File.read('books.json')
data = JSON.parse(file)
result = []
data.each do |book|
result << Book.new(book['title'], book['author'])
end
else
result = []
end
result
end
def load_people
if File.exist?('people.json')
file = File.read('people.json')
data = JSON.parse(file)
result = []
data.each do |person|
result << if person.key?('parent_permission')
Student.new(nil, person['age'], person['name'], person['parent_permission'])
else
Teacher.new(person['specialization'], person['age'], person['name'])
end
end
else
result = []
end
result
end
def load_rentals(books, people)
result = []
return result unless File.exist?('rentals.json')
file = File.read('rentals.json')
data = JSON.parse(file)
data.each do |rental|
book_title = rental['book']
person_name = rental['person']
book = books.find { |bk| bk.title == book_title }
person = people.find { |pple| pple.name == person_name }
if book && person
result << Rental.new(rental['date'], book, person)
else
log_missing_entities(rental, book, person)
end
end
result
end
def log_missing_entities(rental, book, person)
puts "Book not found for rental: #{rental.inspect}" unless book
puts "Person not found for rental: #{rental.inspect}" unless person
end
# def load_rentals(first, second)
# if File.exist?('rentals.json')
# file = File.read('rentals.json')
# data = JSON.parse(file)
# result = []
# data.each do |rental|
# book = first.find { |bk| bk.title == rental['book'] }
# person = second.find { |pple| pple.name == rental['person'] }
# puts "Book not found for rental: #{rental.inspect}" if book.nil?
# puts "Person not found for rental: #{rental.inspect}" if person.nil?
# result << Rental.new(rental['date'], book, person) if book && person
# end
# else
# result = []
# end
# result
# end
end