class Tapioca::Dsl::Compilers::RailsGenerators
~~~
end
def skip_comments; end
sig { returns(T::Boolean)}
def result_type; end
sig { returns(::String)}
class ServiceGenerator
# typed: strong
# service_generator.rbi
~~~rbi
this compiler will produce the RBI file ‘service_generator.rbi` with the following content:
~~~
end
class_option :skip_comments, type: :boolean, default: false
argument :result_type, type: :string
class ServiceGenerator < Rails::Generators::NamedBase
# lib/generators/sample_generator.rb
~~~rb
For example, with the following generator:
`Tapioca::Dsl::Compilers::RailsGenerators` generates RBI files for Rails generators
def base_class_of_constant
def base_class_of_constant ancestor = inherited_ancestors_of(constant).find do |klass| qualified_name_of(klass)&.match?(BUILT_IN_MATCHER) end T.cast(ancestor, T.class_of(::Rails::Generators::Base)) end
def decorate
def decorate base_class = base_class_of_constant arguments = constant.arguments - base_class.arguments class_options = constant.class_options.reject do |name, option| base_class.class_options[name] == option end return if arguments.empty? && class_options.empty? root.create_path(constant) do |klass| arguments.each { |argument| generate_methods_for_argument(klass, argument) } class_options.each { |_name, option| generate_methods_for_argument(klass, option) } end end
def gather_constants
def gather_constants all_classes.select do |const| name = qualified_name_of(const) name && !name.match?(BUILT_IN_MATCHER) && ::Rails::Generators::Base > const end end
def generate_methods_for_argument(klass, argument)
def generate_methods_for_argument(klass, argument) klass.create_method( argument.name, parameters: [], return_type: type_for(argument), ) end
def type_for(arg)
def type_for(arg) type = case arg.type when :array then "T::Array[::String]" when :boolean then "T::Boolean" when :hash then "T::Hash[::String, ::String]" when :numeric then "::Numeric" when :string then "::String" else "T.untyped" end if arg.required || arg.default type else as_nilable_type(type) end end