lib/alexandria/models/library.rb in alexandria-book-collection-manager-0.7.3 vs lib/alexandria/models/library.rb in alexandria-book-collection-manager-0.7.4

- old
+ new

@@ -2,29 +2,29 @@ # This file is part of Alexandria. # # See the file README.md for authorship and licensing information. -require 'yaml' -require 'fileutils' -require 'rexml/document' -require 'tempfile' -require 'etc' -require 'alexandria/library_store' +require "yaml" +require "fileutils" +require "rexml/document" +require "tempfile" +require "etc" +require "alexandria/library_store" module Alexandria class Library < Array include Logging attr_reader :name attr_accessor :ruined_books, :updating, :deleted_books - DEFAULT_DIR = File.join(ENV['HOME'], '.alexandria') - EXT = { book: '.yaml', cover: '.cover' }.freeze + DEFAULT_DIR = File.join(ENV["HOME"], ".alexandria") + EXT = { book: ".yaml", cover: ".cover" }.freeze include GetText extend GetText - bindtextdomain(Alexandria::TEXTDOMAIN, charset: 'UTF-8') + bindtextdomain(Alexandria::TEXTDOMAIN, charset: "UTF-8") BOOK_ADDED, BOOK_UPDATED, BOOK_REMOVED = (0..3).to_a include Observable def path @@ -34,11 +34,11 @@ def updating? @updating end def self.generate_new_name(existing_libraries, - from_base = _('Untitled')) + from_base = _("Untitled")) i = 1 name = nil all_libraries = existing_libraries + @@deleted_libraries loop do name = i == 1 ? from_base : from_base + " #{i}" @@ -69,24 +69,24 @@ end def self.extract_numbers(entry) return [] if entry.nil? || entry.empty? - normalized = entry.delete('- ').upcase - return [] unless normalized =~ /\A[\dX]*\Z/ + normalized = entry.delete("- ").upcase + return [] unless /\A[\dX]*\Z/.match?(normalized) - normalized.split('').map do |char| - char == 'X' ? 10 : char.to_i + normalized.split("").map do |char| + char == "X" ? 10 : char.to_i end end def self.isbn_checksum(numbers) sum = (0...numbers.length).reduce(0) do |accumulator, i| accumulator + numbers[i] * (i + 1) end % 11 - sum == 10 ? 'X' : sum + sum == 10 ? "X" : sum end def self.valid_isbn?(isbn) numbers = extract_numbers(isbn) (numbers.length == 10) && isbn_checksum(numbers).zero? @@ -115,39 +115,39 @@ ((numbers.length == 17) && (upc_checksum(numbers[0..10]) == numbers[11])) end AMERICAN_UPC_LOOKUP = { - '014794' => '08041', '018926' => '0445', '02778' => '0449', - '037145' => '0812', '042799' => '0785', '043144' => '0688', - '044903' => '0312', '045863' => '0517', '046594' => '0064', - '047132' => '0152', '051487' => '08167', '051488' => '0140', - '060771' => '0002', '065373' => '0373', '070992' => '0523', - '070993' => '0446', '070999' => '0345', '071001' => '0380', - '071009' => '0440', '071125' => '088677', '071136' => '0451', - '071149' => '0451', '071152' => '0515', '071162' => '0451', - '071268' => '08217', '071831' => '0425', '071842' => '08439', - '072742' => '0441', '076714' => '0671', '076783' => '0553', - '076814' => '0449', '078021' => '0872', '079808' => '0394', - '090129' => '0679', '099455' => '0061', '099769' => '0451' + "014794" => "08041", "018926" => "0445", "02778" => "0449", + "037145" => "0812", "042799" => "0785", "043144" => "0688", + "044903" => "0312", "045863" => "0517", "046594" => "0064", + "047132" => "0152", "051487" => "08167", "051488" => "0140", + "060771" => "0002", "065373" => "0373", "070992" => "0523", + "070993" => "0446", "070999" => "0345", "071001" => "0380", + "071009" => "0440", "071125" => "088677", "071136" => "0451", + "071149" => "0451", "071152" => "0515", "071162" => "0451", + "071268" => "08217", "071831" => "0425", "071842" => "08439", + "072742" => "0441", "076714" => "0671", "076783" => "0553", + "076814" => "0449", "078021" => "0872", "079808" => "0394", + "090129" => "0679", "099455" => "0061", "099769" => "0451" }.freeze def self.upc_convert(upc) test_upc = upc.map(&:to_s).join extract_numbers(AMERICAN_UPC_LOOKUP[test_upc]) end def self.canonicalise_ean(code) - code = code.to_s.delete('- ') + code = code.to_s.delete("- ") if valid_ean?(code) code elsif valid_isbn?(code) - code = '978' + code[0..8] + code = "978" + code[0..8] code + String(ean_checksum(extract_numbers(code))) elsif valid_upc?(code) isbn10 = canonicalise_isbn - code = '978' + isbn10[0..8] + code = "978" + isbn10[0..8] code + String(ean_checksum(extract_numbers(code))) end end def self.canonicalise_isbn(isbn) @@ -183,12 +183,12 @@ FileUtils.rm(yaml(book.saved_ident)) FileUtils.mv(cover(book.saved_ident), cover(book.ident)) if File.exist?(cover(book.saved_ident)) end book.saved_ident = book.ident - filename = book.saved_ident.to_s + '.yaml' - File.open(filename, 'w') { |io| io.puts book.to_yaml } + filename = book.saved_ident.to_s + ".yaml" + File.open(filename, "w") { |io| io.puts book.to_yaml } filename end def save(book, final = false) changed unless final @@ -210,11 +210,11 @@ already_there = (File.size?(yaml(book)) && !@deleted_books.include?(book)) temp_book = book.dup temp_book.library = nil - File.open(yaml(temp_book), 'w') { |io| io.puts temp_book.to_yaml } + File.open(yaml(temp_book), "w") { |io| io.puts temp_book.to_yaml } # Do not notify twice. if changed? notify_observers(self, already_there ? BOOK_UPDATED : BOOK_ADDED, @@ -229,11 +229,11 @@ def save_cover(book, cover_uri) Dir.chdir(path) do # Fetch the cover picture. cover_file = cover(book) - File.open(cover_file, 'w') do |io| + File.open(cover_file, "w") do |io| uri = URI.parse(cover_uri) if uri.scheme.nil? # Regular filename. File.open(cover_uri) { |io2| io.puts io2.read } else @@ -386,15 +386,15 @@ File.join(somewhere, final_cover(book))) end end def self.jpeg?(file) - IO.read(file, 10)[6..9] == 'JFIF' + IO.read(file, 10)[6..9] == "JFIF" end def final_cover(book) # TODO: what about PNG? - book.ident + (Library.jpeg?(cover(book)) ? '.jpg' : '.gif') + book.ident + (Library.jpeg?(cover(book)) ? ".jpg" : ".gif") end protected def initialize(name, store = nil)