From db2a51a26b7108275905e62f2b0d62ec1afea728 Mon Sep 17 00:00:00 2001 From: Alex Ling Date: Wed, 30 Dec 2020 15:23:38 +0000 Subject: [PATCH] Clean up library classes --- src/library/entry.cr | 5 +++-- src/library/library.cr | 4 ++-- src/library/title.cr | 27 ++++++++++++++------------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/library/entry.cr b/src/library/entry.cr index 810c6c8..3e270cf 100644 --- a/src/library/entry.cr +++ b/src/library/entry.cr @@ -1,11 +1,12 @@ require "image_size" class Entry - property zip_path : String, book : Title, title : String, + getter zip_path : String, book : Title, title : String, size : String, pages : Int32, id : String, encoded_path : String, encoded_title : String, mtime : Time, err_msg : String? - def initialize(@zip_path, @book, storage) + def initialize(@zip_path, @book) + storage = Storage.default @encoded_path = URI.encode @zip_path @title = File.basename @zip_path, File.extname @zip_path @encoded_title = URI.encode @title diff --git a/src/library/library.cr b/src/library/library.cr index a7e1c49..a3db640 100644 --- a/src/library/library.cr +++ b/src/library/library.cr @@ -1,5 +1,5 @@ class Library - property dir : String, title_ids : Array(String), + getter dir : String, title_ids : Array(String), title_hash : Hash(String, Title) use_default @@ -106,7 +106,7 @@ class Library .select { |fn| !fn.starts_with? "." } .map { |fn| File.join @dir, fn } .select { |path| File.directory? path } - .map { |path| Title.new path, "", storage, self } + .map { |path| Title.new path, "" } .select { |title| !(title.entries.empty? && title.titles.empty?) } .sort { |a, b| a.title <=> b.title } .tap { |_| @title_ids.clear } diff --git a/src/library/title.cr b/src/library/title.cr index f9af6f7..5fef991 100644 --- a/src/library/title.cr +++ b/src/library/title.cr @@ -1,13 +1,14 @@ require "../archive" class Title - property dir : String, parent_id : String, title_ids : Array(String), + getter dir : String, parent_id : String, title_ids : Array(String), entries : Array(Entry), title : String, id : String, - encoded_title : String, mtime : Time, - entry_display_name_cache : Hash(String, String)? + encoded_title : String, mtime : Time - def initialize(@dir : String, @parent_id, storage, - @library : Library) + @entry_display_name_cache : Hash(String, String)? + + def initialize(@dir : String, @parent_id) + storage = Storage.default id = storage.get_id @dir, true if id.nil? id = random_str @@ -28,26 +29,26 @@ class Title next if fn.starts_with? "." path = File.join dir, fn if File.directory? path - title = Title.new path, @id, storage, library + title = Title.new path, @id next if title.entries.size == 0 && title.titles.size == 0 - @library.title_hash[title.id] = title + Library.default.title_hash[title.id] = title @title_ids << title.id next end if [".zip", ".cbz", ".rar", ".cbr"].includes? File.extname path - entry = Entry.new path, self, storage + entry = Entry.new path, self @entries << entry if entry.pages > 0 || entry.err_msg end end mtimes = [@mtime] - mtimes += @title_ids.map { |e| @library.title_hash[e].mtime } + mtimes += @title_ids.map { |e| Library.default.title_hash[e].mtime } mtimes += @entries.map { |e| e.mtime } @mtime = mtimes.max @title_ids.sort! do |a, b| - compare_numerically @library.title_hash[a].title, - @library.title_hash[b].title + compare_numerically Library.default.title_hash[a].title, + Library.default.title_hash[b].title end sorter = ChapterSorter.new @entries.map { |e| e.title } @entries.sort! do |a, b| @@ -83,7 +84,7 @@ class Title end def titles - @title_ids.map { |tid| @library.get_title! tid } + @title_ids.map { |tid| Library.default.get_title! tid } end # Get all entries, including entries in nested titles @@ -101,7 +102,7 @@ class Title ary = [] of Title tid = @parent_id while !tid.empty? - title = @library.get_title! tid + title = Library.default.get_title! tid ary << title tid = title.parent_id end