-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from AndreaM2429/preserve-book/label
Preserve book/label
- Loading branch information
Showing
9 changed files
with
124 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require 'json' | ||
require_relative 'book' | ||
|
||
class PreserveBooks | ||
def gets_books(genres, authors, labels) | ||
return unless File.exist?('./books.json') | ||
|
||
saved_books = [] | ||
file = File.read('./books.json') | ||
data = JSON.parse(file) | ||
|
||
data.each do |element| | ||
genre = genres.select { |gen| gen.name == element['genre'] } | ||
author = authors.select { |aut| aut.first_name == element['author'] } | ||
label = labels.select { |lab| lab.title == element['label'] } | ||
|
||
book = Book.new(publish_date: element['publish_date'], publisher: element['publisher'], | ||
cover_state: element['cover_state'], id: element['id']) | ||
book.genre = genre[0] | ||
book.author = author[0] | ||
book.label = label[0] | ||
book.archived = element['archived'] | ||
|
||
saved_books << book | ||
end | ||
saved_books | ||
end | ||
|
||
def save_books(books) | ||
return if books.empty? | ||
|
||
data_hash = [] | ||
|
||
books.each do |book| | ||
data_hash << { id: book.id, publish_date: book.publish_date, | ||
archived: book.archived, genre: book.genre, author: book.author.first_name, | ||
label: book.label.title, publisher: book.publisher, cover_state: book.cover_state } | ||
end | ||
|
||
File.write('./books.json', JSON.dump(data_hash)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'json' | ||
require_relative 'label' | ||
|
||
class PreserveLabels | ||
def gets_labels | ||
return unless File.exist?('./labels.json') | ||
|
||
saved_labels = [] | ||
file_data = File.read('./labels.json') | ||
data = JSON.parse(file_data) | ||
data.each { |elem| saved_labels << Label.new(elem['title'], elem['color'], elem['id']) } | ||
saved_labels | ||
end | ||
|
||
def save_labels(labels) | ||
return if labels.empty? | ||
|
||
data_hash = [] | ||
labels.each do |label| | ||
data_hash << { id: label.id, title: label.title, color: label.color, items_class: label.items[0].class } | ||
end | ||
File.write('./labels.json', JSON.dump(data_hash)) | ||
end | ||
end |