Module: T::Utils::Private

Defined in:
lib/types/utils.rb

Class Method Summary collapse

Class Method Details

.coerce_and_check_module_types(val, check_val, check_module_type) ⇒ Object

NOTE: the Module and SimplePairUnion branches of this method are inlined for speed in several hot paths. The ‘T.cast` / `T.let` / `T.bind` / `T.assert_type!` happy paths in `_types.rb` inline the value check and return early on success; `T::Private::Casts.cast` is only reached after that check has already failed, so it reproduces the coercion branches but skips the (now always-false) value check. If you change the behavior here, update those callers too.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/types/utils.rb', line 13

def self.coerce_and_check_module_types(val, check_val, check_module_type)
  # rubocop:disable Style/CaseLikeIf
  if val.is_a?(T::Types::Base)
    if val.is_a?(T::Private::Types::TypeAlias)
      val.aliased_type
    else
      val
    end
  elsif val.is_a?(Module)
    if check_module_type && check_val.is_a?(val)
      nil
    else
      T::Types::Simple::Private::Pool.type_for_module(val)
    end
  elsif val.is_a?(::Array)
    T::Types::FixedArray.new(val)
  elsif val.is_a?(::Hash)
    T::Types::FixedHash.new(val)
  elsif val.is_a?(T::Private::Methods::DeclBuilder)
    T::Private::Methods.finalize_proc(val.decl)
  elsif val.is_a?(::T::Enum)
    T::Types::TEnum.new(val)
  elsif val.is_a?(::String)
    raise "Invalid String literal for type constraint. Must be an #{T::Types::Base}, a " \
          "class/module, or an array. Got a String with value `#{val}`."
  else
    raise "Invalid value for type constraint. Must be an #{T::Types::Base}, a " \
          "class/module, or an array. Got a `#{val.class}`."
  end
  # rubocop:enable Style/CaseLikeIf
end