Class: T::Types::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/types/types/base.rb
Direct Known Subclasses
Private::Types::NotTyped, Private::Types::StringHolder, Private::Types::Void, ClassOf, Enum, FixedArray, FixedHash, Intersection, NoReturn, OpusEnum, Proc, SelfType, Simple, TypeParameter, TypeVariable, TypedEnumerable, Union, Untyped
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.method_added(method_name) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/types/types/base.rb', line 6
def self.method_added(method_name)
super(method_name)
if method_name == :subtype_of? && self != T::Types::Base
raise "`subtype_of?` should not be overridden. You probably want to override " \
"`subtype_of_single?` instead."
end
end
|
Instance Method Details
#==(other) ⇒ Object
Also known as:
eql?
136
137
138
|
# File 'lib/types/types/base.rb', line 136
def ==(other)
other.class == self.class && other.name == self.name
end
|
#describe_obj(obj) ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/types/types/base.rb', line 97
def describe_obj(obj)
case obj
when nil, true, false
return "type #{obj.class}"
end
begin
if obj.method(:inspect).owner == Kernel
"type #{obj.class} with hash #{obj.hash}"
else
"type #{obj.class} with value #{T::Utils.string_truncate_middle(obj.inspect, 30, 30)}"
end
rescue StandardError, SystemStackError
"type #{obj.class} with unprintable value"
end
end
|
#error_message_for_obj(obj) ⇒ Object
117
118
119
120
121
122
123
|
# File 'lib/types/types/base.rb', line 117
def error_message_for_obj(obj)
if valid?(obj)
nil
else
"Expected type #{self.name}, got #{describe_obj(obj)}"
end
end
|
#hash ⇒ Object
Equality methods (necessary for deduping types with ‘uniq`)
132
133
134
|
# File 'lib/types/types/base.rb', line 132
def hash
name.hash
end
|
#name ⇒ Object
Equality is based on name, so be sure the name reflects all relevant state when implementing.
32
33
34
|
# File 'lib/types/types/base.rb', line 32
def name
raise NotImplementedError
end
|
#subtype_of?(t2) ⇒ Boolean
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/types/types/base.rb', line 41
def subtype_of?(t2)
t1 = self
if t1.is_a?(T::Types::Union) return t1.types.all? {|t1_member| t1_member.subtype_of?(t2)}
end
if t2.is_a?(T::Types::Intersection) return t2.types.all? {|t2_member| t1.subtype_of?(t2_member)}
end
if t2.is_a?(T::Types::Union)
if t1.is_a?(T::Types::Intersection) return t2.types.any? {|t2_member| t1.subtype_of?(t2_member)} ||
t1.types.any? {|t1_member| t1_member.subtype_of?(t2)}
end
return t2.types.any? {|t2_member| t1.subtype_of?(t2_member)} end
if t1.is_a?(T::Types::Intersection) return t1.types.any? {|t1_member| t1_member.subtype_of?(t2)}
end
if t1.is_a?(T::Private::Types::Void)
return t2.is_a?(T::Private::Types::Void)
end
if t1.is_a?(T::Types::Untyped) || t2.is_a?(T::Types::Untyped)
return true
end
subtype_of_single?(t2)
end
|
#to_s ⇒ Object
93
94
95
|
# File 'lib/types/types/base.rb', line 93
def to_s
name
end
|
#valid?(obj) ⇒ Boolean
19
20
21
|
# File 'lib/types/types/base.rb', line 19
def valid?(obj)
raise NotImplementedError
end
|
#validate!(obj) ⇒ Object
125
126
127
128
|
# File 'lib/types/types/base.rb', line 125
def validate!(obj)
err = error_message_for_obj(obj)
raise TypeError.new(err) if err
end
|