Class: Tapioca::Dsl::Compilers::JobIteration

Inherits:
Compiler
  • Object
show all
Extended by:
T::Generic, T::Sig
Defined in:
lib/tapioca/dsl/compilers/job_iteration.rb

Constant Summary collapse

ConstantType =
type_member { { fixed: T.class_of(::JobIteration::Iteration) } }
PARAM_TYPES_IN_ORDER =
[
  RBI::ReqParam,
  RBI::OptParam,
  RBI::RestParam,
  RBI::KwParam,
  RBI::KwOptParam,
  RBI::KwRestParam,
  RBI::BlockParam,
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gather_constantsObject



114
115
116
# File 'lib/tapioca/dsl/compilers/job_iteration.rb', line 114

def gather_constants
  all_classes.select { |c| ::JobIteration::Iteration > c }
end

Instance Method Details

#decorateObject



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
80
81
82
83
84
85
86
87
88
# File 'lib/tapioca/dsl/compilers/job_iteration.rb', line 25

def decorate
  return unless constant.instance_methods(false).include?(:build_enumerator)

  root.create_path(constant) do |job|
    method = constant.instance_method(:build_enumerator)
    constant_name = name_of(constant)
    signature = signature_of(method)

    parameters = compile_method_parameters_to_rbi(method).reject do |typed_param|
      typed_param.param.name == "cursor"
    end

    if signature
      fixed_hash_args = signature.arg_types.select { |arg_type| T::Types::FixedHash === arg_type[1] }.to_h
      expanded_parameters = parameters.flat_map do |typed_param|
        if (hash_type = fixed_hash_args[typed_param.param.name.to_sym])
          hash_type.types.map do |key, value|
            if value.name.start_with?("T.nilable")
              create_kw_opt_param(key.to_s, type: value.to_s, default: "nil")
            else
              create_kw_param(key.to_s, type: value.to_s)
            end
          end
        else
          typed_param
        end
      end
    else
      expanded_parameters = parameters
    end

    # Sorbet expects optional keyword arguments to be after required keyword arguments.
    expanded_parameters.sort_by! { |typed_param| PARAM_TYPES_IN_ORDER.index(typed_param.param.class) }

    number_of_generic_type_members = Tapioca::Runtime::GenericTypeRegistry.lookup_type_variables(constant)&.size

    returned_job_class = if number_of_generic_type_members&.nonzero?
      "#{constant_name}[#{number_of_generic_type_members.times.map { "T.untyped" }.join(", ")}]"
    else
      constant_name
    end

    job.create_method(
      "perform_later",
      parameters: perform_later_parameters(expanded_parameters, returned_job_class),
      return_type: "T.any(#{returned_job_class}, FalseClass)",
      class_method: true,
    )

    job.create_method(
      "perform_now",
      parameters: expanded_parameters,
      return_type: "T.any(NilClass, Exception)",
      class_method: true,
    )

    job.create_method(
      "perform",
      parameters: expanded_parameters,
      return_type: "void",
      class_method: false,
    )
  end
end