mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-02 19:05:32 -04:00
Choose correct subclass based on YAML node
This commit is contained in:
parent
5b23a112b2
commit
2fb620211d
@ -1,5 +1,15 @@
|
||||
require "image_size"
|
||||
|
||||
private def node_has_key(node : YAML::Nodes::Mapping, key : String)
|
||||
node.nodes
|
||||
.map_with_index { |n, i| {n, i} }
|
||||
.select(&.[1].even?)
|
||||
.map(&.[0])
|
||||
.select(&.is_a?(YAML::Nodes::Scalar))
|
||||
.map(&.as(YAML::Nodes::Scalar).value)
|
||||
.includes? key
|
||||
end
|
||||
|
||||
abstract class Entry
|
||||
getter id : String, book : Title, title : String, path : String,
|
||||
size : String, pages : Int32, mtime : Time,
|
||||
@ -13,10 +23,20 @@ abstract class Entry
|
||||
end
|
||||
|
||||
def self.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
|
||||
# TODO: check node? and select proper subclass
|
||||
ArchiveEntry.new ctx, node
|
||||
rescue e
|
||||
DirEntry.new ctx, node
|
||||
unless node.is_a? YAML::Nodes::Mapping
|
||||
raise "Unexpected node type in YAML"
|
||||
end
|
||||
# Doing YAML::Any.new(ctx, node) here causes a weird error, so
|
||||
# instead we are using a more hacky approach (see `node_has_key`).
|
||||
# TODO: Use a more elegant approach
|
||||
if node_has_key node, "zip_path"
|
||||
ArchiveEntry.new ctx, node
|
||||
elsif node_has_key node, "dir_path"
|
||||
DirEntry.new ctx, node
|
||||
else
|
||||
raise "Unknown entry found in YAML cache. Try deleting the " \
|
||||
"`library.yml.gz` file"
|
||||
end
|
||||
end
|
||||
|
||||
def build_json(*, slim = false)
|
||||
|
Loading…
x
Reference in New Issue
Block a user