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



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

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

Instance Method Details

#decorateObject



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

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



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

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 = T.let([], T::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')],
    )
  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
  end
end

#wrap_array_type(type) ⇒ Object



103
104
105
# File 'lib/tapioca/dsl/compilers/shale.rb', line 103

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

#wrap_nilable_type(type) ⇒ Object



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

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

  T.unsafe(type).to_s
end