Skip to content

Commit

Permalink
restructure files
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasesquivelgc committed Oct 22, 2023
1 parent 4b498c0 commit aa46ffe
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 42 deletions.
2 changes: 1 addition & 1 deletion app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize
@albums = load_albums_from_json
@books = load_books_from_json
@games = load_games_from_json

@genres = load_genres_from_json
@labels = load_labels_from_json
@authors = load_authors_from_json
Expand Down
2 changes: 1 addition & 1 deletion data/albums.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"publish_date":"1234-11-11","id":"d666134c-2121-4a7f-8532-e8cd0f88d7d1","archived":false,"author":{"id":"ffca4b1e-c8f4-43ef-adc6-8b3f22199b0c","first_name":"Ubisoft","last_name":""},"genre":{"id":"6739afba-fe48-4c66-83fd-b8dbee114bf7","name":"Action"},"label":{"id":414,"title":null,"color":"Blue"},"on_spotify":false}
{"publish_date":"1234-11-11","id":"d666134c-2121-4a7f-8532-e8cd0f88d7d1","archived":false,"author":{"id":"a68452ee-a07b-4967-9951-0d995c551e7e","first_name":"Ubisoft","last_name":""},"genre":{"id":"65c0318c-fb1c-4dd8-b885-06848e4c0c24","name":"Action"},"label":{"id":262,"title":null,"color":"Blue"},"on_spotify":false}
2 changes: 1 addition & 1 deletion data/books.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"publisher":"Me","cover_state":"good","publish_date":"1234-11-11","id":"a29e4867-8318-48c9-b9a7-5fa65125993f","archived":false,"author":{"id":"df362a90-0ea2-4432-a007-817c4ef80670","first_name":"Ubisoft","last_name":""},"genre":{"id":"5a600be1-d634-4060-b07e-b282a3ae1ae8","name":"Action"},"label":{"id":343,"title":null,"color":"Blue"}}
{"publisher":"Me","cover_state":"good","publish_date":"1234-11-11","id":"a29e4867-8318-48c9-b9a7-5fa65125993f","archived":false,"author":{"id":"03dfd534-6419-4ae4-9894-83d52c3bc404","first_name":"Ubisoft","last_name":""},"genre":{"id":"666a7889-a305-445c-9129-256f2d48f9bc","name":"Action"},"label":{"id":48,"title":null,"color":"Blue"}}
2 changes: 1 addition & 1 deletion data/games.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"publish_date":"1234-11-11","archived":false,"author":{"id":"6abf5473-ef10-42a8-97d7-64d27a21eaf5","first_name":"Ubisoft","last_name":""},"genre":{"id":"085513c2-6cd5-4442-9ab0-bd31f2aaf5a5","name":"Action"},"label":{"id":281,"title":null,"color":"Blue"},"multiplayer":"n","last_played_at":"2023-02-02","id":"37bd485f-8991-4ce9-a911-23ed74edcbfc"}
{"publish_date":"1234-11-11","archived":false,"author":{"id":"fa6511e0-1789-4d7f-ae81-86b8fd730e9f","first_name":"Ubisoft","last_name":""},"genre":{"id":"dbff3ab5-e146-4418-9ea9-a69b7c94d2a0","name":"Action"},"label":{"id":20,"title":null,"color":"Blue"},"multiplayer":"n","last_played_at":"2023-02-02","id":"37bd485f-8991-4ce9-a911-23ed74edcbfc"}
14 changes: 7 additions & 7 deletions modules/decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def self.decorate(item, authors, genres, labels)
if authors.any? { |author| author.first_name == author_first_name && author.last_name == author_last_name }
item.author = authors.find { |author| author.first_name == author_first_name && author.last_name == author_last_name }
else
item.author = Author.new(author_first_name, author_last_name)
authors << item.author
item.author = Author.new(author_first_name, author_last_name)
authors << item.author
end

puts 'name of the genre is:'
Expand All @@ -22,10 +22,10 @@ def self.decorate(item, authors, genres, labels)
if genres.any? { |genre| genre.name == genre_name }
item.genre = genres.find { |genre| genre.name == genre_name }
else
item.genre = Genre.new(genre_name)
genres << item.genre
item.genre = Genre.new(genre_name)
genres << item.genre
end

