lib/alexandria/models/library.rb in alexandria-book-collection-manager-0.7.1 vs lib/alexandria/models/library.rb in alexandria-book-collection-manager-0.7.2

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + # Copyright (C) 2004-2006 Laurent Sansonetti # Copyright (C) 2014 Matijs van Zuijlen # # Alexandria is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as @@ -128,11 +130,10 @@ test[1] = savedfilename # retries the Dir.each block... # but gives up after three tries redo unless test[0] > 2 - else test = [0, nil] end end @@ -191,11 +192,11 @@ new_yaml.sub!(/^\s*\-+\s*/, '') text.sub!(md[0], "loaned_since: #{new_yaml}\n") end # TODO: Ensure book loading passes through Book#initialize - book = YAML.load(text) + book = YAML.safe_load(text, whitelist_classes = [Book, Time]) unless book.isbn.class == String # HACK md = /isbn: (.+)/.match(text) if md @@ -229,11 +230,10 @@ # Skip non-directory files. next unless File.stat(File.join(DIR, file)).directory? a << load(file) end - rescue Errno::ENOENT FileUtils.mkdir_p(DIR) end # Create the default library if there is no library yet. @@ -244,13 +244,11 @@ def self.move(source_library, dest_library, *books) dest = dest_library.path books.each do |book| FileUtils.mv(source_library.yaml(book), dest) - if File.exist?(source_library.cover(book)) - FileUtils.mv(source_library.cover(book), dest) - end + FileUtils.mv(source_library.cover(book), dest) if File.exist?(source_library.cover(book)) source_library.changed source_library.old_delete(book) source_library.notify_observers(source_library, BOOK_REMOVED, @@ -363,13 +361,11 @@ end end def self.canonicalise_isbn(isbn) numbers = extract_numbers(isbn) - if valid_ean?(isbn) && (numbers[0..2] != [9, 7, 8]) - return isbn - end + return isbn if valid_ean?(isbn) && (numbers[0..2] != [9, 7, 8]) canonical = if valid_ean?(isbn) # Looks like an EAN number -- extract the intersting part and # calculate a checksum. It would be nice if we could validate # the EAN number somehow. numbers[3..11] + [isbn_checksum(numbers[3..11])] @@ -390,13 +386,11 @@ def simple_save(book) # Let's initialize the saved identifier if not already # (backward compatibility from 0.4.0) # book.saved_ident ||= book.ident - if book.saved_ident.nil? || book.saved_ident.empty? - book.saved_ident = book.ident - end + book.saved_ident = book.ident if book.saved_ident.nil? || book.saved_ident.empty? if book.ident != book.saved_ident # log.debug { "Backwards compatibility step: #{book.saved_ident.inspect}, #{book.ident.inspect}" } FileUtils.rm(yaml(book.saved_ident)) end if File.exist?(cover(book.saved_ident)) @@ -419,13 +413,11 @@ # (backward compatibility from 0.4.0). book.saved_ident ||= book.ident if book.ident != book.saved_ident FileUtils.rm(yaml(book.saved_ident)) - if File.exist?(cover(book.saved_ident)) - FileUtils.mv(cover(book.saved_ident), cover(book.ident)) - end + FileUtils.mv(cover(book.saved_ident), cover(book.ident)) if File.exist?(cover(book.saved_ident)) # Notify before updating the saved identifier, so the views # can still use the old one to update their models. notify_observers(self, BOOK_UPDATED, book) unless final book.saved_ident = book.ident @@ -465,13 +457,11 @@ io.puts transport.get(uri) end end # Remove the file if its blank. - if Alexandria::UI::Icons.blank?(cover_file) - File.delete(cover_file) - end + File.delete(cover_file) if Alexandria::UI::Icons.blank?(cover_file) end end @@deleted_libraries = [] @@ -499,11 +489,11 @@ # Delete the whole library. raise if @@deleted_libraries.include?(self) @@deleted_libraries << self else if @deleted_books.include?(book) - doubles = @deleted_books.reject { |b| !b.equal?(book) } + doubles = @deleted_books.select { |b| b.equal?(book) } raise ArgumentError, "Book #{book.isbn} was already deleted" unless doubles.empty? end @deleted_books << book i = index(book) # We check object IDs there because the user could have added @@ -608,10 +598,10 @@ File.join(somewhere, final_cover(book))) end end def self.jpeg?(file) - 'JFIF' == IO.read(file, 10)[6..9] + IO.read(file, 10)[6..9] == 'JFIF' end def final_cover(book) # TODO: what about PNG? book.ident + (Library.jpeg?(cover(book)) ? '.jpg' : '.gif')