Clean up library classes

This commit is contained in:
Alex Ling 2020-12-30 15:23:38 +00:00
parent cf930418cb
commit db2a51a26b
3 changed files with 19 additions and 17 deletions

View File

@ -1,11 +1,12 @@
require "image_size" require "image_size"
class Entry 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, size : String, pages : Int32, id : String, encoded_path : String,
encoded_title : String, mtime : Time, err_msg : 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 @encoded_path = URI.encode @zip_path
@title = File.basename @zip_path, File.extname @zip_path @title = File.basename @zip_path, File.extname @zip_path
@encoded_title = URI.encode @title @encoded_title = URI.encode @title

View File

@ -1,5 +1,5 @@
class Library class Library
property dir : String, title_ids : Array(String), getter dir : String, title_ids : Array(String),
title_hash : Hash(String, Title) title_hash : Hash(String, Title)
use_default use_default
@ -106,7 +106,7 @@ class Library
.select { |fn| !fn.starts_with? "." } .select { |fn| !fn.starts_with? "." }
.map { |fn| File.join @dir, fn } .map { |fn| File.join @dir, fn }
.select { |path| File.directory? path } .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?) } .select { |title| !(title.entries.empty? && title.titles.empty?) }
.sort { |a, b| a.title <=> b.title } .sort { |a, b| a.title <=> b.title }
.tap { |_| @title_ids.clear } .tap { |_| @title_ids.clear }

View File

@ -1,13 +1,14 @@
require "../archive" require "../archive"
class Title 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, entries : Array(Entry), title : String, id : String,
encoded_title : String, mtime : Time, encoded_title : String, mtime : Time
entry_display_name_cache : Hash(String, String)?
def initialize(@dir : String, @parent_id, storage, @entry_display_name_cache : Hash(String, String)?
@library : Library)
def initialize(@dir : String, @parent_id)
storage = Storage.default
id = storage.get_id @dir, true id = storage.get_id @dir, true
if id.nil? if id.nil?
id = random_str id = random_str
@ -28,26 +29,26 @@ class Title
next if fn.starts_with? "." next if fn.starts_with? "."
path = File.join dir, fn path = File.join dir, fn
if File.directory? path 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 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 @title_ids << title.id
next next
end end
if [".zip", ".cbz", ".rar", ".cbr"].includes? File.extname path 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 @entries << entry if entry.pages > 0 || entry.err_msg
end end
end end
mtimes = [@mtime] 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 } mtimes += @entries.map { |e| e.mtime }
@mtime = mtimes.max @mtime = mtimes.max
@title_ids.sort! do |a, b| @title_ids.sort! do |a, b|
compare_numerically @library.title_hash[a].title, compare_numerically Library.default.title_hash[a].title,
@library.title_hash[b].title Library.default.title_hash[b].title
end end
sorter = ChapterSorter.new @entries.map { |e| e.title } sorter = ChapterSorter.new @entries.map { |e| e.title }
@entries.sort! do |a, b| @entries.sort! do |a, b|
@ -83,7 +84,7 @@ class Title
end end
def titles def titles
@title_ids.map { |tid| @library.get_title! tid } @title_ids.map { |tid| Library.default.get_title! tid }
end end
# Get all entries, including entries in nested titles # Get all entries, including entries in nested titles
@ -101,7 +102,7 @@ class Title
ary = [] of Title ary = [] of Title
tid = @parent_id tid = @parent_id
while !tid.empty? while !tid.empty?
title = @library.get_title! tid title = Library.default.get_title! tid
ary << title ary << title
tid = title.parent_id tid = title.parent_id
end end