rbs_rails 0.12.0 → 0.12.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,9 +17,9 @@ module RbsRails
17
17
  def initialize(klass, dependencies:)
18
18
  @klass = klass
19
19
  @dependencies = dependencies
20
- @klass_name = Util.module_name(klass)
20
+ @klass_name = Util.module_name(klass, abs: false)
21
21
 
22
- namespaces = klass_name.split('::').tap{ |names| names.pop }
22
+ namespaces = klass_name(abs: false).split('::').tap{ |names| names.pop }
23
23
  @dependencies << namespaces.join('::') unless namespaces.empty?
24
24
  end
25
25
 
@@ -30,7 +30,7 @@ module RbsRails
30
30
  private def klass_decl
31
31
  <<~RBS
32
32
  #{header}
33
- extend _ActiveRecord_Relation_ClassMethods[#{klass_name}, #{relation_class_name}, #{pk_type}]
33
+ extend ::_ActiveRecord_Relation_ClassMethods[#{klass_name}, #{relation_class_name}, #{pk_type}]
34
34
 
35
35
  #{columns}
36
36
  #{associations}
@@ -62,7 +62,7 @@ module RbsRails
62
62
 
63
63
  private def generated_relation_methods_decl
64
64
  <<~RBS
65
- module GeneratedRelationMethods
65
+ module #{generated_relation_methods_name(abs: false)}
66
66
  #{enum_scope_methods(singleton: false)}
67
67
  #{scopes(singleton: false)}
68
68
  #{delegated_type_scope(singleton: false)}
@@ -72,10 +72,10 @@ module RbsRails
72
72
 
73
73
  private def relation_decl
74
74
  <<~RBS
75
- class #{relation_class_name} < ::ActiveRecord::Relation
76
- include GeneratedRelationMethods
77
- include _ActiveRecord_Relation[#{klass_name}, #{pk_type}]
78
- include Enumerable[#{klass_name}]
75
+ class #{relation_class_name(abs: false)} < ::ActiveRecord::Relation
76
+ include #{generated_relation_methods_name}
77
+ include ::_ActiveRecord_Relation[#{klass_name}, #{pk_type}]
78
+ include ::Enumerable[#{klass_name}]
79
79
  end
80
80
  RBS
81
81
  end
@@ -83,22 +83,22 @@ module RbsRails
83
83
  private def collection_proxy_decl
84
84
  <<~RBS
85
85
  class ActiveRecord_Associations_CollectionProxy < ::ActiveRecord::Associations::CollectionProxy
86
- include GeneratedRelationMethods
87
- include _ActiveRecord_Relation[#{klass_name}, #{pk_type}]
86
+ include #{generated_relation_methods_name}
87
+ include ::_ActiveRecord_Relation[#{klass_name}, #{pk_type}]
88
88
  end
89
89
  RBS
90
90
  end
91
91
 
92
92
  private def header
93
93
  namespace = +''
94
- klass_name.split('::').map do |mod_name|
94
+ klass_name(abs: false).split('::').map do |mod_name|
95
95
  namespace += "::#{mod_name}"
96
96
  mod_object = Object.const_get(namespace)
97
97
  case mod_object
98
98
  when Class
99
99
  # @type var superclass: Class
100
100
  superclass = _ = mod_object.superclass
101
- superclass_name = Util.module_name(superclass)
101
+ superclass_name = Util.module_name(superclass, abs: false)
102
102
  @dependencies << superclass_name
103
103
 
104
104
  "class #{mod_name} < ::#{superclass_name}"
@@ -111,7 +111,7 @@ module RbsRails
111
111
  end
112
112
 
113
113
  private def footer
114
- "end\n" * klass_name.split('::').size
114
+ "end\n" * klass_name(abs: false).split('::').size
115
115
  end
116
116
 
117
117
  private def associations
@@ -133,9 +133,9 @@ module RbsRails
133
133
 
134
134
  <<~RUBY.chomp
135
135
  def #{a.name}: () -> #{collection_type}
136
- def #{a.name}=: (#{collection_type} | Array[#{type}]) -> (#{collection_type} | Array[#{type}])
137
- def #{singular_name}_ids: () -> Array[Integer]
138
- def #{singular_name}_ids=: (Array[Integer]) -> Array[Integer]
136
+ def #{a.name}=: (#{collection_type} | ::Array[#{type}]) -> (#{collection_type} | ::Array[#{type}])
137
+ def #{singular_name}_ids: () -> ::Array[::Integer]
138
+ def #{singular_name}_ids=: (::Array[::Integer]) -> ::Array[::Integer]
139
139
  RUBY
140
140
  end.join("\n")
141
141
  end
@@ -149,7 +149,7 @@ module RbsRails
149
149
  <<~RUBY.chomp
150
150
  def #{a.name}: () -> #{type_optional}
151
151
  def #{a.name}=: (#{type_optional}) -> #{type_optional}
152
- def build_#{a.name}: (untyped) -> #{type}
152
+ def build_#{a.name}: (?untyped) -> #{type}
153
153
  def create_#{a.name}: (untyped) -> #{type}
154
154
  def create_#{a.name}!: (untyped) -> #{type}
155
155
  def reload_#{a.name}: () -> #{type_optional}
@@ -190,21 +190,21 @@ module RbsRails
190
190
  case reflection.macro
191
191
  when :has_one_attached
192
192
  <<~EOS
193
- def #{name}: () -> ActiveStorage::Attached::One
194
- def #{name}=: (ActionDispatch::Http::UploadedFile) -> ActionDispatch::Http::UploadedFile
195
- | (Rack::Test::UploadedFile) -> Rack::Test::UploadedFile
196
- | (ActiveStorage::Blob) -> ActiveStorage::Blob
197
- | (String) -> String
198
- | ({ io: IO, filename: String, content_type: String? }) -> { io: IO, filename: String, content_type: String? }
193
+ def #{name}: () -> ::ActiveStorage::Attached::One
194
+ def #{name}=: (::ActionDispatch::Http::UploadedFile) -> ::ActionDispatch::Http::UploadedFile
195
+ | (::Rack::Test::UploadedFile) -> ::Rack::Test::UploadedFile
196
+ | (::ActiveStorage::Blob) -> ::ActiveStorage::Blob
197
+ | (::String) -> ::String
198
+ | ({ io: ::IO, filename: ::String, content_type: ::String? }) -> { io: ::IO, filename: ::String, content_type: ::String? }
199
199
  | (nil) -> nil
200
200
  EOS
201
201
  when :has_many_attached
202
202
  <<~EOS
203
- def #{name}: () -> ActiveStorage::Attached::Many
203
+ def #{name}: () -> ::ActiveStorage::Attached::Many
204
204
  def #{name}=: (untyped) -> untyped
205
205
  EOS
206
206
  else
207
- raise
207
+ raise "unknown macro: #{reflection.macro}"
208
208
  end
209
209
  end.join("\n")
210
210
  sigs << "end"
@@ -460,7 +460,7 @@ module RbsRails
460
460
  private def parse_model_file
461
461
  return @parse_model_file if defined?(@parse_model_file)
462
462
 
463
- path = Rails.root.join('app/models/', klass_name.underscore + '.rb')
463
+ path = Rails.root.join('app/models/', klass_name(abs: false).underscore + '.rb')
464
464
  return @parse_model_file = nil unless path.exist?
465
465
  return [] unless path.exist?
466
466
 
@@ -471,24 +471,32 @@ module RbsRails
471
471
  end
472
472
 
473
473
  private def traverse(node, &block)
474
- return to_enum(__method__ || raise, node) unless block_given?
474
+ return to_enum(__method__ || raise, node) unless block
475
475
 
476
- # @type var block: ^(Parser::AST::Node) -> untyped
477
476
  block.call node
478
477
  node.children.each do |child|
479
478
  traverse(child, &block) if child.is_a?(Parser::AST::Node)
480
479
  end
481
480
  end
482
481
 
483
- private def relation_class_name
484
- "ActiveRecord_Relation"
482
+ private def relation_class_name(abs: true)
483
+ abs ? "#{klass_name}::ActiveRecord_Relation" : "ActiveRecord_Relation"
485
484
  end
486
485
 
486
+ private def klass_name(abs: true)
487
+ abs ? "::#{@klass_name}" : @klass_name
488
+ end
489
+
490
+ private def generated_relation_methods_name(abs: true)
491
+ abs ? "#{klass_name}::GeneratedRelationMethods" : "GeneratedRelationMethods"
492
+ end
493
+
494
+
487
495
  private def columns
488
496
  mod_sig = +"module GeneratedAttributeMethods\n"
489
497
  mod_sig << klass.columns.map do |col|
490
498
  class_name = if enum_definitions.any? { |hash| hash.key?(col.name) || hash.key?(col.name.to_sym) }
491
- 'String'
499
+ '::String'
492
500
  else
493
501
  sql_type_to_class(col.type)
494
502
  end
@@ -503,12 +511,12 @@ module RbsRails
503
511
  def #{col.name}_will_change!: () -> void
504
512
  def #{col.name}_was: () -> #{class_name_opt}
505
513
  def #{col.name}_previously_changed?: () -> bool
506
- def #{col.name}_previous_change: () -> Array[#{class_name_opt}]?
514
+ def #{col.name}_previous_change: () -> ::Array[#{class_name_opt}]?
507
515
  def #{col.name}_previously_was: () -> #{class_name_opt}
508
516
  def #{col.name}_before_last_save: () -> #{class_name_opt}
509
- def #{col.name}_change_to_be_saved: () -> Array[#{class_name_opt}]?
517
+ def #{col.name}_change_to_be_saved: () -> ::Array[#{class_name_opt}]?
510
518
  def #{col.name}_in_database: () -> #{class_name_opt}
511
- def saved_change_to_#{col.name}: () -> Array[#{class_name_opt}]?
519
+ def saved_change_to_#{col.name}: () -> ::Array[#{class_name_opt}]?
512
520
  def saved_change_to_#{col.name}?: () -> bool
513
521
  def will_save_change_to_#{col.name}?: () -> bool
514
522
  def restore_#{col.name}!: () -> void
@@ -529,25 +537,25 @@ module RbsRails
529
537
  private def sql_type_to_class(t)
530
538
  case t
531
539
  when :integer
532
- 'Integer'
540
+ '::Integer'
533
541
  when :float
534
- 'Float'
542
+ '::Float'
535
543
  when :decimal
536
- 'BigDecimal'
544
+ '::BigDecimal'
537
545
  when :string, :text, :citext, :uuid, :binary
538
- 'String'
546
+ '::String'
539
547
  when :datetime
540
- 'ActiveSupport::TimeWithZone'
548
+ '::ActiveSupport::TimeWithZone'
541
549
  when :boolean
542
550
  "bool"
543
551
  when :jsonb, :json
544
552
  "untyped"
545
553
  when :date
546
- 'Date'
554
+ '::Date'
547
555
  when :time
548
- 'Time'
556
+ '::Time'
549
557
  when :inet
550
- "IPAddr"
558
+ "::IPAddr"
551
559
  else
552
560
  # Unknown column type, give up
553
561
  'untyped'
@@ -555,7 +563,7 @@ module RbsRails
555
563
  end
556
564
 
557
565
  private
558
- attr_reader :klass, :klass_name
566
+ attr_reader :klass
559
567
  end
560
568
  end
561
569
  end
@@ -10,7 +10,7 @@ module RbsRails
10
10
  def build
11
11
  dep_rbs = +""
12
12
  deps.uniq!
13
- while dep = deps.shift
13
+ while dep = shift
14
14
  next unless done.add?(dep)
15
15
 
16
16
  case dep_object = Object.const_get(dep)
@@ -39,5 +39,9 @@ module RbsRails
39
39
  Util.format_rbs(dep_rbs)
40
40
  end
41
41
  end
42
+
43
+ private def shift
44
+ deps.shift&.sub(/^::/, '')
45
+ end
42
46
  end
43
47
  end
@@ -3,17 +3,17 @@ require 'rake/tasklib'
3
3
 
4
4
  module RbsRails
5
5
  class RakeTask < Rake::TaskLib
6
- attr_accessor :ignore_model_if, :name, :signature_root_dir
6
+ attr_accessor :ignore_model_if, :name
7
+ attr_writer :signature_root_dir
7
8
 
8
9
  def initialize(name = :rbs_rails, &block)
9
10
  super()
10
11
 
11
12
  @name = name
13
+ @signature_root_dir = Rails.root / 'sig/rbs_rails'
12
14
 
13
15
  block.call(self) if block
14
16
 
15
- setup_signature_root_dir!
16
-
17
17
  def_generate_rbs_for_models
18
18
  def_generate_rbs_for_path_helpers
19
19
  def_all
@@ -64,10 +64,10 @@ module RbsRails
64
64
  end
65
65
  end
66
66
 
67
- private def setup_signature_root_dir!
68
- @signature_root_dir ||= Rails.root / 'sig/rbs_rails'
69
- @signature_root_dir = Pathname(@signature_root_dir)
70
- @signature_root_dir.mkpath
67
+ private def signature_root_dir
68
+ Pathname(@signature_root_dir).tap do |dir|
69
+ dir.mkpath
70
+ end
71
71
  end
72
72
  end
73
73
  end
@@ -4,26 +4,21 @@ module RbsRails
4
4
 
5
5
  extend self
6
6
 
7
- if '2.7' <= RUBY_VERSION
8
- def module_name(mod)
9
- # HACK: RBS doesn't have UnboundMethod#bind_call
10
- (_ = MODULE_NAME).bind_call(mod)
11
- end
12
- else
13
- def module_name(mod)
14
- MODULE_NAME.bind(mod).call
15
- end
7
+ def module_name(mod, abs: true)
8
+ name = MODULE_NAME.bind_call(mod)
9
+ name ="::#{name}" if abs
10
+ name
16
11
  end
17
12
 
18
13
  def format_rbs(rbs)
19
14
  decls =
20
15
  if Gem::Version.new('3') <= Gem::Version.new(RBS::VERSION)
21
- # TODO: Remove this type annotation when rbs_rails depends on RBS v3
22
- # @type var parsed: [RBS::Buffer, untyped, RBS::Declarations::t]
23
16
  parsed = _ = RBS::Parser.parse_signature(rbs)
24
17
  parsed[1] + parsed[2]
25
18
  else
26
- RBS::Parser.parse_signature(rbs)
19
+ # TODO: Remove this type annotation when rbs_rails drops support of RBS 2.x.
20
+ # @type var parsed: [RBS::Declarations::t]
21
+ parsed = _ = RBS::Parser.parse_signature(rbs)
27
22
  end
28
23
 
29
24
  StringIO.new.tap do |io|
@@ -2,5 +2,5 @@ module RbsRails
2
2
  # Because of copy_signatures is defined by lib/rbs_rails.rb
3
3
  # @dynamic self.copy_signatures
4
4
 
5
- VERSION = "0.12.0"
5
+ VERSION = "0.12.1"
6
6
  end