Class: Tapioca::TypeVariableModule

Inherits:
Module
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/tapioca/sorbet_ext/generic_name_patch.rb

Overview

This is subclassing from ‘Module` so that instances of this type will be modules. The reason why we want that is because that means those instances will automatically get bound to the constant names they are assigned to by Ruby. As a result, we don’t need to do any matching of constants to type variables to bind their names, Ruby will do that automatically for us and we get the ‘name` method for free from `Module`.

Defined Under Namespace

Classes: Type

Constant Summary collapse

DEFAULT_BOUNDS_PROC =
T.let(-> { {} }, T.proc.returns(T::Hash[Symbol, T.untyped]))

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Module

#autoload, #autoload_without_tapioca

Constructor Details

#initialize(context, type, variance, bounds_proc) ⇒ TypeVariableModule

Returns a new instance of TypeVariableModule.



146
147
148
149
150
151
152
153
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 146

def initialize(context, type, variance, bounds_proc)
  @context = context
  @type = type
  @variance = variance
  @bounds_proc = bounds_proc || DEFAULT_BOUNDS_PROC

  super()
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



136
137
138
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 136

def type
  @type
end

Instance Method Details

#coerce_to_type_variableObject



181
182
183
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 181

def coerce_to_type_variable
  TypeVariable.new(name, @variance)
end

#fixed?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 161

def fixed?
  bounds.key?(:fixed)
end

#nameObject



155
156
157
158
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 155

def name
  constant_name = super
  constant_name&.split("::")&.last
end

#serializeObject



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 166

def serialize
  fixed = bounds[:fixed].to_s if fixed?
  lower = bounds[:lower].to_s if bounds.key?(:lower)
  upper = bounds[:upper].to_s if bounds.key?(:upper)

  RBIHelper.serialize_type_variable(
    @type.serialize,
    @variance,
    fixed,
    upper,
    lower,
  )
end