Module: HakumiComponents::Documentation

Extended by:
T::Sig
Defined in:
lib/hakumi_components/documentation.rb,
lib/hakumi_components/documentation/node.rb,
lib/hakumi_components/documentation/models.rb

Defined Under Namespace

Classes: Component, Example, Member, Node, Section

Constant Summary collapse

INTERNAL_COMPONENTS =
T.let(%w[
  admin_panel
  container
  selection_control
].freeze, T::Array[String])
MemberHash =
T.type_alias { T::Hash[String, T.nilable(T.any(String, T::Boolean))] }
SectionHash =
T.type_alias { T::Hash[String, T.any(T.nilable(String), T::Array[MemberHash])] }
ExampleHash =
T.type_alias { T::Hash[String, T.nilable(T.any(String, T::Boolean))] }
ComponentHash =
T.type_alias do
  T::Hash[String, T.any(
    T.nilable(String),
    T::Array[String],
    T::Array[ExampleHash],
    T::Array[SectionHash]
  )]
end

Class Method Summary collapse

Class Method Details

.allObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/hakumi_components/documentation.rb', line 110

def all
  result = T.let({}, T::Hash[String, Component])
  components.each do |name|
    documentation = (name)
    next unless documentation

    result[name] = documentation.with_examples(examples(name))
  end
  result
end

.api(component_name) ⇒ Object



102
103
104
105
106
107
# File 'lib/hakumi_components/documentation.rb', line 102

def api(component_name)
  meta = (component_name)
  return [] unless meta

  meta.api_sections
end

.component_directoriesObject



36
37
38
39
40
41
42
43
44
# File 'lib/hakumi_components/documentation.rb', line 36

def component_directories
  return [] unless components_root.exist?

  components_root.children
    .select(&:directory?)
    .map { |path| path.basename.to_s }
    .reject { |name| name == "concerns" }
    .sort
end

.componentsObject



26
27
28
29
30
31
32
33
# File 'lib/hakumi_components/documentation.rb', line 26

def components
  return [] unless components_root.exist?

  components_root.children
    .select { |path| path.directory? && (path / "docs" / "meta.yml").exist? }
    .map { |path| path.basename.to_s }
    .sort
end

.components_rootObject



20
21
22
23
# File 'lib/hakumi_components/documentation.rb', line 20

def components_root
  @components_root = T.let(@components_root, T.nilable(Pathname)) unless instance_variable_defined?(:@components_root)
  @components_root ||= Pathname.new(File.expand_path("../../app/components/hakumi_components", __dir__.to_s))
end

.example(component_name, example_name) ⇒ Object



88
89
90
91
92
93
# File 'lib/hakumi_components/documentation.rb', line 88

def example(component_name, example_name)
  example_path = example_path_for(component_name, example_name)
  return nil unless example_path

  example_path.read
end

.example_path_for(component_name, example_name) ⇒ Object



96
97
98
99
# File 'lib/hakumi_components/documentation.rb', line 96

def example_path_for(component_name, example_name)
  path = components_root / component_name / "docs" / "examples" / "_#{example_name}.html.erb"
  path.exist? ? path : nil
end

.examples(component_name) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/hakumi_components/documentation.rb', line 78

def examples(component_name)
  meta = (component_name)
  return [] unless meta

  meta.examples.map do |doc_example|
    doc_example.with_content(example(component_name, doc_example.name))
  end
end

.internal_component?(component_name) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/hakumi_components/documentation.rb', line 57

def internal_component?(component_name)
  internal_components.include?(component_name)
end

.internal_componentsObject



47
48
49
# File 'lib/hakumi_components/documentation.rb', line 47

def internal_components
  INTERNAL_COMPONENTS
end

.metadata(component_name) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hakumi_components/documentation.rb', line 62

def (component_name)
  meta = (component_name)
  return nil unless meta

  Component.new(
    name: meta.fetch("name").string || component_name.tr("_", " ").split.map(&:capitalize).join(" "),
    description: meta.fetch("description").string,
    category: meta.fetch("category").string,
    when_to_use: meta.fetch("when_to_use").string_array,
    when_not_to_use: meta.fetch("when_not_to_use").string_array,
    examples: build_examples(meta),
    api_sections: build_sections(meta)
  )
end

.public_componentsObject



52
53
54
# File 'lib/hakumi_components/documentation.rb', line 52

def public_components
  components - internal_components
end

.to_json(*_args) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/hakumi_components/documentation.rb', line 122

def to_json(*_args)
  require "json"

  serialized = T.let({}, T::Hash[String, ComponentHash])
  all.each do |name, documentation|
    serialized[name] = documentation.to_h
  end

  JSON.pretty_generate(serialized)
end