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 =

: ^-> Hash[Symbol, 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

: (Module context, Type type, Symbol variance, (^-> Hash[Symbol, untyped])? bounds_proc) -> void



139
140
141
142
143
144
145
146
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 139

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)

: 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

: -> Tapioca::TypeVariable



175
176
177
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 175

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

#fixed?Boolean

: -> bool

Returns:

  • (Boolean)


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

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

#nameObject

: -> String?



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

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

#serializeObject

: -> String



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 160

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