Skip to content

Commit b44f4bc

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

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed

lib/tapioca/dsl/compilers/frozen_record.rb

Lines changed: 4 additions & 8 deletions
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

Lines changed: 40 additions & 31 deletions
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,17 @@ 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
152137
- id: 2
153138
first_name: Dan
154139
last_name: Lord
155140
age: 20
156141
location: Toronto, Ontario, Canada
142+
is_cool_person: yes
143+
birth_date: 1967-07-01
144+
updated_at: 2015-02-24T19:08:06-05:00
157145
YAML
158146

159147
expected = <<~RBI
@@ -163,38 +151,59 @@ class Student
163151
include FrozenRecordAttributeMethods
164152
165153
module FrozenRecordAttributeMethods
166-
sig { returns(::Integer) }
154+
sig { returns(Integer) }
167155
def age; end
168156
169157
sig { returns(T::Boolean) }
170158
def age?; end
171159
172-
sig { params(grain: ::Symbol).returns(::String) }
173-
def area(grain:); end
160+
sig { returns(Date) }
161+
def birth_date; end
174162
175-
sig { returns(::String) }
163+
sig { returns(T::Boolean) }
164+
def birth_date?; end
165+
166+
sig { returns(String) }
176167
def first_name; end
177168
178169
sig { returns(T::Boolean) }
179170
def first_name?; end
180171
181-
sig { returns(T.untyped) }
172+
sig { returns(Integer) }
182173
def id; end
183174
184175
sig { returns(T::Boolean) }
185176
def id?; end
186177
187-
sig { returns(::String) }
178+
sig { returns(T::Boolean) }
179+
def is_cool_person; end
180+
181+
sig { returns(T::Boolean) }
182+
def is_cool_person?; end
183+
184+
sig { returns(String) }
188185
def last_name; end
189186
190187
sig { returns(T::Boolean) }
191188
def last_name?; end
192189
193-
sig { returns(::String) }
190+
sig { returns(String) }
194191
def location; end
195192
196193
sig { returns(T::Boolean) }
197194
def location?; end
195+
196+
sig { returns(Symbol) }
197+
def shirt_size; end
198+
199+
sig { returns(T::Boolean) }
200+
def shirt_size?; end
201+
202+
sig { returns(Time) }
203+
def updated_at; end
204+
205+
sig { returns(T::Boolean) }
206+
def updated_at?; end
198207
end
199208
end
200209
RBI
@@ -226,13 +235,13 @@ class Student
226235
extend GeneratedRelationMethods
227236
228237
module FrozenRecordAttributeMethods
229-
sig { returns(T.untyped) }
238+
sig { returns(String) }
230239
def course; end
231240
232241
sig { returns(T::Boolean) }
233242
def course?; end
234243
235-
sig { returns(T.untyped) }
244+
sig { returns(Integer) }
236245
def id; end
237246
238247
sig { returns(T::Boolean) }

0 commit comments

Comments
 (0)