Class: Tapioca::Compilers::Shale

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

Constant Summary collapse

ConstantType =
type_member { { fixed: T.class_of(::Shale::Mapper) } }
SHALE_ATTRIBUTE_MODULE =
'ShaleAttributeMethods'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gather_constantsObject



23
24
25
26
# File 'lib/tapioca/dsl/compilers/shale.rb', line 23

def gather_constants
  # Collect all the classes that inherit from Shale::Mapper
  all_classes.select { |c| c < ::Shale::Mapper }
end

Instance Method Details

#decorateObject

: -> void



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
89
90
91
92
93
94
95
# File 'lib/tapioca/dsl/compilers/shale.rb', line 33

def decorate
  # Create a RBI definition for each class that inherits from Shale::Mapper
  root.create_path(constant) do |klass|
    has_shale_builder = includes_shale_builder(constant)
    mod = klass.create_module(SHALE_ATTRIBUTE_MODULE)
    klass.create_include(SHALE_ATTRIBUTE_MODULE)
    # For each attribute defined in the class
    attribute_names = constant.attributes.keys.sort
    attribute_names.each do |attribute_name|
      attribute = constant.attributes[attribute_name] #: ::Shale::Attribute
      if (type = attribute.return_type)
        return_type = type
        nilable = true
      else
        return_type, nilable = shale_type_to_sorbet_return_type(attribute)
      end
      comments = [] #: T::Array[RBI::Comment]
      if shale_builder_defined? && attribute.doc
        comments << RBI::Comment.new(T.must(attribute.doc))
      end

      if attribute.collection?
        getter_without_block_type = wrap_nilable_type(wrap_array_type(return_type))
      elsif nilable
        getter_without_block_type = wrap_nilable_type(return_type)
      else
        getter_without_block_type = return_type.to_s
      end

      if (type = attribute.return_type || attribute.setter_type)
        setter_type = type
        nilable = true
      else
        setter_type, nilable = shale_type_to_sorbet_setter_type(attribute)
      end
      if attribute.collection?
        setter_type_str = wrap_nilable_type(wrap_array_type(setter_type))
      elsif nilable
        setter_type_str = wrap_nilable_type(setter_type)
      else
        setter_type_str = setter_type.to_s
      end

      attribute.all_names.each do |name|
        name_str = name.to_s
        if has_shale_builder && attribute.type < ::Shale::Mapper
          generate_mapper_getter(mod, name_str, return_type, getter_without_block_type, comments)
        else
          mod.create_method(name_str, return_type: getter_without_block_type, comments: comments)
        end

        # setter
        mod.create_method(
          "#{name_str}=",
          parameters: [create_param('value', type: setter_type_str)],
          return_type: setter_type_str,
          comments: comments,
        )
      end
    end
  end

end

#generate_mapper_getter(mod, method_name, type, getter_without_block_type, comments) ⇒ Object

: (RBI::Scope mod, String method_name, Object type, String getter_without_block_type, Array comments) -> void



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/tapioca/dsl/compilers/shale.rb', line 110

def generate_mapper_getter(mod, method_name, type, getter_without_block_type, comments)
  if mod.respond_to?(:create_sig)
    mod = T.unsafe(mod)
    # for tapioca < 0.16.0
    sigs = [] #: Array[RBI::Sig]

    # simple getter
    sigs << mod.create_sig(
      parameters: {
        block: 'NilClass',
      },
      return_type: getter_without_block_type,
    )
    # getter with block
    sigs << mod.create_sig(
      parameters: {
        block: "T.proc.params(arg0: #{type}).void"
      },
      return_type: type.to_s
    )
    mod.create_method_with_sigs(
      method_name,
      sigs: sigs,
      comments: comments,
      parameters: [
        RBI::BlockParam.new('block'),
      ],
    )

    mod.create_method_with_sigs(
      "memo_#{method_name}",
      sigs: [
        mod.create_sig(
          parameters: {
            block: "T.proc.params(arg0: #{type}).void"
          },
          return_type: type.to_s
        )
      ],
      comments: [
        RBI::Comment.new("Version of `#{method_name}` that memoizes the previous object if it was already present.\n"),
        *comments,
      ],
      parameters: [
        RBI::BlockParam.new('block'),
      ],
    )
  else
    # for tapioca >= 0.16.0
    mod.create_method(method_name, comments: comments) do |method|
      method.add_block_param('block')

      method.add_sig do |sig|
        sig.add_param('block', 'NilClass')
        sig.return_type = getter_without_block_type
      end

      method.add_sig do |sig|
        sig.add_param('block', "T.proc.params(arg0: #{type}).void")
        sig.return_type = type.to_s
      end
    end

    mod.create_method(
      "memo_#{method_name}",
      comments: [
        RBI::Comment.new("Version of `#{method_name}` that memoizes the previous object if it was already present.\n"),
        *comments,
      ],
    ) do |method|
      method.add_block_param('block')

      method.add_sig do |sig|
        sig.add_param('block', "T.proc.params(arg0: #{type}).void")
        sig.return_type = type.to_s
      end
    end
  end
end

#wrap_array_type(type) ⇒ Object

: (untyped type) -> String



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

def wrap_array_type(type)
  "T::Array[#{type}]"
end

#wrap_nilable_type(type) ⇒ Object

: (untyped type) -> String



98
99
100
101
102
# File 'lib/tapioca/dsl/compilers/shale.rb', line 98

def wrap_nilable_type(type)
  return "T.nilable(#{type})" if type != T.untyped

  T.unsafe(type).to_s
end