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

Inherits:
Compiler
  • Object
show all
Extended by:
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::Param,
  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



105
106
107
# File 'lib/tapioca/dsl/compilers/job_iteration.rb', line 105

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

Instance Method Details

#decorateObject



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/tapioca/dsl/compilers/job_iteration.rb', line 24

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) }

    job.create_method(
      "perform_later",
      parameters: perform_later_parameters(expanded_parameters, constant_name),
      return_type: "T.any(#{constant_name}, 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