@@ -73,19 +73,19 @@ class Student
73
73
include FrozenRecordAttributeMethods
74
74
75
75
module FrozenRecordAttributeMethods
76
- sig { returns(String ) }
76
+ sig { returns(T::untyped ) }
77
77
def first_name; end
78
78
79
79
sig { returns(T::Boolean) }
80
80
def first_name?; end
81
81
82
- sig { returns(Integer ) }
82
+ sig { returns(T::untyped ) }
83
83
def id; end
84
84
85
85
sig { returns(T::Boolean) }
86
86
def id?; end
87
87
88
- sig { returns(String ) }
88
+ sig { returns(T::untyped ) }
89
89
def last_name; end
90
90
91
91
sig { returns(T::Boolean) }
@@ -101,13 +101,66 @@ def last_name?; end
101
101
add_ruby_file ( "student.rb" , <<~RUBY )
102
102
# typed: strong
103
103
104
+ class ArrayOfType < ActiveModel::Type::Value
105
+ attr_reader :element_type
106
+
107
+ def initialize(element_type:)
108
+ super()
109
+ @element_type = element_type
110
+ end
111
+
112
+ def type
113
+ :array
114
+ end
115
+ end
116
+
117
+ class HashOfType < ActiveModel::Type::Value
118
+ attr_reader :key_type
119
+ attr_reader :value_type
120
+
121
+ def initialize(key_type:, value_type:)
122
+ super()
123
+ @key_type = key_type
124
+ @value_type = value_type
125
+ end
126
+
127
+ def type
128
+ :hash
129
+ end
130
+ end
131
+
132
+ class SymbolType < ActiveModel::Type::Value
133
+ def type
134
+ :symbol
135
+ end
136
+ end
137
+
138
+ ActiveModel::Type.register(:array_of_type, ArrayOfType)
139
+ ActiveModel::Type.register(:hash_of_type, HashOfType)
140
+ ActiveModel::Type.register(:symbol, SymbolType)
141
+
104
142
class Student < FrozenRecord::Base
105
- extend(T::Sig)
143
+ extend T::Sig
144
+ include ActiveModel::Attributes
145
+
146
+ # specifically missing the id field, should be untyped
147
+ attribute :first_name, :string
148
+ attribute :last_name, :string
149
+ attribute :age, :integer
150
+ attribute :location, :string
151
+ attribute :is_cool_person, :boolean
152
+ attribute :birth_date, :date
153
+ attribute :updated_at, :time
154
+ # custom attribute types
155
+ attribute :favourite_foods, :array_of_type, element_type: :string
156
+ attribute :skills, :hash_of_type, key_type: :symbol, value_type: :string
157
+ # attribute with a default, shouldn't be nilable
158
+ attribute :shirt_size, :symbol
106
159
107
160
self.base_path = __dir__
108
-
109
161
self.default_attributes = { shirt_size: :large }
110
162
163
+ # Explicit method, shouldn't be in the RBI output
111
164
sig { params(grain: Symbol).returns(String) }
112
165
def area(grain:)
113
166
parts = location.split(',').map(&:strip)
@@ -161,67 +214,67 @@ class Student
161
214
include FrozenRecordAttributeMethods
162
215
163
216
module FrozenRecordAttributeMethods
164
- sig { returns(Integer) }
217
+ sig { returns(T.nilable(:: Integer) ) }
165
218
def age; end
166
219
167
220
sig { returns(T::Boolean) }
168
221
def age?; end
169
222
170
- sig { returns(Date) }
223
+ sig { returns(T.nilable(:: Date) ) }
171
224
def birth_date; end
172
225
173
226
sig { returns(T::Boolean) }
174
227
def birth_date?; end
175
228
176
- sig { returns(Array) }
229
+ sig { returns(T.nilable(:: Array) ) }
177
230
def favourite_foods; end
178
231
179
232
sig { returns(T::Boolean) }
180
233
def favourite_foods?; end
181
234
182
- sig { returns(String) }
235
+ sig { returns(T.nilable(:: String) ) }
183
236
def first_name; end
184
237
185
238
sig { returns(T::Boolean) }
186
239
def first_name?; end
187
240
188
- sig { returns(Integer ) }
241
+ sig { returns(T.untyped ) }
189
242
def id; end
190
243
191
244
sig { returns(T::Boolean) }
192
245
def id?; end
193
246
194
- sig { returns(T::Boolean) }
247
+ sig { returns(T.nilable(T ::Boolean) ) }
195
248
def is_cool_person; end
196
249
197
250
sig { returns(T::Boolean) }
198
251
def is_cool_person?; end
199
252
200
- sig { returns(String) }
253
+ sig { returns(T.nilable(:: String) ) }
201
254
def last_name; end
202
255
203
256
sig { returns(T::Boolean) }
204
257
def last_name?; end
205
258
206
- sig { returns(String) }
259
+ sig { returns(T.nilable(:: String) ) }
207
260
def location; end
208
261
209
262
sig { returns(T::Boolean) }
210
263
def location?; end
211
264
212
- sig { returns(Symbol) }
265
+ sig { returns(:: Symbol) }
213
266
def shirt_size; end
214
267
215
268
sig { returns(T::Boolean) }
216
269
def shirt_size?; end
217
270
218
- sig { returns(Hash) }
271
+ sig { returns(T.nilable(:: Hash) ) }
219
272
def skills; end
220
273
221
274
sig { returns(T::Boolean) }
222
275
def skills?; end
223
276
224
- sig { returns(Time ) }
277
+ sig { returns(T.nilable(::DateTime) ) }
225
278
def updated_at; end
226
279
227
280
sig { returns(T::Boolean) }
@@ -257,13 +310,13 @@ class Student
257
310
extend GeneratedRelationMethods
258
311
259
312
module FrozenRecordAttributeMethods
260
- sig { returns(String ) }
313
+ sig { returns(T::untyped ) }
261
314
def course; end
262
315
263
316
sig { returns(T::Boolean) }
264
317
def course?; end
265
318
266
- sig { returns(Integer ) }
319
+ sig { returns(T::untyped ) }
267
320
def id; end
268
321
269
322
sig { returns(T::Boolean) }
0 commit comments