Class: Tapioca::Compilers::PhlexController

Inherits:
Dsl::Compiler
  • Object
show all
Defined in:
lib/tapioca/dsl/compilers/phlex_controller.rb

Constant Summary collapse

ConstantType =
type_member { { fixed: T.class_of(Phlex::Stimulus::Components::Controller) } }
MOD_NAME =

Signature:

  • String

'PhlexControllerMethods'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gather_constantsObject

Signature:

  • -> T::Enumerable[Module]



14
15
16
# File 'lib/tapioca/dsl/compilers/phlex_controller.rb', line 14

def gather_constants
  all_classes.select { |c| c < Phlex::Stimulus::Components::Controller }
end

Instance Method Details

#decorateObject

Signature:

  • -> void



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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/tapioca/dsl/compilers/phlex_controller.rb', line 23

def decorate
  root.create_path(constant) do |klass|
    klass.comments << RBI::Comment.new(
      <<~DOC,
        A Phlex component that wraps the `#{constant.controller_name.inspect}` Stimulus controller.

        LINK: #{constant.controller_path}
      DOC
    )

    mod = klass.create_module(MOD_NAME)
    klass.create_extend(MOD_NAME)

    actions = constant.action_defs.sort_by(&:action_name)
    actions.each do |action|
      comments = [
        RBI::Comment.new(
          <<~DOC,
            Returns the identifier of `#{action.action_name}`
            action on `#{action.component.controller_name.inspect}` Stimulus controller

            Result: `#{action.full_name.inspect}`
          DOC
        ),
      ]
      mod.create_method(
        action.ruby_action_method_name,
        return_type: 'String',
        comments:    comments,
      )

      comments = [
        RBI::Comment.new(
          <<~DOC,
            Returns na anchor hash that can be used to attach `#{action.action_name}`
            action on `#{action.component.controller_name.inspect}` Stimulus controller
            to a particular DOM event.
          DOC
        ),
      ]
      mod.create_method(
        action.ruby_on_method_name,
        comments:    comments,
      ) do |method|
        method.add_param('event_name')
        action.params.each do |param|
          if param.optional
            method.add_kw_opt_param(param.param_name, 'nil')
          else
            method.add_kw_param(param.param_name)
          end
        end

        method.add_sig do |sig|
          sig.return_type = 'T::Hash[T.any(Symbol, String), String]'

          sig.add_param('event_name', 'String')
          action.params.each do |param|
            if param.optional
              sig.add_param(param.param_name, 'T.nilable(String)')
            else
              sig.add_param(param.param_name, 'String')
            end
          end
        end
      end
    end

    targets = constant.target_defs.sort_by(&:target_name)
    targets.each do |target|
      comments = [
        RBI::Comment.new(
          <<~DOC,
            Returns the identifier of `#{target.target_name}`
            target on `#{target.component.controller_name.inspect}` Stimulus controller

            Result: `#{target.target_name.inspect}`
          DOC
        ),
      ]
      mod.create_method(
        target.ruby_target_method_name,
        return_type: 'String',
        comments:    comments,
      )

      comments = [
        RBI::Comment.new(
          <<~DOC,
            Returns an anchor hash for `#{target.target_name}`
            target on `#{target.component.controller_name.inspect}` Stimulus controller
            that can be used to attach this target to a particular DOM element.

            Result: `{ #{constant.target_key} => #{target.target_name.inspect} }`
          DOC
        ),
      ]
      mod.create_method(
        target.ruby_target_anchor_method_name,
        return_type: 'T::Hash[String, String]',
        comments:    comments,
      )
    end

  end

end