Skip to content

Commit

Permalink
Split up Docs serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
elsom25 committed Mar 19, 2024
1 parent d15b108 commit 2b37d62
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 112 deletions.
89 changes: 0 additions & 89 deletions app/serializers/docs/all.rb

This file was deleted.

37 changes: 37 additions & 0 deletions app/serializers/docs/search_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require "json"
require "cgi"

module Serializers
module Docs
class SearchIndex
include Singleton

class << self
delegate :serialize, to: :instance
end

def serialize(category_dist_json)
return @serialized if @serialized

search_index = category_dist_json.flat_map do |vertical|
vertical["categories"].map do |category|
{
"title" => category.fetch("full_name"),
"url" => "?categoryId=#{CGI.escapeURIComponent(category.fetch("id"))}",
"category" => {
"id" => category.fetch("id"),
"name" => category.fetch("name"),
"fully_qualified_type" => category.fetch("full_name"),
"depth" => category.fetch("level"),
},
}
end
end

@serialized = ::JSON.fast_generate(search_index)
end
end
end
end
45 changes: 45 additions & 0 deletions app/serializers/docs/sibling_groups.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

require "yaml"

module Serializers
module Docs
class SiblingGroups
include Singleton

class << self
delegate :serialize, to: :instance
end

def serialize(category_dist_json)
return @serialized if @serialized

sibling_groups = {}
category_dist_json.each do |vertical|
vertical["categories"].each do |category|
parent_id = category.fetch("parent_id")
parent_id = "root" if parent_id.nil? || parent_id.empty?
node_depth = category.fetch("level")

sibling_group = {
"id" => category.fetch("id"),
"name" => category.fetch("name"),
"fully_qualified_type" => category.fetch("full_name"),
"depth" => node_depth,
"parent_id" => parent_id,
"node_type" => node_depth.zero? ? "root" : "leaf",
"ancestor_ids" => category.fetch("ancestors").map { _1.fetch("id") }.join(","),
"attribute_ids" => category.fetch("attributes").map { _1.fetch("id") }.join(","),
}

sibling_groups[node_depth] ||= {}
sibling_groups[node_depth][parent_id] ||= []
sibling_groups[node_depth][parent_id] << sibling_group
end
end

@serialized = sibling_groups.to_yaml(line_width: 1000)
end
end
end
end
3 changes: 2 additions & 1 deletion application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
require_relative "app/serializers/data/property_value_serializer"
require_relative "app/serializers/dist/json"
require_relative "app/serializers/dist/text"
require_relative "app/serializers/docs/all"
require_relative "app/serializers/docs/sibling_groups"
require_relative "app/serializers/docs/search_index"

module Application
ROOT = File.expand_path(".", __dir__)
Expand Down
40 changes: 18 additions & 22 deletions bin/generate_docs
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,28 @@

require "yaml"
require "json"
require "cgi"
require "fileutils"

require_relative "../application"

FileUtils.mkdir_p("#{Application.root}/docs/_data/unstable")
version = "unstable" # eventually bin command
target = "#{Application.root}/docs/_data/#{version}"
FileUtils.mkdir_p(target)

puts "Loading data"
puts " → Reading dist/categories.json..."
category_data = JSON.load_file("#{Application.root}/dist/categories.json")
puts " → Reading dist/attributes.json..."
attribute_data = JSON.load_file("#{Application.root}/dist/attributes.json")
puts " ✓"
puts
category_data = JSON.load_file("#{Application.root}/dist/categories.json").fetch("verticals")
attribute_data = JSON.load_file("#{Application.root}/dist/attributes.json").fetch("attributes")

docs_serializer = Serializers::Docs::All.new(
verticals: category_data.fetch("verticals"),
properties: attribute_data.fetch("attributes"),
)
File.open("#{target}/sibling_groups.yml", "w") do |file|
file.write(Serializers::Docs::SiblingGroups.serialize(category_data))
file.write("\n")
end

docs_serializer.generate_sibling_groups(
target: "docs/_data/unstable/sibling_groups.yml",
)
docs_serializer.generate_search_index(
target: "docs/_data/unstable/search_index.json",
)
docs_serializer.generate_attributes(
target: "docs/_data/unstable/attributes.yml",
)
File.open("#{target}/search_index.json", "w") do |file|
file.write(Serializers::Docs::SearchIndex.serialize(category_data))
file.write("\n")
end

File.open("#{target}/attributes.yml", "w") do |file|
file.write(attribute_data.to_yaml(line_width: 1000))
file.write("\n")
end

0 comments on commit 2b37d62

Please sign in to comment.