Skip to content

Commit 1e96598

Browse files
committed
Infer types from the first record
1 parent 7872cf2 commit 1e96598

File tree

2 files changed

+66
-39
lines changed

2 files changed

+66
-39
lines changed

lib/tapioca/dsl/compilers/frozen_record.rb

+4-8
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,18 @@ def decorate
7272
attributes = constant.attributes
7373
return if attributes.empty?
7474

75+
instance = constant.first
76+
7577
root.create_path(constant) do |record|
7678
module_name = "FrozenRecordAttributeMethods"
7779

7880
record.create_module(module_name) do |mod|
79-
extra_methods = constant.instance_methods(false) - attributes.to_a.map(&:to_sym)
8081
attributes.each do |attribute|
81-
return_type = compile_method_return_type_to_rbi(constant.instance_method(attribute))
82+
return_type = instance.attributes[attribute].class.name
83+
return_type = "T::Boolean" if ["FalseClass", "TrueClass"].include?(return_type)
8284
mod.create_method("#{attribute}?", return_type: "T::Boolean")
8385
mod.create_method(attribute.to_s, return_type: return_type)
8486
end
85-
extra_methods.each do |method|
86-
method_def = constant.instance_method(method)
87-
parameters = compile_method_parameters_to_rbi(method_def)
88-
return_type = compile_method_return_type_to_rbi(method_def)
89-
mod.create_method(method.to_s, return_type: return_type, parameters: parameters)
90-
end
9187
end
9288

9389
record.create_include(module_name)

spec/tapioca/dsl/compilers/frozen_record_spec.rb

+62-31
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,19 @@ class Student
7373
include FrozenRecordAttributeMethods
7474
7575
module FrozenRecordAttributeMethods
76-
sig { returns(T.untyped) }
76+
sig { returns(String) }
7777
def first_name; end
7878
7979
sig { returns(T::Boolean) }
8080
def first_name?; end
8181
82-
sig { returns(T.untyped) }
82+
sig { returns(Integer) }
8383
def id; end
8484
8585
sig { returns(T::Boolean) }
8686
def id?; end
8787
88-
sig { returns(T.untyped) }
88+
sig { returns(String) }
8989
def last_name; end
9090
9191
sig { returns(T::Boolean) }
@@ -106,25 +106,7 @@ class Student < FrozenRecord::Base
106106
107107
self.base_path = __dir__
108108
109-
sig { returns(String) }
110-
def first_name
111-
super
112-
end
113-
114-
sig { returns(String) }
115-
def last_name
116-
super
117-
end
118-
119-
sig { returns(String) }
120-
def location
121-
super
122-
end
123-
124-
sig { returns(Integer) }
125-
def age
126-
return super + 5
127-
end
109+
self.default_attributes = { shirt_size: :large }
128110
129111
sig { params(grain: Symbol).returns(String) }
130112
def area(grain:)
@@ -149,11 +131,27 @@ def area(grain:)
149131
last_name: Smith
150132
age: 19
151133
location: Ottawa, Ontario, Canada
134+
is_cool_person: no
135+
birth_date: 1867-07-01
136+
updated_at: 2014-02-24T19:08:06-05:00
137+
favourite_foods:
138+
- Pizza
139+
skills:
140+
backend: Ruby
141+
frontend: HTML
152142
- id: 2
153143
first_name: Dan
154144
last_name: Lord
155145
age: 20
156146
location: Toronto, Ontario, Canada
147+
is_cool_person: yes
148+
birth_date: 1967-07-01
149+
updated_at: 2015-02-24T19:08:06-05:00
150+
favourite_foods:
151+
- Tacos
152+
skills:
153+
backend: Ruby
154+
frontend: CSS
157155
YAML
158156

159157
expected = <<~RBI
@@ -163,38 +161,71 @@ class Student
163161
include FrozenRecordAttributeMethods
164162
165163
module FrozenRecordAttributeMethods
166-
sig { returns(::Integer) }
164+
sig { returns(Integer) }
167165
def age; end
168166
169167
sig { returns(T::Boolean) }
170168
def age?; end
171169
172-
sig { params(grain: ::Symbol).returns(::String) }
173-
def area(grain:); end
170+
sig { returns(Date) }
171+
def birth_date; end
174172
175-
sig { returns(::String) }
173+
sig { returns(T::Boolean) }
174+
def birth_date?; end
175+
176+
sig { returns(Array) }
177+
def favourite_foods; end
178+
179+
sig { returns(T::Boolean) }
180+
def favourite_foods?; end
181+
182+
sig { returns(String) }
176183
def first_name; end
177184
178185
sig { returns(T::Boolean) }
179186
def first_name?; end
180187
181-
sig { returns(T.untyped) }
188+
sig { returns(Integer) }
182189
def id; end
183190
184191
sig { returns(T::Boolean) }
185192
def id?; end
186193
187-
sig { returns(::String) }
194+
sig { returns(T::Boolean) }
195+
def is_cool_person; end
196+
197+
sig { returns(T::Boolean) }
198+
def is_cool_person?; end
199+
200+
sig { returns(String) }
188201
def last_name; end
189202
190203
sig { returns(T::Boolean) }
191204
def last_name?; end
192205
193-
sig { returns(::String) }
206+
sig { returns(String) }
194207
def location; end
195208
196209
sig { returns(T::Boolean) }
197210
def location?; end
211+
212+
sig { returns(Symbol) }
213+
def shirt_size; end
214+
215+
sig { returns(T::Boolean) }
216+
def shirt_size?; end
217+
218+
sig { returns(Hash) }
219+
def skills; end
220+
221+
sig { returns(T::Boolean) }
222+
def skills?; end
223+
224+
sig { returns(Time) }
225+
def updated_at; end
226+
227+
sig { returns(T::Boolean) }
228+
def updated_at?; end
198229
end
199230
end
200231
RBI
@@ -226,13 +257,13 @@ class Student
226257
extend GeneratedRelationMethods
227258
228259
module FrozenRecordAttributeMethods
229-
sig { returns(T.untyped) }
260+
sig { returns(String) }
230261
def course; end
231262
232263
sig { returns(T::Boolean) }
233264
def course?; end
234265
235-
sig { returns(T.untyped) }
266+
sig { returns(Integer) }
236267
def id; end
237268
238269
sig { returns(T::Boolean) }

0 commit comments

Comments
 (0)