Skip to content

Commit 4c64acf

Browse files
committed
Integrate model_dependencies.rbs to each model signatures
As a preparation for LSP server, this integrates model_dependencies.rbs to each model signatures. With this change, the signature of dependencies will be separated to the each RBS file of the model. It allows to rbs_rails to generate signatures for models one by one, not only the whole of application. It also helps the LSP server generate signatures on the fly.
1 parent e0f9fc5 commit 4c64acf

File tree

11 files changed

+41
-41
lines changed

11 files changed

+41
-41
lines changed

lib/rbs_rails/active_record.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ def self.generatable?(klass) #: boolish
1010

1111
# @rbs klass: untyped
1212
# @rbs dependencies: Array[String]
13-
def self.class_to_rbs(klass, dependencies: []) #: untyped
14-
Generator.new(klass, dependencies: dependencies).generate
13+
def self.class_to_rbs(klass) #: untyped
14+
Generator.new(klass).generate
1515
end
1616

1717
class Generator
1818
IGNORED_ENUM_KEYS = %i[_prefix _suffix _default _scopes] #: Array[Symbol]
1919

2020
# @rbs @parse_model_file: nil | Parser::AST::Node
21-
# @rbs @dependencies: Array[String]
2221
# @rbs @enum_definitions: Array[Hash[Symbol, untyped]]
2322
# @rbs @klass_name: String
2423

24+
attr_reader :dependencies #: DependencyBuilder
25+
2526
# @rbs klass: singleton(ActiveRecord::Base) & Enum
26-
# @rbs dependencies: Array[String]
27-
def initialize(klass, dependencies:) #: untyped
27+
def initialize(klass) #: untyped
2828
@klass = klass
29-
@dependencies = dependencies
29+
@dependencies = DependencyBuilder.new
3030
@klass_name = Util.module_name(klass, abs: false)
3131

3232
namespaces = klass_name(abs: false).split('::').tap{ |names| names.pop }
@@ -62,6 +62,8 @@ def generate #: String
6262
#{collection_proxy_decl}
6363
6464
#{footer}
65+
66+
#{dependencies.build}
6567
RBS
6668
end
6769

lib/rbs_rails/cli.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,16 @@ def install_hooks #: void
9292
def generate_models #: void
9393
Rails.application.eager_load!
9494

95-
dep_builder = DependencyBuilder.new
96-
9795
::ActiveRecord::Base.descendants.each do |klass|
98-
generate_single_model(klass, dep_builder)
96+
generate_single_model(klass)
9997
rescue => e
10098
puts "Error generating RBS for #{klass.name} model"
10199
raise e
102100
end
103-
104-
if dep_rbs = dep_builder.build
105-
config.signature_root_dir.join('model_dependencies.rbs').write(dep_rbs)
106-
end
107101
end
108102

109103
# @rbs klass: singleton(ActiveRecord::Base)
110-
# @rbs dep_builder: DependencyBuilder
111-
def generate_single_model(klass, dep_builder) #: bool
104+
def generate_single_model(klass) #: bool
112105
return false if config.ignored_model?(klass)
113106
return false unless RbsRails::ActiveRecord.generatable?(klass)
114107

@@ -125,9 +118,8 @@ def generate_single_model(klass, dep_builder) #: bool
125118
path = config.signature_root_dir / rbs_relative_path
126119
path.dirname.mkpath
127120

128-
sig = RbsRails::ActiveRecord.class_to_rbs(klass, dependencies: dep_builder.deps)
121+
sig = RbsRails::ActiveRecord.class_to_rbs(klass)
129122
path.write sig
130-
dep_builder.done << klass.name
131123

132124
true
133125
end

lib/rbs_rails/dependency_builder.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ def initialize #: void
1919
])
2020
end
2121

22+
# @rbs name: String
23+
def <<(name) #: Array[String]
24+
deps << name
25+
end
26+
2227
def build #: String | nil
2328
dep_rbs = +""
2429
deps.uniq!

sig/rbs_rails/active_record.rbs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module RbsRails
77

88
# @rbs klass: untyped
99
# @rbs dependencies: Array[String]
10-
def self.class_to_rbs: (untyped klass, ?dependencies: Array[String]) -> untyped
10+
def self.class_to_rbs: (untyped klass) -> untyped
1111

1212
class Generator
1313
IGNORED_ENUM_KEYS: Array[Symbol]
@@ -16,13 +16,12 @@ module RbsRails
1616

1717
@enum_definitions: Array[Hash[Symbol, untyped]]
1818

19-
@dependencies: Array[String]
20-
2119
@parse_model_file: nil | Parser::AST::Node
2220

21+
attr_reader dependencies: DependencyBuilder
22+
2323
# @rbs klass: singleton(ActiveRecord::Base) & Enum
24-
# @rbs dependencies: Array[String]
25-
def initialize: (singleton(ActiveRecord::Base) & Enum klass, dependencies: Array[String]) -> untyped
24+
def initialize: (singleton(ActiveRecord::Base) & Enum klass) -> untyped
2625

2726
def generate: () -> String
2827

sig/rbs_rails/cli.rbs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ module RbsRails
2323
def generate_models: () -> void
2424

2525
# @rbs klass: singleton(ActiveRecord::Base)
26-
# @rbs dep_builder: DependencyBuilder
27-
def generate_single_model: (singleton(ActiveRecord::Base) klass, DependencyBuilder dep_builder) -> bool
26+
def generate_single_model: (singleton(ActiveRecord::Base) klass) -> bool
2827

2928
def generate_path_helpers: () -> void
3029

sig/rbs_rails/dependency_builder.rbs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ module RbsRails
88

99
def initialize: () -> void
1010

11+
# @rbs name: String
12+
def <<: (String name) -> Array[String]
13+
1114
def build: () -> (String | nil)
1215

1316
private def shift: () -> (String | nil)

test/expectations/audited_audit.rbs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,3 +681,6 @@ module ::Audited
681681
end
682682
end
683683
end
684+
685+
module ::Audited
686+
end

test/expectations/blog.rbs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,10 @@ class ::Blog < ::ApplicationRecord
288288
def prepend: (*::Blog | ::Array[::Blog]) -> self
289289
end
290290
end
291+
292+
class ::ApplicationRecord < ::ActiveRecord::Base
293+
end
294+
class ::User < ::ApplicationRecord
295+
end
296+
class ::User::ActiveRecord_Associations_CollectionProxy < ::ActiveRecord::Associations::CollectionProxy
297+
end

test/expectations/model_dependencies.rbs

Lines changed: 0 additions & 8 deletions
This file was deleted.

test/expectations/user.rbs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,10 @@ class ::User < ::ApplicationRecord
508508
def prepend: (*::User | ::Array[::User]) -> self
509509
end
510510
end
511+
512+
class ::ApplicationRecord < ::ActiveRecord::Base
513+
end
514+
class ::Blog < ::ApplicationRecord
515+
end
516+
class ::Blog::ActiveRecord_Associations_CollectionProxy < ::ActiveRecord::Associations::CollectionProxy
517+
end

0 commit comments

Comments
 (0)