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

#build_typeObject



16
17
18
# File 'lib/types/types/simple.rb', line 16

def build_type
  nil
end

#nameObject

overrides Base



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/types/types/simple.rb', line 21

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))`.
  #
  # Care more about back compat than we do about performance here.
  # Once 2.6 is well in the rear view mirror, we can replace this.
  # rubocop:disable Performance/BindCall
  @name ||= (NAME_METHOD.bind(@raw_type).call || @raw_type.name).freeze
  # rubocop:enable Performance/BindCall
end

#to_nilableObject



71
72
73
74
75
76
# File 'lib/types/types/simple.rb', line 71

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

#valid?(obj) ⇒ Boolean

overrides Base

Returns:



35
36
37
# File 'lib/types/types/simple.rb', line 35

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