-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_save.rb
More file actions
83 lines (67 loc) · 2.11 KB
/
file_save.rb
File metadata and controls
83 lines (67 loc) · 2.11 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
82
83
public
def save_books
books = []
@books.each do |book|
b = {
'title' => book.title,
'author' => book.author
}
books.push(b)
end
File.write('./data/books.json', JSON.generate(books), mode: 'w')
end
def save_people
people = []
@people.each do |person|
p = if person.instance_of?(Teacher)
{ 'name' => person.name, 'specialization' => person.specialization, 'age' => person.age,
'parent_permission' => person.parent_permission, 'id' => person.id }
else
{ 'name' => person.name, 'age' => person.age,
'parent_permission' => person.parent_permission, 'id' => person.id }
end
people.push(p)
end
File.write('./data/people.json', JSON.generate(people), mode: 'w')
end
def save_rentals
rentals = []
@rentals.each do |rental|
r = {
'book' => { 'title' => rental.book.title, 'author' => rental.book.author },
'person' => { 'name' => rental.person.name, 'age' => rental.person.age, 'id' => rental.person.id },
'date' => rental.date
}
rentals.push(r)
end
File.write('./data/rentals.json', JSON.generate(rentals), mode: 'w')
end
def read_book_data
return unless File.exist?('./data/books.json')
books = JSON.parse(File.read('./data/books.json'))
books.each do |book|
@books.push(Book.new(book['title'], book['author']))
end
end
def read_person_data
return unless File.exist?('./data/people.json')
people = JSON.parse(File.read('./data/people.json'))
people.each do |person|
if person['specialization']
@people.push(Teacher.new(person['specialization'], person['name'], person['age']))
else
@people.push(Student.new(person['name'], person['age']))
end
end
end
def read_rental_data
return unless File.exist?('./data/rentals.json')
rentals = JSON.parse(File.read('./data/rentals.json'))
rentals.each do |rental|
b = Book.new(rental['book']['title'], rental['book']['author'])
pr = Person.new(rental['person']['age'], rental['person']['name'])
pr.id = rental['person']['id']
date = rental['date']
@rentals.push(Rental.new(b, pr, date))
end
end