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"
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

View File

@ -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 }

View File

@ -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