Class: Primer::Yard::LookbookPage

Inherits:
Object
  • Object
show all
Includes:
DocsHelper
Defined in:
lib/primer/yard/lookbook_pages_backend.rb

Overview

A single Lookbook page.

Constant Summary collapse

PREVIEW_MAP =
{
  Primer::Alpha::TextField => [:single_text_field_form, :multi_text_field_form],
  Primer::Alpha::TextArea => [],
  Primer::Alpha::Select => [:select_form],
  Primer::Alpha::MultiInput => [:multi_input_form],
  Primer::Alpha::RadioButton => [:radio_button_with_nested_form],
  Primer::Alpha::RadioButtonGroup => [:radio_button_group_form],
  Primer::Alpha::CheckBox => [:check_box_with_nested_form],
  Primer::Alpha::CheckBoxGroup => [:check_box_group_form],
  Primer::Alpha::SubmitButton => [:submit_button_form],
  Primer::Alpha::FormButton => [:submit_button_form]
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DocsHelper

#link_to_accessibility, #link_to_component, #link_to_heading_practices, #link_to_octicons, #link_to_system_arguments_docs, #link_to_typography_docs, #one_of, #pretty_default_value, #pretty_value, #status_module_and_short_name

Constructor Details

#initialize(component_ref, backend, docs) ⇒ LookbookPage

Returns a new instance of LookbookPage.



25
26
27
28
29
# File 'lib/primer/yard/lookbook_pages_backend.rb', line 25

def initialize(component_ref, backend, docs)
  @component_ref = component_ref
  @backend = backend
  @docs = docs
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



23
24
25
# File 'lib/primer/yard/lookbook_pages_backend.rb', line 23

def backend
  @backend
end

#component_refObject (readonly)

Returns the value of attribute component_ref.



23
24
25
# File 'lib/primer/yard/lookbook_pages_backend.rb', line 23

def component_ref
  @component_ref
end

#docsObject (readonly)

Returns the value of attribute docs.



23
24
25
# File 'lib/primer/yard/lookbook_pages_backend.rb', line 23

def docs
  @docs
end

Instance Method Details

#generateObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/primer/yard/lookbook_pages_backend.rb', line 37

def generate
  path = File.expand_path(
    File.join(
      *%w[.. .. .. previews pages forms inputs],
      "#{docs.short_name.dasherize.underscore}.md.erb"
    ), __dir__
  )

  documented_methods = docs.non_slot_methods.select do |mtd|
    [component.name, "Primer::Forms::Dsl::InputMethods"].include?(mtd.parent.title)
  end

  preview_methods = PREVIEW_MAP[component]
  preview_erbs = preview_methods.map do |preview_method|
    if Primer::FormsPreview.instance_methods.exclude?(preview_method)
      raise "Preview '#{preview_method}' does not exist in Primer::FormsPreview"
    end
    # rubocop:enable Style/IfUnlessModifier

    "<%= embed Primer::FormsPreview, #{preview_method.inspect} %>"
  end
  # rubocop:enable Lint/UselessAssignment

  # rubocop:disable Security/Eval
  File.open(path, "w") do |f|
    f.write(eval(Erubi::Engine.new(<<~ERB, trim: true).src))
      ---
      title: <%= docs.title.underscore.titleize %>
      id: <%= page_id %>
      ---

      <%= docs.base_docstring %>

      ## Usage

      ```ruby
      <%= docs.tags(:form_usage).first.text %>
      ```

      <% specific_args = specific_args_from(docs.params) %>
      <% unless specific_args.empty? %>
      ## Arguments

      <%= generate_args_table(specific_args) %>
      <% end %>

      ## Common arguments

      <%= generate_args_table(common_args_from(docs.params)) %>

      <% unless documented_methods.empty? %>
      ## Methods

        <% documented_methods.each do |method_docs| %>
      ### `#<%= method_docs.signature.sub(/def /, "") %>`

      <%= method_docs.base_docstring %>

          <% param_tags = method_docs.tags(:param) %>

          <% if param_tags.any? %>

      <%= generate_args_table(param_tags) %>
          <% end %>
        <% end %>
      <% end %>

      <% unless preview_methods.empty? %>
      ## Examples

      <%= preview_erbs.join("\n") %>
      <% end %>
    ERB
  end
  # rubocop:enable Security/Eval
end

#page_idObject



31
32
33
34
35
# File 'lib/primer/yard/lookbook_pages_backend.rb', line 31

def page_id
  @page_id ||= docs.short_name.dasherize.underscore.tap do |page_id|
    page_id << "_input" unless page_id.end_with?("_input")
  end
end