forked from rubytoolbox/catalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatalog.rb
53 lines (43 loc) · 1.24 KB
/
catalog.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# frozen_string_literal: true
ENV["RACK_ENV"] ||= "development"
require "pathname"
require "yaml"
require "bundler"
Bundler.require :default, ENV["RACK_ENV"]
class Catalog
attr_accessor :root
private :root=
def self.schema
YAML.safe_load Pathname.new(__dir__).join("..", "json-schema.yml").read
end
def initialize(root: File.join(__dir__, "../catalog"))
self.root = Pathname.new(root).expand_path
end
def as_json
{
category_groups: category_groups,
}
end
def export(path: File.join(__dir__, "../build/catalog.json"))
File.open(path, "w+") do |f|
f.puts JSON.pretty_generate(as_json)
end
end
private
def category_groups
@category_groups ||= Dir[root.join("*")].map do |path|
meta = YAML.safe_load Pathname.new(path).join("_meta.yml").read
{
categories: categories_at(path),
description: meta["description"],
name: meta["name"],
permalink: File.basename(path),
}
end
end
def categories_at(path)
Dir[File.join(path, "*.yml")].reject { |file_path| File.basename(file_path) == "_meta.yml" }.map do |category_path|
YAML.safe_load(File.read(category_path)).merge(permalink: File.basename(category_path).gsub(".yml", ""))
end
end
end