Skip to content

Commit 5b5793d

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

File tree

2 files changed

+32
-35
lines changed

2 files changed

+32
-35
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: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -106,26 +106,6 @@ 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
128-
129109
sig { params(grain: Symbol).returns(String) }
130110
def area(grain:)
131111
parts = location.split(',').map(&:strip)
@@ -149,11 +129,17 @@ def area(grain:)
149129
last_name: Smith
150130
age: 19
151131
location: Ottawa, Ontario, Canada
132+
is_cool_person: no
133+
birth_date: 1867-07-01
134+
updated_at: 2014-02-24T19:08:06-05:00
152135
- id: 2
153136
first_name: Dan
154137
last_name: Lord
155138
age: 20
156139
location: Toronto, Ontario, Canada
140+
is_cool_person: yes
141+
birth_date: 1967-07-01
142+
updated_at: 2015-02-24T19:08:06-05:00
157143
YAML
158144

159145
expected = <<~RBI
@@ -163,38 +149,53 @@ class Student
163149
include FrozenRecordAttributeMethods
164150
165151
module FrozenRecordAttributeMethods
166-
sig { returns(::Integer) }
152+
sig { returns(Integer) }
167153
def age; end
168154
169155
sig { returns(T::Boolean) }
170156
def age?; end
171157
172-
sig { params(grain: ::Symbol).returns(::String) }
173-
def area(grain:); end
158+
sig { returns(Date) }
159+
def birth_date; end
160+
161+
sig { returns(T::Boolean) }
162+
def birth_date?; end
174163
175-
sig { returns(::String) }
164+
sig { returns(String) }
176165
def first_name; end
177166
178167
sig { returns(T::Boolean) }
179168
def first_name?; end
180169
181-
sig { returns(T.untyped) }
170+
sig { returns(Integer) }
182171
def id; end
183172
184173
sig { returns(T::Boolean) }
185174
def id?; end
186175
187-
sig { returns(::String) }
176+
sig { returns(T::Boolean) }
177+
def is_cool_person; end
178+
179+
sig { returns(T::Boolean) }
180+
def is_cool_person?; end
181+
182+
sig { returns(String) }
188183
def last_name; end
189184
190185
sig { returns(T::Boolean) }
191186
def last_name?; end
192187
193-
sig { returns(::String) }
188+
sig { returns(String) }
194189
def location; end
195190
196191
sig { returns(T::Boolean) }
197192
def location?; end
193+
194+
sig { returns(Time) }
195+
def updated_at; end
196+
197+
sig { returns(T::Boolean) }
198+
def updated_at?; end
198199
end
199200
end
200201
RBI

0 commit comments

Comments
 (0)