Class: T::Types::Simple

Inherits:
Base
  • Object
show all
Defined in:
lib/types/types/simple.rb

Overview

Validates that an object belongs to the specified class.

Defined Under Namespace

Modules: Private

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==, #describe_obj, #error_message_for_obj, #error_message_for_obj_recursive, #hash, method_added, #recursively_valid?, #subtype_of?, #to_s, #validate!

Constructor Details

#initialize(raw_type) ⇒ Simple

Returns a new instance of Simple.



12
13
14
# File 'lib/types/types/simple.rb', line 12

def initialize(raw_type)
  @raw_type = raw_type
end

Instance Attribute Details

#raw_typeObject (readonly)

Returns the value of attribute raw_type.



10
11
12
# File 'lib/types/types/simple.rb', line 10

def raw_type
  @raw_type
end

Instance Method Details

#nameObject

overrides Base



17
18
19
20
21
22
23
# File 'lib/types/types/simple.rb', line 17

def name
  # Memoize to mitigate pathological performance with anonymous modules (https://bugs.ruby-lang.org/issues/11119)
  #
  # `name` isn't normally a hot path for types, but it is used in initializing a T::Types::Union,
  # and so in `T.nilable`, and so in runtime constructions like `x = T.let(nil, T.nilable(Integer))`.
  @name ||= (NAME_METHOD.bind(@raw_type).call || @raw_type.name).freeze
end

#to_nilableObject



62
63
64
65
66
67
# File 'lib/types/types/simple.rb', line 62

def to_nilable
  @nilable ||= T::Private::Types::SimplePairUnion.new(
    self,
    T::Utils::Nilable::NIL_TYPE,
  )
end

#valid?(obj) ⇒ Boolean

overrides Base

Returns:



26
27
28
# File 'lib/types/types/simple.rb', line 26

def valid?(obj)
  obj.is_a?(@raw_type)
end