Class: SorbetRails::MailerRbiFormatter

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/sorbet-rails/mailer_rbi_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(mailer_class) ⇒ MailerRbiFormatter

Returns a new instance of MailerRbiFormatter.



9
10
11
12
# File 'lib/sorbet-rails/mailer_rbi_formatter.rb', line 9

def initialize(mailer_class)
  @mailer_class = T.let(mailer_class, T.class_of(ActionMailer::Base))
  @parlour = T.let(Parlour::RbiGenerator.new, Parlour::RbiGenerator)
end

Instance Method Details

#generate_rbiObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sorbet-rails/mailer_rbi_formatter.rb', line 15

def generate_rbi
  puts "-- Generate sigs for mailer #{@mailer_class.name} --"

  @parlour.root.add_comments([
    "This is an autogenerated file for Rails' mailers.",
    'Please rerun bundle exec rake rails_rbi:mailers to regenerate.'
  ])

  @parlour.root.create_class(@mailer_class.name) do |mailer_rbi|
    @mailer_class.action_methods.to_a.sort.each do |mailer_method|
      method_def = @mailer_class.instance_method(mailer_method)
      parameters = SorbetRails::SorbetUtils.parameters_from_method_def(method_def)
      mailer_rbi.create_method(
        mailer_method,
        parameters: parameters,
        return_type: 'ActionMailer::MessageDelivery',
        class_method: true,
      )
    end
  end

  @parlour.rbi
end