lib/alexandria/models/library.rb in alexandria-book-collection-manager-0.6.9 vs lib/alexandria/models/library.rb in alexandria-book-collection-manager-0.7.0
- old
+ new
@@ -36,11 +36,11 @@
include Logging
attr_reader :name
attr_accessor :ruined_books, :updating, :deleted_books
DIR = File.join(ENV['HOME'], '.alexandria')
- EXT = { book: '.yaml', cover: '.cover' }
+ EXT = { book: '.yaml', cover: '.cover' }.freeze
include GetText
extend GetText
bindtextdomain(Alexandria::TEXTDOMAIN, charset: 'UTF-8')
@@ -52,10 +52,11 @@
end
def updating?
@updating
end
+
def self.generate_new_name(existing_libraries,
from_base = _('Untitled'))
i = 1
name = nil
all_libraries = existing_libraries + @@deleted_libraries
@@ -75,21 +76,21 @@
ruined_books = []
library = Library.new(name)
FileUtils.mkdir_p(library.path) unless File.exist?(library.path)
Dir.chdir(library.path) do
Dir['*' + EXT[:book]].each do |filename|
- test[1] = filename if test[0] == 0
+ test[1] = filename if (test[0]).zero?
unless File.size? test[1]
log.warn { "Book file #{test[1]} was empty" }
md = /([\dxX]{10,13})#{EXT[:book]}/.match(filename)
if md
file_isbn = md[1]
ruined_books << [nil, file_isbn, library]
else
log.warn { "Filename #{filename} does not contain an ISBN" }
- # TODO delete this file...
+ # TODO: delete this file...
end
next
end
book = regularize_book_from_yaml(test[1])
old_isbn = book.isbn
@@ -181,11 +182,11 @@
# Code to remove the mystery string in books imported from Amazon
# (In the past, still?) To allow ruby-amazon to be removed.
# The string is removed on load, but can't make it stick, maybe has to do with cache
- if /!str:Amazon::Search::Response/.match(text)
+ if text =~ /!str:Amazon::Search::Response/
log.debug { "Removing Ruby/Amazon strings from #{name}" }
text.gsub!('!str:Amazon::Search::Response', '')
end
# Backward compatibility with versions <= 0.6.0, where the
@@ -217,22 +218,22 @@
string_saved_ident = md2[1].strip
log.debug { "fixing saved_ident #{book.saved_ident} -> #{string_saved_ident}" }
book.saved_ident = string_saved_ident
end
end
- if book.isbn.class == String and book.isbn.length == 0
+ if (book.isbn.class == String) && book.isbn.empty?
book.isbn = nil # save trouble later
end
book
end
def self.loadall
a = []
begin
Dir.entries(DIR).each do |file|
# Skip hidden files.
- next if /^\./.match(file)
+ next if file =~ /^\./
# Skip non-directory files.
next unless File.stat(File.join(DIR, file)).directory?
a << load(file)
end
@@ -240,13 +241,11 @@
rescue Errno::ENOENT
FileUtils.mkdir_p(DIR)
end
# Create the default library if there is no library yet.
- if a.empty?
- a << load(_('My Library'))
- end
+ a << load(_('My Library')) if a.empty?
a
end
def self.move(source_library, dest_library, *books)
@@ -283,14 +282,14 @@
@isbn = isbn
end
end
def self.extract_numbers(isbn)
- raise NoISBNError.new('Nil ISBN') if isbn.nil? || isbn.empty?
+ raise NoISBNError, 'Nil ISBN' if isbn.nil? || isbn.empty?
isbn.delete('- ').upcase.split('').map do |x|
- raise InvalidISBNError.new(isbn) unless x =~ /[\dX]/
+ raise InvalidISBNError, isbn unless x =~ /[\dX]/
x == 'X' ? 10 : x.to_i
end
end
def self.isbn_checksum(numbers)
@@ -301,11 +300,11 @@
sum == 10 ? 'X' : sum
end
def self.valid_isbn?(isbn)
numbers = extract_numbers(isbn)
- numbers.length == 10 and isbn_checksum(numbers) == 0
+ (numbers.length == 10) && isbn_checksum(numbers).zero?
rescue InvalidISBNError
false
end
def self.ean_checksum(numbers)
@@ -313,14 +312,14 @@
[0, 2, 4, 6, 8, 10].map { |x| numbers[x] }.sum)) % 10
end
def self.valid_ean?(ean)
numbers = extract_numbers(ean)
- (numbers.length == 13 and
- ean_checksum(numbers[0..11]) == numbers[12]) or
- (numbers.length == 18 and
- ean_checksum(numbers[0..11]) == numbers[12])
+ ((numbers.length == 13) &&
+ (ean_checksum(numbers[0..11]) == numbers[12])) ||
+ ((numbers.length == 18) &&
+ (ean_checksum(numbers[0..11]) == numbers[12]))
rescue InvalidISBNError
false
end
def self.upc_checksum(numbers)
@@ -328,12 +327,12 @@
[1, 3, 5, 7, 9].map { |x| numbers[x] }.sum)) % 10
end
def self.valid_upc?(upc)
numbers = extract_numbers(upc)
- (numbers.length == 17 and
- upc_checksum(numbers[0..10]) == numbers[11])
+ ((numbers.length == 17) &&
+ (upc_checksum(numbers[0..10]) == numbers[11]))
rescue InvalidISBNError
false
end
AMERICAN_UPC_LOOKUP = {
@@ -347,64 +346,64 @@
'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('- ')
- if self.valid_ean?(code)
+ if valid_ean?(code)
return code
- elsif self.valid_isbn?(code)
+ elsif valid_isbn?(code)
code = '978' + code[0..8]
return code + String(ean_checksum(extract_numbers(code)))
- elsif self.valid_upc?(code)
- isbn10 = canonicalise_isbn
+ elsif valid_upc?(code)
+ isbn10 = canonicalise_isbn
code = '978' + isbn10[0..8]
return code + String(ean_checksum(extract_numbers(code)))
## raise "fix function Alexandria::Library.canonicalise_ean"
else
- raise InvalidISBNError.new(code)
+ raise InvalidISBNError, code
end
end
def self.canonicalise_isbn(isbn)
numbers = extract_numbers(isbn)
- if self.valid_ean?(isbn) and numbers[0..2] != [9, 7, 8]
+ if valid_ean?(isbn) && (numbers[0..2] != [9, 7, 8])
return isbn
end
- canonical = if self.valid_ean?(isbn)
+ 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])]
- elsif self.valid_upc?(isbn)
+ elsif valid_upc?(isbn)
# Seems to be a valid UPC number.
prefix = upc_convert(numbers[0..5])
isbn_sans_chcksm = prefix + numbers[(8 + prefix.length)..17]
isbn_sans_chcksm + [isbn_checksum(isbn_sans_chcksm)]
- elsif self.valid_isbn?(isbn)
+ elsif valid_isbn?(isbn)
# Seems to be a valid ISBN number.
numbers[0..-2] + [isbn_checksum(numbers[0..-2])]
else
- raise InvalidISBNError.new(isbn)
+ raise InvalidISBNError, isbn
end
canonical.map(&:to_s).join
end
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? or book.saved_ident.empty?
+ if book.saved_ident.nil? || book.saved_ident.empty?
book.saved_ident = book.ident
end
if book.ident != book.saved_ident
# log.debug { "Backwards compatibility step: #{book.saved_ident.inspect}, #{book.ident.inspect}" }
FileUtils.rm(yaml(book.saved_ident))
@@ -439,11 +438,11 @@
# can still use the old one to update their models.
notify_observers(self, BOOK_UPDATED, book) unless final
book.saved_ident = book.ident
end
# #was File.exist? but that returns true for empty files... CathalMagus
- already_there = (File.size?(yaml(book)) and
+ 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 }
@@ -501,27 +500,27 @@
FileUtils.rm_f(file)
end
end
end
- alias_method :old_delete, :delete
+ alias old_delete delete
def delete(book = nil)
if book.nil?
# 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) }
- raise "Book #{book.isbn} was already deleted" unless doubles.empty?
+ 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
# a book with the same identifier as another book he/she
# previously deleted and that he/she is trying to redo.
- if i and self[i].equal? book
+ if i && self[i].equal?(book)
changed
old_delete(book) # FIX this will old_delete all '==' books
notify_observers(self, BOOK_REMOVED, book)
end
end
@@ -537,19 +536,19 @@
raise unless @@deleted_libraries.include?(self)
@@deleted_libraries.delete(self)
else
raise unless @deleted_books.include?(book)
@deleted_books.delete(book)
- unless self.include?(book)
+ unless include?(book)
changed
self << book
notify_observers(self, BOOK_ADDED, book)
end
end
end
- alias_method :old_select, :select
+ alias old_select select
def select
filtered_library = Library.new(@name)
each do |book|
filtered_library << book if yield(book)
end
@@ -568,13 +567,13 @@
else
"g#{something.ident}" # g is for generated id...
end
when String
something
- when Bignum
+ when Integer
something
- when Fixnum
+ when Integer
something
else
raise "#{something} is a #{something.class}"
end
File.join(path, ident.to_s + EXT[:cover])
@@ -584,13 +583,13 @@
ident = case something
when Book
something.ident
when String
something
- when Bignum
+ when Integer
something
- when Fixnum
+ when Integer
something
else
raise "#{something} is #{something.class}"
end
File.join(basedir, ident.to_s + EXT[:book])
@@ -600,11 +599,11 @@
File.rename(path, File.join(DIR, name))
@name = name
end
def n_rated
- count { |x| !x.rating.nil? and x.rating > 0 }
+ count { |x| !x.rating.nil? && x.rating > 0 }
end
def n_unrated
length - n_rated
end
@@ -626,11 +625,11 @@
def self.jpeg?(file)
'JFIF' == IO.read(file, 10)[6..9]
end
def final_cover(book)
- # TODO what about PNG?
+ # TODO: what about PNG?
book.ident + (Library.jpeg?(cover(book)) ? '.jpg' : '.gif')
end
protected
@@ -651,11 +650,11 @@
@all_libraries.concat(Library.loadall)
@all_libraries.concat(SmartLibrary.loadall)
ruined = []
deleted = []
- all_regular_libraries.each {|library|
+ all_regular_libraries.each { |library|
ruined += library.ruined_books
# make deleted books from each library accessible so we don't crash on smart libraries
deleted += library.deleted_books
}
@ruined_books = ruined
@@ -672,10 +671,11 @@
# def all_dynamic_libraries
# @all_libraries.select { |x| x.is_a?(SmartLibrary) }
# end
- LIBRARY_ADDED, LIBRARY_REMOVED = 1, 2
+ LIBRARY_ADDED = 1
+ LIBRARY_REMOVED = 2
def add_library(library)
@all_libraries << library
notify(LIBRARY_ADDED, library)
end