Use depth instead of shallow in API

This commit is contained in:
Alex Ling
2021-09-22 09:17:14 +00:00
parent 72fae7f5ed
commit 974b6cfe9b
3 changed files with 29 additions and 20 deletions
+2 -2
View File
@@ -97,14 +97,14 @@ class Library
titles.flat_map &.deep_entries
end
def build_json(*, slim = false, shallow = false)
def build_json(*, slim = false, depth = -1)
JSON.build do |json|
json.object do
json.field "dir", @dir
json.field "titles" do
json.array do
self.titles.each do |title|
json.raw title.build_json(slim: slim, shallow: shallow)
json.raw title.build_json(slim: slim, depth: depth)
end
end
end
+11 -10
View File
@@ -172,7 +172,7 @@ class Title
alias SortContext = NamedTuple(username: String, opt: SortOptions)
def build_json(*, slim = false, shallow = false,
def build_json(*, slim = false, depth = -1,
sort_context : SortContext? = nil)
JSON.build do |json|
json.object do
@@ -185,11 +185,12 @@ class Title
json.field "cover_url", cover_url
json.field "mtime" { json.number @mtime.to_unix }
end
unless shallow
unless depth == 0
json.field "titles" do
json.array do
self.titles.each do |title|
json.raw title.build_json(slim: slim, shallow: shallow)
json.raw title.build_json(slim: slim,
depth: depth > 0 ? depth - 1 : depth)
end
end
end
@@ -206,13 +207,13 @@ class Title
end
end
end
json.field "parents" do
json.array do
self.parents.each do |title|
json.object do
json.field "title", title.title
json.field "id", title.id
end
end
json.field "parents" do
json.array do
self.parents.each do |title|
json.object do
json.field "title", title.title
json.field "id", title.id
end
end
end