-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
class_of.rb
55 lines (47 loc) 路 1.06 KB
/
class_of.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true
# typed: true
module T::Types
# Validates that an object belongs to the specified class.
class ClassOf < Base
attr_reader :type
def initialize(type)
@type = type
end
def build_type
nil
end
# overrides Base
def name
"T.class_of(#{@type})"
end
# overrides Base
def valid?(obj)
obj.is_a?(Module) && obj <= @type
end
# overrides Base
def subtype_of_single?(other)
case other
when ClassOf
@type <= other.type
when Simple
@type.is_a?(other.raw_type)
when TypedClass
true
else
false
end
end
# overrides Base
def describe_obj(obj)
obj.inspect
end
# So that `T.class_of(...)[...]` syntax is valid.
# Mirrors the definition of T::Generic#[] (generics are erased).
#
# We avoid simply writing `include T::Generic` because we don't want any of
# the other methods to appear (`T.class_of(A).type_member` doesn't make sense)
def [](*types)
self
end
end
end