Class: Ariadne::Yard::LookbookPage

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

Overview

A single Lookbook page.

Constant Summary collapse

PREVIEW_MAP =
{
  Ariadne::Alpha::TextField => [:single_text_field_form, :multi_text_field_form],
  Ariadne::Alpha::TextArea => [],
  Ariadne::Alpha::Select => [:select_form],
  Ariadne::Alpha::MultiInput => [:multi_input_form],
  Ariadne::Alpha::RadioButton => [:radio_button_with_nested_form],
  Ariadne::Alpha::RadioButtonGroup => [:radio_button_group_form],
  Ariadne::Alpha::CheckBox => [:check_box_with_nested_form],
  Ariadne::Alpha::CheckBoxGroup => [:check_box_group_form],
  Ariadne::Alpha::SubmitButton => [:submit_button_form],
  Ariadne::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/ariadne/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/ariadne/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/ariadne/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/ariadne/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
113
114
115
116
117
# File 'lib/ariadne/yard/lookbook_pages_backend.rb', line 37

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

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

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

    "<%= embed Ariadne::FormsPreview, #{preview_method.inspect} %>"
  end
  # 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/ariadne/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