Class: Tapioca::Dsl::Compilers::ViewComponentSlotables
- Inherits:
-
Tapioca::Dsl::Compiler
- Object
- Tapioca::Dsl::Compiler
- Tapioca::Dsl::Compilers::ViewComponentSlotables
- Extended by:
- T::Sig
- Defined in:
- lib/tapioca/dsl/compilers/view_component_slotables.rb
Overview
Generates RBI for ViewComponent::Slotable See github.com/ViewComponent/view_component/blob/main/lib/view_component/slotable.rb
Constant Summary collapse
- ConstantType =
type_member { { fixed: T.class_of(::ViewComponent::Slotable) } }
Class Method Summary collapse
Instance Method Summary collapse
- #decorate ⇒ Object
- #generate_instance_methods(klass, name, return_type, is_many) ⇒ Object
- #generate_polymorphic_instance_methods(klass, slot_name, underlying_types, is_many) ⇒ Object
- #renderable_to_type_name(input) ⇒ Object
Class Method Details
.gather_constants ⇒ Object
23 24 25 26 |
# File 'lib/tapioca/dsl/compilers/view_component_slotables.rb', line 23 def gather_constants all_classes .select { |c| c < ViewComponent::Slotable && c.name != 'ViewComponent::Base' } end |
Instance Method Details
#decorate ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/tapioca/dsl/compilers/view_component_slotables.rb', line 30 def decorate root.create_path(constant) do |klass| T.unsafe(constant).registered_slots.each do |name, config| is_many = T.let(config[:collection], T::Boolean) module_name = 'ViewComponentSlotablesMethodsModule' klass.create_module(module_name) do |mod| if config[:renderable_hash] generate_polymorphic_instance_methods(mod, name.to_s, config[:renderable_hash], is_many) else return_type = renderable_to_type_name(config[:renderable]) generate_instance_methods(mod, name.to_s, return_type, is_many) end end klass.create_include(module_name) end end end |
#generate_instance_methods(klass, name, return_type, is_many) ⇒ Object
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/tapioca/dsl/compilers/view_component_slotables.rb', line 98 def generate_instance_methods(klass, name, return_type, is_many) return_type_maybe_plural = if is_many "T::Enumerable[#{return_type}]" else return_type end klass.create_method(name, return_type: return_type_maybe_plural) klass.create_method("#{name}?", return_type: 'T::Boolean') klass.create_method( "with_#{name}", parameters: [ create_rest_param('args', type: 'T.untyped'), create_block_param( 'block', type: "T.nilable(T.proc.params(#{name}: #{return_type_maybe_plural}).returns(T.untyped))" ) ], return_type: return_type_maybe_plural ) if is_many # For collection subcomponents, ViewComponent generates methods for the singular version # of the name. singular_name = ActiveSupport::Inflector.singularize(name) klass.create_method( "with_#{singular_name}", parameters: [ create_rest_param('args', type: 'T.untyped'), create_block_param( 'block', type: "T.nilable(T.proc.params(#{singular_name}: #{return_type}).returns(T.untyped))" ) ], return_type: return_type ) klass.create_method( "with_#{singular_name}_content", parameters: [ create_param('content', type: 'T.untyped') ], return_type: 'T.untyped' ) else klass.create_method( "with_#{name}_content", parameters: [ create_param('content', type: 'T.untyped') ], return_type: return_type ) end end |
#generate_polymorphic_instance_methods(klass, slot_name, underlying_types, is_many) ⇒ Object
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 |
# File 'lib/tapioca/dsl/compilers/view_component_slotables.rb', line 70 def generate_polymorphic_instance_methods(klass, slot_name, , is_many) # We want to signularize the slot name since it's used as the namespace singular_slot_name = if is_many ActiveSupport::Inflector.singularize(slot_name) else slot_name end klass.create_method(slot_name, return_type: 'T.untyped') # TODO: should this be T.any of underlying types? klass.create_method("#{slot_name}?", return_type: 'T::Boolean') .each do |name, config| namespaced_name = "#{singular_slot_name}_#{name}" return_type = renderable_to_type_name(config[:renderable]) klass.create_method( "with_#{namespaced_name}", parameters: [ create_rest_param('args', type: 'T.untyped'), create_block_param('block', type: 'T.untyped') ], return_type: return_type ) end end |
#renderable_to_type_name(input) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/tapioca/dsl/compilers/view_component_slotables.rb', line 51 def renderable_to_type_name(input) case input when String input when Class T.must(input.name) else 'T.untyped' end end |