Class: Tapioca::Compilers::RailsOnSorbetActiveRecordSerializer

Inherits:
Dsl::Compiler
  • Object
show all
Defined in:
lib/tapioca/dsl/compilers/rails_on_sorbet_active_record_serializer.rb

Overview

Creates .rbi files with types for classes that use ActiveRecord serializers : [ConstantType < Class & ::Rails::On::Sorbet::ActiveRecordSerializer]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gather_constantsObject



12
13
14
15
16
# File 'lib/tapioca/dsl/compilers/rails_on_sorbet_active_record_serializer.rb', line 12

def gather_constants
  all_classes.select do |klass|
    klass.singleton_class < ::Rails::On::Sorbet::ActiveRecordSerializer
  end
end

Instance Method Details

#decorateObject

: -> void



21
22
23
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
# File 'lib/tapioca/dsl/compilers/rails_on_sorbet_active_record_serializer.rb', line 21

def decorate
  root.create_path(constant) do |klass|
    constant._sorbet_serializer_definitions.sort.each do |name, definition|
      return_type = definition.return_type || definition.coder.try(:return_type) || T.untyped
      setter_type = definition.setter_type || definition.coder.try(:setter_type) || return_type

      return_type_string =
        if return_type == T.unsafe(T.untyped)
          return_type.to_s
        else
          "T.nilable(#{return_type})"
        end

      setter_type_string =
        if setter_type == T.unsafe(T.untyped)
          setter_type.to_s
        else
          "T.nilable(#{setter_type})"
        end

      doc = definition.doc
      comments = []
      comments << RBI::Comment.new(doc) if doc

      klass.create_method(
        name.to_s,
        comments:,
        return_type: return_type_string,
      )
      klass.create_method(
        "#{name}=",
        comments:,
        parameters:  [create_param('value', type: setter_type_string)],
        return_type: setter_type_string,
      )
    end

  end

end