File: docs_builder_component.rb

package info (click to toggle)
ruby-view-component 4.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,832 kB
  • sloc: ruby: 8,385; sh: 166; makefile: 4
file content (99 lines) | stat: -rw-r--r-- 2,252 bytes parent folder | download
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
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
# frozen_string_literal: true

module ViewComponent
  class DocsBuilderComponent < Base
    class Section < Struct.new(:heading, :methods, :error_klasses, :show_types, keyword_init: true)
      def initialize(heading: nil, methods: [], error_klasses: [], show_types: true)
        methods.sort_by! { |method| method[:name] }
        error_klasses.sort!
        super
      end
    end

    class ErrorKlassDoc < ViewComponent::Base
      def initialize(error_klass, _show_types)
        @error_klass = error_klass
      end

      def klass_name
        @error_klass.gsub("ViewComponent::", "").gsub("::MESSAGE", "")
      end

      def error_message
        ViewComponent.const_get(@error_klass)
      end

      def call
        <<~DOCS.chomp.html_safe
          `#{klass_name}`

          #{error_message}
        DOCS
      end
    end

    class MethodDoc < ViewComponent::Base
      def initialize(method, show_types = true)
        @method = method
        @show_types = show_types
      end

      def deprecated?
        @method.tag(:deprecated).present?
      end

      def suffix
        " (Deprecated)" if deprecated?
      end

      def types
        " → [#{@method.tag(:return).types.join(",")}]" if @method.tag(:return)&.types && @show_types
      end

      def signature_or_name
        @method.signature ? @method.signature.gsub("def ", "") : @method.name
      end

      def separator
        @method.sep
      end

      def docstring
        @method.docstring
      end

      # Not covered as we have no deprecated methods
      # :nocov:
      def deprecation_text
        @method.tag(:deprecated)&.text
      end
      # :nocov:

      def docstring_and_deprecation_text
        <<~DOCS.strip.html_safe
          #{docstring}

          #{"_#{deprecation_text}_" if deprecated?}
        DOCS
      end

      def call
        <<~DOCS.chomp.html_safe
          `#{separator}#{signature_or_name}`#{types}#{suffix}

          #{docstring_and_deprecation_text}
        DOCS
      end
    end

    # { heading: String, public_only: Boolean, show_types: Boolean}
    def initialize(sections: [])
      @sections = sections
    end

    # deprecation
    # return
    # only public methods
    # sig with types or name
  end
end