rbs_rails 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ #!ruby
2
+
3
+ ARGV.each do |p|
4
+ File.write p, File.read(p).gsub(/[^[:ascii:]]+/, '(trim non-ascii characters)')
5
+ end
@@ -1,13 +1,12 @@
1
1
  module RbsRails
2
2
  module ActiveRecord
3
- def self.class_to_rbs(klass, mode:)
4
- Generator.new(klass, mode: mode).generate
3
+ def self.class_to_rbs(klass)
4
+ Generator.new(klass).generate
5
5
  end
6
6
 
7
7
  class Generator
8
- def initialize(klass, mode:)
8
+ def initialize(klass)
9
9
  @klass = klass
10
- @mode = mode
11
10
  end
12
11
 
13
12
  def generate
@@ -52,16 +51,9 @@ module RbsRails
52
51
 
53
52
 
54
53
  private def header
55
- case mode
56
- when :extension
57
- "extension #{klass.name} (RbsRails)"
58
- when :class
59
- # @type var superclass: Class
60
- superclass = _ = klass.superclass
61
- "class #{klass.name} < #{superclass.name}"
62
- else
63
- raise "unexpected mode: #{mode}"
64
- end
54
+ # @type var superclass: Class
55
+ superclass = _ = klass.superclass
56
+ "class #{klass.name} < #{superclass.name}"
65
57
  end
66
58
 
67
59
  private def associations
@@ -246,10 +238,30 @@ module RbsRails
246
238
 
247
239
  private def columns
248
240
  klass.columns.map do |col|
249
- "attr_accessor #{col.name} (): #{sql_type_to_class(col.type)}"
241
+ class_name = sql_type_to_class(col.type)
242
+ class_name_opt = optional(class_name)
243
+ column_type = col.null ? class_name_opt : class_name
244
+ sig = <<~EOS
245
+ attr_accessor #{col.name} (): #{column_type}
246
+ def #{col.name}_changed?: () -> bool
247
+ def #{col.name}_change: () -> [#{class_name_opt}, #{class_name_opt}]
248
+ def #{col.name}_will_change!: () -> void
249
+ def #{col.name}_was: () -> #{class_name_opt}
250
+ def #{col.name}_previously_changed?: () -> bool
251
+ def #{col.name}_previous_change: () -> Array[#{class_name_opt}]?
252
+ def #{col.name}_previously_was: () -> #{class_name_opt}
253
+ def restore_#{col.name}!: () -> void
254
+ def clear_#{col.name}_change: () -> void
255
+ EOS
256
+ sig << "attr_accessor #{col.name}? (): #{class_name}\n" if col.type == :boolean
257
+ sig
250
258
  end.join("\n")
251
259
  end
252
260
 
261
+ private def optional(class_name)
262
+ class_name.include?("|") ? "(#{class_name})?" : "#{class_name}?"
263
+ end
264
+
253
265
  private def sql_type_to_class(t)
254
266
  case t
255
267
  when :integer
@@ -270,14 +282,18 @@ module RbsRails
270
282
  # TODO
271
283
  # Date.name
272
284
  'untyped'
285
+ when :time
286
+ Time.name
287
+ when :inet
288
+ "IPAddr"
273
289
  else
274
290
  raise "unexpected: #{t.inspect}"
275
291
  end
276
292
  end
277
293
 
278
294
  private
279
- # @dynamic klass, mode
280
- attr_reader :klass, :mode
295
+ # @dynamic klass
296
+ attr_reader :klass
281
297
  end
282
298
  end
283
299
  end
@@ -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.2.0"
5
+ VERSION = "0.3.0"
6
6
  end
@@ -1,11 +1,11 @@
1
1
  module RbsRails::ActiveRecord
2
- def self.class_to_rbs: (untyped klass, mode: untyped mode) -> untyped
2
+ def self.class_to_rbs: (untyped klass) -> untyped
3
3
  end
4
4
 
5
5
  class RbsRails::ActiveRecord::Generator
6
6
  @parse_model_file: nil | Parser::AST::Node
7
7
 
8
- def initialize: (singleton(ActiveRecord::Base) klass, mode: (:extension | :class) mode) -> untyped
8
+ def initialize: (singleton(ActiveRecord::Base) klass) -> untyped
9
9
 
10
10
  def generate: () -> String
11
11
 
@@ -53,7 +53,7 @@ class RbsRails::ActiveRecord::Generator
53
53
 
54
54
  def sql_type_to_class: (untyped t) -> untyped
55
55
 
56
- attr_reader klass: singleton(ActiveRecord::Base)
56
+ def optional: (String) -> String
57
57
 
