-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_manager.rb
More file actions
60 lines (49 loc) · 1.42 KB
/
data_manager.rb
File metadata and controls
60 lines (49 loc) · 1.42 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
require_relative 'book'
require_relative 'person'
require_relative 'rental'
require 'json'
class DataManager
def self.save_data(books, people, rentals)
data = {
books: books.map(&:to_hash),
people: people.map(&:to_hash),
rentals: rentals.map(&:to_hash)
}
File.write('books.json', data[:books].to_json)
File.write('people.json', data[:people].to_json)
File.write('rentals.json', data[:rentals].to_json)
end
def self.load_data
puts 'Loading data...'
books = load_books
people = load_people
rentals = load_rentals(books, people)
[books, people, rentals]
end
def self.load_books
return [] unless File.exist?('books.json')
data = JSON.parse(File.read('books.json'))
data.map { |book| Book.from_hash(book) }
end
def self.load_people
return [] unless File.exist?('people.json')
data = JSON.parse(File.read('people.json'))
data.map do |person|
case person['type']
when 'Student'
Student.from_hash(person)
when 'Teacher'
Teacher.from_hash(person)
end
end
end
def self.load_rentals(books, people)
return [] unless File.exist?('rentals.json')
data = JSON.parse(File.read('rentals.json'))
data.map do |rental|
person = people.find { |p| p.id == rental['person_id'] }
book = books.find { |b| b.title == rental['book_title'] }
Rental.new(rental['date'], book, person)
end
end
end