Class: Alexandria::LibraryStore
- Inherits:
-
Object
- Object
- Alexandria::LibraryStore
- Includes:
- Logging, GetText
- Defined in:
- lib/alexandria/library_store.rb
Constant Summary collapse
- FIX_BIGNUM_REGEX =
%r{loaned_since:\s*(!ruby/object:Bignum\s*)?(\d+)\n}.freeze
Instance Method Summary collapse
-
#initialize(dir) ⇒ LibraryStore
constructor
A new instance of LibraryStore.
- #library_dir ⇒ Object
- #load_all_libraries ⇒ Object
- #load_all_smart_libraries ⇒ Object
- #load_library(name) ⇒ Object
- #smart_library_dir ⇒ Object
Methods included from Logging
Constructor Details
#initialize(dir) ⇒ LibraryStore
Returns a new instance of LibraryStore.
17 18 19 |
# File 'lib/alexandria/library_store.rb', line 17 def initialize(dir) @dir = dir end |
Instance Method Details
#library_dir ⇒ Object
173 174 175 |
# File 'lib/alexandria/library_store.rb', line 173 def library_dir @dir end |
#load_all_libraries ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/alexandria/library_store.rb', line 21 def load_all_libraries a = [] begin Dir.entries(library_dir).each do |file| # Skip hidden files. next if file.start_with?(".") # Skip non-directory files. next unless File.stat(File.join(library_dir, file)).directory? a << load_library(file) end rescue Errno::ENOENT FileUtils.mkdir_p(library_dir) end # Create the default library if there is no library yet. a << load_library(_("My Library")) if a.empty? a end |
#load_all_smart_libraries ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/alexandria/library_store.rb', line 146 def load_all_smart_libraries a = [] begin # Deserialize smart libraries. Dir.chdir(smart_library_dir) do Dir["*" + SmartLibrary::EXT].each do |filename| # Skip non-regular files. next unless File.stat(filename).file? text = File.read(filename) hash = YAML.safe_load(text, permitted_classes: [Symbol]) smart_library = SmartLibrary.from_hash(hash, self) a << smart_library end end rescue Errno::ENOENT # First run and no smart libraries yet? Provide some default # ones. SmartLibrary.sample_smart_libraries(self).each do |smart_library| smart_library.save a << smart_library end end a.each(&:refilter) a end |
#load_library(name) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/alexandria/library_store.rb', line 42 def load_library(name) test = [0, nil] ruined_books = [] library = Library.new(name, self) FileUtils.mkdir_p(library.path) Dir.chdir(library.path) do Dir["*" + Library::EXT[:book]].sort.each do |filename| test[1] = filename if (test[0]).zero? unless File.size? test[1] log.warn { "Book file #{test[1]} was empty" } md = /([\dxX]{10,13})#{Library::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... end next end book = regularize_book_from_yaml(test[1]) old_isbn = book.isbn old_pub_year = book.publishing_year begin raise format(_("Not a book: %<book>s"), book.inspect) unless book.is_a?(Book) ean = Library.canonicalise_ean(book.isbn) book.isbn = ean if ean unless book.publishing_year.nil? book.publishing_year = book.publishing_year.to_i end # Or if isbn has changed unless book.isbn == old_isbn raise format(_("%<file>s isbn is not okay"), file: test[1]) end # Re-save book if Alexandria::DATA_VERSION changes unless book.version == Alexandria::DATA_VERSION raise format(_("%<file>s version is not okay"), file: test[1]) end # Or if publishing year has changed unless book.publishing_year == old_pub_year raise format(_("%<file>s pub year is not okay"), file: test[1]) end # ruined_books << [book, book.isbn, library] book.library = library.name ## TODO copy cover image file, if necessary # due to #26909 cover files for books without ISBN are re-saved as # "g#{ident}.cover" if (book.isbn.nil? || book.isbn.empty?) && File.exist?(library.old_cover(book)) log.debug do "#{library.name}; book #{book.title} has no ISBN, fixing cover image" end FileUtils::Verbose.mv(library.old_cover(book), library.cover(book)) end library << book rescue StandardError book.version = Alexandria::DATA_VERSION savedfilename = library.simple_save(book) test[0] = test[0] + 1 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 # Since 0.4.0 the cover files '_small.jpg' and # '_medium.jpg' have been deprecated for a single medium # cover file named '.cover'. Dir["*" + "_medium.jpg"].each do |medium_cover| FileUtils.mv(medium_cover, medium_cover.sub(/_medium\.jpg$/, Library::EXT[:cover])) end Dir["*" + Library::EXT[:cover]].each do |cover| next if cover[0] == "g" md = /(.+)\.cover/.match(cover) ean = Library.canonicalise_ean(md[1]) || md[1] unless cover == ean + Library::EXT[:cover] FileUtils.mv(cover, ean + Library::EXT[:cover]) end end FileUtils.rm_f(Dir["*_small.jpg"]) end library.ruined_books = ruined_books library end |
#smart_library_dir ⇒ Object
177 178 179 |
# File 'lib/alexandria/library_store.rb', line 177 def smart_library_dir File.join(@dir, ".smart_libraries") end |