generated from microverseinc/readme-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_save.rb
64 lines (59 loc) · 1.41 KB
/
json_save.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
52
53
54
55
56
57
58
59
60
61
62
63
64
require 'json'
module Save
def save_files
save_books
save_people
save_rentals
end
def save_books
books_json = @books.map do |book|
{
title: book.title,
author: book.author
}
end
File.write('./books.json', JSON.generate(books_json))
end
def save_people
people_json = []
@people.map do |person|
person_json = {
id: person.id,
name: person.name,
age: person.age
}
append_data(person, person_json)
people_json.push(person_json)
end
File.write('./people.json', JSON.generate(people_json))
end
def save_rentals
rentals_json = []
@rentals.map do |rental|
rental_json = {
book: {
title: rental.book.title,
author: rental.book.author
},
person: {
id: rental.person.id,
name: rental.person.name,
age: rental.person.age
},
date: rental.date
}
append_data(rental.person, rental_json[:person])
rentals_json.push(rental_json)
end
File.write('./rentals.json', JSON.generate(rentals_json))
end
def append_data(person, person_json)
if person.instance_of?(::Teacher)
person_json[:specialization] = person.specialization
else
person_json[:parent_permission] = person.parent_permission
person_json[:classroom] = person.classroom.label
end
person_json
end
end