Class: SorbetRails::MailerRbiFormatter

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

Constant Summary collapse

Parameter =
::Parlour::RbiGenerator::Parameter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mailer_class) ⇒ MailerRbiFormatter

Returns a new instance of MailerRbiFormatter.


18
19
20
21
# File 'lib/sorbet-rails/mailer_rbi_formatter.rb', line 18

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

Instance Attribute Details

#mailer_classObject (readonly)

Returns the value of attribute mailer_class


15
16
17
# File 'lib/sorbet-rails/mailer_rbi_formatter.rb', line 15

def mailer_class
  @mailer_class
end

#rbi_generatorObject (readonly)

Returns the value of attribute rbi_generator


12
13
14
# File 'lib/sorbet-rails/mailer_rbi_formatter.rb', line 12

def rbi_generator
  @rbi_generator
end

Instance Method Details

#generate_rbiObject


45
46
47
48
49
# File 'lib/sorbet-rails/mailer_rbi_formatter.rb', line 45

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

#populate_rbiObject


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sorbet-rails/mailer_rbi_formatter.rb', line 24

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

  @rbi_generator.root.create_class(T.must(@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.to_s,
        parameters: parameters,
        return_type: 'ActionMailer::MessageDelivery',
        class_method: true,
      )
    end
  end
end