Class: Tapioca::Compilers::Dsl::SidekiqWorker

Inherits:
Base
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/tapioca/compilers/dsl/sidekiq_worker.rb

Overview

‘Tapioca::Compilers::Dsl::SidekiqWorker` generates RBI files classes that include [`Sidekiq::Worker`](github.com/mperham/sidekiq/wiki/Getting-Started).

For example, with the following class that includes ‘Sidekiq::Worker`:

~~~rb class NotifierWorker

include Sidekiq::Worker
def perform(customer_id)
  # ...
end

end ~~~

this generator will produce the RBI file ‘notifier_worker.rbi` with the following content:

~~~rbi # notifier_worker.rbi # typed: true class NotifierWorker

sig { params(customer_id: T.untyped).returns(String) }
def self.perform_async(customer_id); end

sig { params(interval: T.any(DateTime, Time), customer_id: T.untyped).returns(String) }
def self.perform_at(interval, customer_id); end

sig { params(interval: Numeric, customer_id: T.untyped).returns(String) }
def self.perform_in(interval, customer_id); end

end ~~~

Constant Summary

Constants included from Reflection

Reflection::ANCESTORS_METHOD, Reflection::CLASS_METHOD, Reflection::CONSTANTS_METHOD, Reflection::EQUAL_METHOD, Reflection::METHOD_METHOD, Reflection::NAME_METHOD, Reflection::OBJECT_ID_METHOD, Reflection::PRIVATE_INSTANCE_METHODS_METHOD, Reflection::PROTECTED_INSTANCE_METHODS_METHOD, Reflection::PUBLIC_INSTANCE_METHODS_METHOD, Reflection::SINGLETON_CLASS_METHOD, Reflection::SUPERCLASS_METHOD

Instance Attribute Summary

Attributes inherited from Base

#errors, #processable_constants

Instance Method Summary collapse

Methods inherited from Base

#add_error, #handles?, #initialize

Methods included from Reflection

#ancestors_of, #are_equal?, #class_of, #constants_of, #descendants_of, #inherited_ancestors_of, #method_of, #name_of, #name_of_type, #object_id_of, #private_instance_methods_of, #protected_instance_methods_of, #public_instance_methods_of, #qualified_name_of, #signature_of, #singleton_class_of, #superclass_of

Constructor Details

This class inherits a constructor from Tapioca::Compilers::Dsl::Base

Instance Method Details

#decorate(root, constant) ⇒ Object



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
# File 'lib/tapioca/compilers/dsl/sidekiq_worker.rb', line 47

def decorate(root, constant)
  return unless constant.instance_methods.include?(:perform)

  root.create_path(constant) do |worker|
    method_def = constant.instance_method(:perform)

    async_params = compile_method_parameters_to_rbi(method_def)

    # `perform_at` and is just an alias for `perform_in` so both methods technically
    # accept a datetime, time, or numeric but we're typing them differently so they
    # semantically make sense.
    at_params = [
      create_param("interval", type: "T.any(DateTime, Time)"),
      *async_params,
    ]
    in_params = [
      create_param("interval", type: "Numeric"),
      *async_params,
    ]

    generate_perform_method(constant, worker, "perform_async", async_params)
    generate_perform_method(constant, worker, "perform_at", at_params)
    generate_perform_method(constant, worker, "perform_in", in_params)
  end
end

#gather_constantsObject



74
75
76
# File 'lib/tapioca/compilers/dsl/sidekiq_worker.rb', line 74

def gather_constants
  all_classes.select { |c| c < Sidekiq::Worker }
end