58
- attr_reader mode: (:extension | :class)
58
+ attr_reader klass: singleton(ActiveRecord::Base)
59
59
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbs_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masataka Pocke Kuwabara
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-24 00:00:00.000000000 Z
11
+ date: 2020-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -40,20 +40,38 @@ files:
40
40
  - Steepfile
41
41
  - assets/sig/action_controller.rbs
42
42
  - assets/sig/action_mailer.rbs
43
- - assets/sig/action_view.rbs
44
43
  - assets/sig/active_record.rbs
45
44
  - assets/sig/builtin.rbs
45
+ - assets/sig/capybara.rbs
46
+ - assets/sig/concurrent.rbs
47
+ - assets/sig/erb.rbs
48
+ - assets/sig/erubi.rbs
49
+ - assets/sig/generated/actionpack.rbs
50
+ - assets/sig/generated/actionview.rbs
46
51
  - assets/sig/generated/activemodel.rbs
52
+ - assets/sig/generated/activerecord-meta-programming.rbs
53
+ - assets/sig/generated/activerecord.rbs
47
54
  - assets/sig/generated/activesupport.rbs
55
+ - assets/sig/generated/railties.rbs
56
+ - assets/sig/i18n.rbs
48
57
  - assets/sig/libxml.rbs
49
58
  - assets/sig/minitest.rbs
50
59
  - assets/sig/nokogiri.rbs
60
+ - assets/sig/racc.rbs
61
+ - assets/sig/rack-test.rbs
62
+ - assets/sig/rack.rbs
51
63
  - assets/sig/rails.rbs
64
+ - assets/sig/rdoc.rbs
52
65
  - assets/sig/stdlib.rbs
66
+ - assets/sig/thor.rbs
67
+ - assets/sig/tzinfo.rbs
53
68
  - bin/add-type-params.rb
54
69
  - bin/console
55
- - bin/merge-duplicate-decls.rb
70
+ - bin/postprocess.rb
71
+ - bin/rbs
72
+ - bin/rbs-prototype-rb.rb
56
73
  - bin/setup
74
+ - bin/to-ascii.rb
57
75
  - lib/rbs_rails.rb
58
76
  - lib/rbs_rails/active_record.rb
59
77
  - lib/rbs_rails/path_helpers.rb
@@ -71,7 +89,7 @@ licenses:
71
89
  metadata:
72
90
  homepage_uri: https://github.com/pocke/rbs_rails
73
91
  source_code_uri: https://github.com/pocke/rbs_rails
74
- post_install_message:
92
+ post_install_message:
75
93
  rdoc_options: []
76
94
  require_paths:
77
95
  - lib
@@ -86,8 +104,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
104
  - !ruby/object:Gem::Version
87
105
  version: '0'
88
106
  requirements: []
89
- rubygems_version: 3.2.0.pre1
90
- signing_key:
107
+ rubygems_version: 3.2.0.rc.1
108
+ signing_key:
91
109
  specification_version: 4
92
110
  summary: A RBS files generator for Rails application
93
111
  test_files: []
@@ -1,3 +0,0 @@
1
- module ActionView::Layouts::ClassMethods
2
- def layout: (String) -> void
3
- end
@@ -1,30 +0,0 @@
1
- require 'rbs'
2
- rbs = ARGF.read
3
-
4
- decls = RBS::Parser.parse_signature(rbs)
5
- decl_map = {}
6
- decls.each do |decl|
7
- if d = decl_map[decl.name]
8
- # TODO: Is it right for decl is not a class / module?
9
- c = RBS::AST::Declarations::Class
10
- m = RBS::AST::Declarations::Module
11
- e = RBS::AST::Declarations::Extension
12
- raise 'class mismatch' if d.class != decl.class
13
- raise 'class mismatch' unless [c,m,e].include?(d.class)
14
-
15
- d.members.concat decl.members
16
- d.members.uniq! { if _1.is_a?(RBS::AST::Members::MethodDefinition) then [_1.name, _1.singleton?] else _1 end}
17
- case d
18
- when c
19
- decl_map[decl.name] = c.new(name: d.name, type_params: d.type_params, super_class: d.super_class || decl.super_class, members: d.members, annotations: d.annotations, location: d.location, comment: d.comment)
20
- when m
21
- decl_map[decl.name] = m.new(name: d.name, type_params: d.type_params, members: d.members, self_type: d.self_type, annotations: d.annotations, location: d.location, comment: d.comment)
22
- when e
23
- decl_map[decl.name] = e.new(name: d.name, type_params: d.type_params, extension_name: d.extension_name, members: d.members, annotations: d.annotations, location: d.location, comment: d.comment)
24
- end
25
- else
26
- decl_map[decl.name] = decl
27
- end
28
- end
29
-
30
- puts RBS::Writer.new(out: $stdout).write(decl_map.values)