forked from AndreaM2429/Catalog_App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preserve_album_genre.rb
76 lines (66 loc) · 1.99 KB
/
preserve_album_genre.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
65
66
67
68
69
70
71
72
73
74
75
76
require_relative 'app'
require_relative 'music_album'
require_relative 'genre'
require 'json'
class PreserveAlbumGenre
attr_reader :albums, :genres
def initialize(albums, genres)
@albums = albums
@genres = genres
end
def save_music_albums
return if @albums.empty?
album_data = @albums.map do |album|
{
id: album.id,
publish_date: album.publish_date,
on_spotify: album.on_spotify,
archived: album.archived,
genre: album.genre,
label: {
title: album.label.title,
color: album.label.color
},
author: {
first_name: album.author.first_name,
last_name: album.author.last_name
}
}
end
File.write('albums.json', JSON.pretty_generate(album_data))
end
def load_music_albums
return unless File.exist?('albums.json')
album_data = JSON.parse(File.read('albums.json'))
album_data.each do |album|
new_album = MusicAlbum.new(publish_date: album['publish_date'], on_spotify: album['on_spotify'])
genre = Genre.new(album['genre'])
label = Label.new(album['label']['title'], album['label']['color'])
author = Author.new(first_name: album['author']['first_name'], last_name: album['author']['last_name'])
new_album.author = author
new_album.id = album['id']
new_album.genre = genre
new_album.label = label
new_album.archived = album['archived']
@albums << new_album
end
end
def save_genres
return if @genres.empty?
genre_data = @genres.map do |genre|
{
id: genre.id,
name: genre.name
}
end
File.write('genres.json', JSON.pretty_generate(genre_data))
end
def load_genres
return unless File.exist?('genres.json')
genre_data = JSON.parse(File.read('genres.json'))
genre_data.each do |genre|
existing_genre = @genres.find { |g| g.id == genre['id'] }
@genres << Genre.new(genre['name'], genre['id']) unless existing_genre
end
end
end