Module: TypeAssertImpl

Included in:
TA
Defined in:
lib/sorbet-rails/type_assert/type_assert_impl.rb

Defined Under Namespace

Classes: Unsupported

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sorbet-rails/type_assert/type_assert_impl.rb', line 5

def self.included(klass)
  klass.define_singleton_method(:[]) do |*types|
    if types.length != 1
      raise Unsupported.new("TypeAssert only supports 1 type, given #{types}")
    end
    type = types.first
    return Class.new do
      include ITypeAssert

      define_method(:to_s) { "TA[#{type.to_s}]" }

      define_method(:assert) do |val|
        T.let(val, type)
      end

      define_method(:get_type) { type }
    end
  end
end