Class: SorbetRails::JobRbiFormatter

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

Constant Summary collapse

Parameter =
::Parlour::RbiGenerator::Parameter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_class) ⇒ JobRbiFormatter

Returns a new instance of JobRbiFormatter.



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

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

Instance Attribute Details

#job_classObject (readonly)

Returns the value of attribute job_class.



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

def job_class
  @job_class
end

#rbi_generatorObject (readonly)

Returns the value of attribute rbi_generator.



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

def rbi_generator
  @rbi_generator
end

Instance Method Details

#generate_rbiObject



82
83
84
85
86
# File 'lib/sorbet-rails/job_rbi_formatter.rb', line 82

def generate_rbi
  puts "-- Generate sigs for mailer #{@job_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
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
# File 'lib/sorbet-rails/job_rbi_formatter.rb', line 24

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

  @rbi_generator.root.create_class(T.must(@job_class.name)) do |job_rbi|
    method_def = @job_class.instance_method(:perform)
    parameters = SorbetRails::SorbetUtils.parameters_from_method_def(method_def)
    job_rbi.create_method(
      "perform_later",
      parameters: parameters,
      class_method: true,
      return_type: @job_class.name,
    )
    job_rbi.create_method(
      "perform_now",
      parameters: parameters,
      class_method: true,
      return_type: @job_class.name,
    )
    # Override the signature for "set" so that we can support it
    # At run-time, this method returns a `ActiveJob::ConfiguredJob` but
    # we fake the signature to return "T.self_class" so that
    # sorbet can type-check when `perform_later` is called on it
    # See: https://guides.rubyonrails.org/active_job_basics.html#enqueue-the-job
    job_rbi.create_method(
      "set",
      # Documentation: https://api.rubyonrails.org/classes/ActiveJob/Core/ClassMethods.html
      parameters: [
        Parameter.new(
          "wait:",
          type: "T.nilable(ActiveSupport::Duration)",
          default: "nil",
        ),
        Parameter.new(
          "wait_until:",
          type: "T.nilable(T.any(ActiveSupport::TimeWithZone, Date, Time))",
          default: "nil",
        ),
        Parameter.new(
          "queue:",
          type: "T.nilable(T.any(String, Symbol))",
          default: "nil",
        ),
        Parameter.new(
          "priority:",
          type: "T.nilable(Integer)",
          default: "nil",
        ),
      ],
      return_type: "T.self_type",
      class_method: true,
    )
  end
end