puts 'title of the label is:'
label_title = gets.chomp
puts 'color of the label is:'
Expand All @@ -34,8 +34,8 @@ def self.decorate(item, authors, genres, labels)
if labels.any? { |label| label.title == label_title && label.color == label_color }
item.label = labels.find { |label| label.title == label_title && label.color == label_color }
else
item.label = Label.new(title: label_title, color: label_color)
labels << item.label
item.label = Label.new(title: label_title, color: label_color)
labels << item.label
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def self.load_genres(games, albums, books)
game_items = games.select { |game| data['items'].include?('id' => game.id, 'class' => 'Game') }
album_items = albums.select { |album| data['items'].include?('id' => album.id, 'class' => 'MusicAlbum') }
book_items = books.select { |book| data['items'].include?('id' => book.id, 'class' => 'Book') }

# Assign the corresponding arrays to the 'items' property of the Genre object
genre.items = game_items + album_items + book_items
genres << genre
Expand All @@ -52,4 +52,4 @@ def self.load_labels(games, albums, books)
end
labels
end
end
end
2 changes: 1 addition & 1 deletion modules/load_items.rb → modules/load_logic/load_items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ def self.load_albums
end
albums
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def self.authors_to_h(items)
{
id: item.id,
class: item.class.to_s
}
}
end
end
end
end
25 changes: 12 additions & 13 deletions modules/save_files.rb → modules/save_logic/save_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@
require_relative 'save_categories'

module SaveFiles
include SaveItems
include SaveCategories
def self.erase_previous_data
files_to_clear = ['data/albums.json', 'data/authors.json', 'data/labels.json', 'data/genres.json', 'data/books.json', 'data/games.json']

begin
files_to_clear.each do |file_path|
File.open(file_path, 'w') do |file|
file.truncate(0)
include SaveItems
include SaveCategories
def self.erase_previous_data
files_to_clear = ['data/albums.json', 'data/authors.json', 'data/labels.json', 'data/genres.json', 'data/books.json', 'data/games.json']

begin
files_to_clear.each do |file_path|
File.open(file_path, 'w') do |file|
file.truncate(0)
end
end
rescue StandardError => e
puts "An error occurred: #{e.message}"
end
puts "Content in JSON files erased successfully."
rescue StandardError => e
puts "An error occurred: #{e.message}"
end
end

def self.save_albums(albums)
albums.each do |album|
Expand Down
2 changes: 1 addition & 1 deletion modules/save_items.rb → modules/save_logic/save_items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ def self.save_game(game)
file.puts(data.to_json)
end
end
end
end
25 changes: 13 additions & 12 deletions options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
require_relative 'classes/item'
require_relative 'modules/decorator'
require_relative 'modules/list'
require_relative 'modules/load_items'
require_relative 'modules/load_categories'
require_relative 'modules/save_files'
require_relative 'modules/load_logic/load_items'
require_relative 'modules/load_logic/load_categories'
require_relative 'modules/save_logic/save_files'
require 'colorize'
require 'json'

Expand Down Expand Up @@ -252,14 +252,22 @@ def load_albums_from_json
end

def load_games_from_json
LoadItems.load_games
end
LoadItems.load_games
end

def show_error
puts 'Error! Please select a valid option.'
end

def quit
SaveFiles.erase_previous_data
SaveFiles.save_books(@books)
SaveFiles.save_albums(@albums)
SaveFiles.save_games(@games)
SaveFiles.save_authors(@authors)
SaveFiles.save_genres(@genres)
SaveFiles.save_labels(@labels)
puts 'Data saved.'
puts "
░██████╗░░█████╗░░█████╗░██████╗░██████╗░██╗░░░██╗███████╗██╗
Expand All @@ -268,13 +276,6 @@ def quit
██║░░╚██╗██║░░██║██║░░██║██║░░██║██╔══██╗░░╚██╔╝░░██╔══╝░░╚═╝
╚██████╔╝╚█████╔╝╚█████╔╝██████╔╝██████╦╝░░░██║░░░███████╗██╗
░╚═════╝░░╚════╝░░╚════╝░╚═════╝░╚═════╝░░░░╚═╝░░░╚══════╝╚═╝".colorize(color: :light_blue, mode: :bold)
SaveFiles.erase_previous_data
SaveFiles.save_books(@books)
SaveFiles.save_albums(@albums)
SaveFiles.save_games(@games)
SaveFiles.save_authors(@authors)
SaveFiles.save_genres(@genres)
SaveFiles.save_labels(@labels)
exit
end
end

0 comments on commit aa46ffe

Please sign in to comment.