Class: Tapioca::TypeVariableModule

Inherits:
Module
  • Object
show all
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



117
118
119
120
121
122
123
124
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 117

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



114
115
116
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 114

def type
  @type
end

Instance Method Details

#coerce_to_type_variableObject

: -> Tapioca::TypeVariable



153
154
155
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 153

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

#fixed?Boolean

: -> bool

Returns:

  • (Boolean)


133
134
135
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 133

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

#nameObject

: -> String?



127
128
129
130
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 127

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

#serializeObject

: -> String



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

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