rbs_active_hash 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ae3f09f4556a17ac66ced6e72014731d08f17f4b959a82f57fcc7a49e238f025
4
- data.tar.gz: 6b4896ee1ac32dd933d1fd2c2796d42290eaf4ccae47d13ecbf16c3d8f07d488
3
+ metadata.gz: 39351f6d9217ba00e6fb1d8787f3740bd81b9f883efd4992708506cfc1b423c2
4
+ data.tar.gz: e74571aeba7af6cde5396a1e595cd2bc3190fa6d34ce8d5e10b569128201fce9
5
5
  SHA512:
6
- metadata.gz: 16296cd5be97a656942d1abc219ce1fc31a09ddc5dec6f69489d1f805c566c959a5a55d3911b8fb7f31f27242d4d339c4d97aa4d73de37073f1c813c1c7afd6d
7
- data.tar.gz: 83b4d3996b2d3aad75ac53d1400666f4c9275f184e8c2af962e62dcc0cb887327493285947f4e8f75ddd1a14995a2f40f6b0e79d0758f72c84b67f854782b7ca
6
+ metadata.gz: 19351b7829c68607011084f0bb253ea8282282045dd66a7fbd4e60e6c55997a3087c591629b8dea60702dff31211d55bde4a574475feda85ddb5fd83e7efef3d
7
+ data.tar.gz: 4a30401c88437f1b534e182a49d54190cb820111093d99ec50934ba5f9429a102e9cae8f2cb1fdc6805580dbe2b749a0caca4239af417df59317272b463855de
data/.rubocop.yml CHANGED
@@ -25,8 +25,11 @@ Metrics/BlockLength:
25
25
  Metrics/ClassLength:
26
26
  Max: 200
27
27
 
28
+ Metrics/CyclomaticComplexity:
29
+ Max: 10
30
+
28
31
  Metrics/MethodLength:
29
- Max: 20
32
+ Max: 30
30
33
 
31
34
  Metrics/PerceivedComplexity:
32
35
  Max: 10
@@ -2,5 +2,8 @@
2
2
  "[ruby]": {
3
3
  "editor.defaultFormatter": "Shopify.ruby-lsp"
4
4
  },
5
- "rbs-helper.signature-directory": "sig"
5
+ "rbs-helper.signature-directory": "sig",
6
+ "cSpell.words": [
7
+ "Cyclomatic"
8
+ ]
6
9
  }
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rbs_active_hash (1.2.1)
4
+ rbs_active_hash (1.3.0)
5
5
  active_hash
6
6
  rbs
7
7
  rbs_rails
@@ -114,7 +114,7 @@ GEM
114
114
  diff-lcs (>= 1.2.0, < 2.0)
115
115
  rspec-support (~> 3.12.0)
116
116
  rspec-support (3.12.0)
117
- rubocop (1.56.0)
117
+ rubocop (1.56.1)
118
118
  base64 (~> 0.1.1)
119
119
  json (~> 2.3)
120
120
  language_server-protocol (>= 3.17.0)
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rbs"
4
+
5
+ module RbsActiveHash
6
+ module ActiveHash
7
+ module Parser
8
+ class AssociationDefinition < RBS::AST::Members::Include
9
+ end
10
+
11
+ class ScopeDefinition < RBS::AST::Members::Include
12
+ end
13
+
14
+ class RB < RBS::Prototype::RB
15
+ def process(node, decls:, comments:, context:)
16
+ case node.type
17
+ when :FCALL, :VCALL
18
+ case node.children.first
19
+ when :has_many, :has_one, :belongs_to
20
+ decls << AssociationDefinition.new(
21
+ name: RBS::TypeName.new(name: node.children.first, namespace: RBS::Namespace.root),
22
+ args: node.children[1],
23
+ annotations: [],
24
+ location: nil,
25
+ comment: nil
26
+ )
27
+ when :scope
28
+ decls << ScopeDefinition.new(
29
+ name: RBS::TypeName.new(name: node.children.first, namespace: RBS::Namespace.root),
30
+ args: node.children[1],
31
+ annotations: [],
32
+ location: nil,
33
+ comment: nil
34
+ )
35
+ else
36
+ super
37
+ end
38
+ else
39
+ super
40
+ end
41
+ end
42
+ end
43
+
44
+ class Parser
45
+ attr_reader :has_many, :has_one, :belongs_to, :scopes
46
+
47
+ def initialize
48
+ @has_many = []
49
+ @has_one = []
50
+ @belongs_to = []
51
+ @scopes = []
52
+ end
53
+
54
+ def parse(string, target)
55
+ parser = RB.new
56
+ parser.parse(string)
57
+ parser.decls.each do |decl|
58
+ process(decl, target)
59
+ end
60
+ end
61
+
62
+ def process(node, target)
63
+ case node
64
+ when RBS::AST::Declarations::Module, RBS::AST::Declarations::Class
65
+ name = node.name.split
66
+ if target[...name.size] == name
67
+ node.members.each do |member|
68
+ process(member, target[name.size...].to_a)
69
+ end
70
+ end
71
+ when AssociationDefinition
72
+ process_association_definition(node) if target.empty?
73
+ when ScopeDefinition
74
+ process_scope_definition(node) if target.empty?
75
+ end
76
+ end
77
+
78
+ def process_association_definition(node)
79
+ case node.name.name
80
+ when :has_many
81
+ association_id, args = node_to_literal(node.args)
82
+ @has_many << [association_id, args.to_h]
83
+ when :has_one
84
+ association_id, args = node_to_literal(node.args)
85
+ @has_one << [association_id, args.to_h]
86
+ when :belongs_to
87
+ association_id, args = node_to_literal(node.args)
88
+ @belongs_to << [association_id, args.to_h]
89
+ end
90
+ end
91
+
92
+ def process_scope_definition(node)
93
+ scope_id, args = node_to_literal(node.args)
94
+ @scopes << [scope_id, args.to_h]
95
+ end
96
+
97
+ def node_to_literal(node)
98
+ case node.type
99
+ when :LIST
100
+ node.children[...-1].map { |child| node_to_literal(child) }
101
+ when :LIT, :STR
102
+ node.children.first
103
+ when :HASH
104
+ Hash[*node_to_literal(node.children.first)]
105
+ when :LAMBDA
106
+ {} # Convert to empty hash because rbs_active_hash does not process lambda
107
+ else
108
+ node
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -15,11 +15,17 @@ module RbsActiveHash
15
15
  end
16
16
 
17
17
  class Generator
18
- attr_reader :klass, :klass_name
18
+ attr_reader :klass, :klass_name, :parser
19
19
 
20
20
  def initialize(klass)
21
21
  @klass = klass
22
22
  @klass_name = RbsRails::Util.module_name(klass)
23
+ @parser = ActiveHash::Parser::Parser.new
24
+
25
+ path, = Object.const_source_location(klass_name)
26
+ return unless path
27
+
28
+ @parser.parse(IO.read(path.to_s), klass_name.split("::").map(&:to_sym))
23
29
  end
24
30
 
25
31
  def generate
@@ -40,6 +46,7 @@ module RbsActiveHash
40
46
  <<~RBS
41
47
  #{header}
42
48
  #{enum_decls}
49
+ #{scope_decls}
43
50
  #{association_decls}
44
51
  #{method_decls}
45
52
  #{footer}
@@ -79,7 +86,7 @@ module RbsActiveHash
79
86
  include ActiveHash::Enum
80
87
  extend ActiveHash::Enum::Methods
81
88
 
82
- #{constants.map { |c| "#{c}: instance" }.join("\n")}
89
+ #{constants.map { |c| "#{c}: #{klass_name}" }.join("\n")}
83
90
  RBS
84
91
  end
85
92
 
@@ -98,14 +105,18 @@ module RbsActiveHash
98
105
  end
99
106
  end
100
107
 
101
- def association_decls
102
- return unless klass.ancestors.include? ::ActiveHash::Associations
108
+ def scope_decls
109
+ return if parser.scopes.empty?
103
110
 
104
- path, = Object.const_source_location(klass_name)
105
- return unless path
111
+ parser.scopes.map do |scope_id, _|
112
+ <<~RBS
113
+ def self.#{scope_id}: () -> ActiveHash::Relation[instance]
114
+ RBS
115
+ end.join("\n")
116
+ end
106
117
 
107
- parser = Associations::Parser.new
108
- parser.parse(IO.read(path.to_s), klass_name.split("::").map(&:to_sym))
118
+ def association_decls
119
+ return unless klass.ancestors.include? ::ActiveHash::Associations
109
120
 
110
121
  <<~RBS
111
122
  include ActiveHash::Associations
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RbsActiveHash
4
- VERSION = "1.2.1"
4
+ VERSION = "1.3.0"
5
5
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "rbs_active_hash/active_hash"
4
- require_relative "rbs_active_hash/associations"
4
+ require_relative "rbs_active_hash/active_hash/parser"
5
5
  require_relative "rbs_active_hash/version"
6
6
 
7
7
  module RbsActiveHash
@@ -2,7 +2,7 @@
2
2
  sources:
3
3
  - type: git
4
4
  name: ruby/gem_rbs_collection
5
- revision: a4c633634493ab7ae73219022f56acff56ab69af
5
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
6
6
  remote: https://github.com/ruby/gem_rbs_collection.git
7
7
  repo_dir: gems
8
8
  path: ".gem_rbs_collection"
@@ -16,7 +16,7 @@ gems:
16
16
  source:
17
17
  type: git
18
18
  name: ruby/gem_rbs_collection
19
- revision: a4c633634493ab7ae73219022f56acff56ab69af
19
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
20
20
  remote: https://github.com/ruby/gem_rbs_collection.git
21
21
  repo_dir: gems
22
22
  - name: actionview
@@ -24,7 +24,7 @@ gems:
24
24
  source:
25
25
  type: git
26
26
  name: ruby/gem_rbs_collection
27
- revision: a4c633634493ab7ae73219022f56acff56ab69af
27
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
28
28
  remote: https://github.com/ruby/gem_rbs_collection.git
29
29
  repo_dir: gems
30
30
  - name: active_hash
@@ -32,7 +32,7 @@ gems:
32
32
  source:
33
33
  type: git
34
34
  name: ruby/gem_rbs_collection
35
- revision: a4c633634493ab7ae73219022f56acff56ab69af
35
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
36
36
  remote: https://github.com/ruby/gem_rbs_collection.git
37
37
  repo_dir: gems
38
38
  - name: activemodel
@@ -40,7 +40,7 @@ gems:
40
40
  source:
41
41
  type: git
42
42
  name: ruby/gem_rbs_collection
43
- revision: a4c633634493ab7ae73219022f56acff56ab69af
43
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
44
44
  remote: https://github.com/ruby/gem_rbs_collection.git
45
45
  repo_dir: gems
46
46
  - name: activerecord
@@ -48,7 +48,7 @@ gems:
48
48
  source:
49
49
  type: git
50
50
  name: ruby/gem_rbs_collection
51
- revision: a4c633634493ab7ae73219022f56acff56ab69af
51
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
52
52
  remote: https://github.com/ruby/gem_rbs_collection.git
53
53
  repo_dir: gems
54
54
  - name: activesupport
@@ -56,7 +56,7 @@ gems:
56
56
  source:
57
57
  type: git
58
58
  name: ruby/gem_rbs_collection
59
- revision: a4c633634493ab7ae73219022f56acff56ab69af
59
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
60
60
  remote: https://github.com/ruby/gem_rbs_collection.git
61
61
  repo_dir: gems
62
62
  - name: ast
@@ -64,7 +64,7 @@ gems:
64
64
  source:
65
65
  type: git
66
66
  name: ruby/gem_rbs_collection
67
- revision: a4c633634493ab7ae73219022f56acff56ab69af
67
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
68
68
  remote: https://github.com/ruby/gem_rbs_collection.git
69
69
  repo_dir: gems
70
70
  - name: base64
@@ -80,7 +80,7 @@ gems:
80
80
  source:
81
81
  type: git
82
82
  name: ruby/gem_rbs_collection
83
- revision: a4c633634493ab7ae73219022f56acff56ab69af
83
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
84
84
  remote: https://github.com/ruby/gem_rbs_collection.git
85
85
  repo_dir: gems
86
86
  - name: date
@@ -92,7 +92,7 @@ gems:
92
92
  source:
93
93
  type: git
94
94
  name: ruby/gem_rbs_collection
95
- revision: a4c633634493ab7ae73219022f56acff56ab69af
95
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
96
96
  remote: https://github.com/ruby/gem_rbs_collection.git
97
97
  repo_dir: gems
98
98
  - name: json
@@ -120,7 +120,7 @@ gems:
120
120
  source:
121
121
  type: git
122
122
  name: ruby/gem_rbs_collection
123
- revision: a4c633634493ab7ae73219022f56acff56ab69af
123
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
124
124
  remote: https://github.com/ruby/gem_rbs_collection.git
125
125
  repo_dir: gems
126
126
  - name: optparse
@@ -132,7 +132,7 @@ gems:
132
132
  source:
133
133
  type: git
134
134
  name: ruby/gem_rbs_collection
135
- revision: a4c633634493ab7ae73219022f56acff56ab69af
135
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
136
136
  remote: https://github.com/ruby/gem_rbs_collection.git
137
137
  repo_dir: gems
138
138
  - name: pathname
@@ -144,7 +144,7 @@ gems:
144
144
  source:
145
145
  type: git
146
146
  name: ruby/gem_rbs_collection
147
- revision: a4c633634493ab7ae73219022f56acff56ab69af
147
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
148
148
  remote: https://github.com/ruby/gem_rbs_collection.git
149
149
  repo_dir: gems
150
150
  - name: rails-dom-testing
@@ -152,7 +152,7 @@ gems:
152
152
  source:
153
153
  type: git
154
154
  name: ruby/gem_rbs_collection
155
- revision: a4c633634493ab7ae73219022f56acff56ab69af
155
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
156
156
  remote: https://github.com/ruby/gem_rbs_collection.git
157
157
  repo_dir: gems
158
158
  - name: railties
@@ -160,7 +160,7 @@ gems:
160
160
  source:
161
161
  type: git
162
162
  name: ruby/gem_rbs_collection
163
- revision: a4c633634493ab7ae73219022f56acff56ab69af
163
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
164
164
  remote: https://github.com/ruby/gem_rbs_collection.git
165
165
  repo_dir: gems
166
166
  - name: rainbow
@@ -168,7 +168,7 @@ gems:
168
168
  source:
169
169
  type: git
170
170
  name: ruby/gem_rbs_collection
171
- revision: a4c633634493ab7ae73219022f56acff56ab69af
171
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
172
172
  remote: https://github.com/ruby/gem_rbs_collection.git
173
173
  repo_dir: gems
174
174
  - name: rbs
@@ -0,0 +1,28 @@
1
+ module RbsActiveHash
2
+ module ActiveHash
3
+ module Parser
4
+ class AssociationDefinition < RBS::AST::Members::Include
5
+ end
6
+
7
+ class ScopeDefinition < RBS::AST::Members::Include
8
+ end
9
+
10
+ class RB < RBS::Prototype::RB
11
+ end
12
+
13
+ class Parser
14
+ attr_reader has_many: Array[[Symbol, Hash[untyped, untyped]]]
15
+ attr_reader has_one: Array[[Symbol, Hash[untyped, untyped]]]
16
+ attr_reader belongs_to: Array[[Symbol, Hash[untyped, untyped]]]
17
+ attr_reader scopes: Array[[Symbol, Hash[untyped, untyped]]]
18
+
19
+ def initialize: () -> void
20
+ def parse: (String string, Array[Symbol] target) -> void
21
+ def process: (RBS::AST::Declarations::t | RBS::AST::Members::t node, Array[Symbol] target) -> void
22
+ def process_association_definition: (AssociationDefinition node) -> void
23
+ def process_scope_definition: (AssociationDefinition node) -> void
24
+ def node_to_literal: (untyped node) -> untyped
25
+ end
26
+ end
27
+ end
28
+ end
@@ -6,6 +6,7 @@ module RbsActiveHash
6
6
  class Generator
7
7
  attr_reader klass: singleton(ActiveHash::Base)
8
8
  attr_reader klass_name: String
9
+ attr_reader parser: ActiveHash::Parser::Parser
9
10
 
10
11
  def initialize: (singleton(ActiveHash::Base) klass) -> void
11
12
  def generate: () -> String
@@ -16,6 +17,7 @@ module RbsActiveHash
16
17
  def header: () -> String
17
18
  def enum_decls: () -> String?
18
19
  def constants: () -> Array[String]
20
+ def scope_decls: () -> String?
19
21
  def association_decls: () -> String?
20
22
  def has_many_decls: (Array[[Symbol, Hash[untyped, untyped]]]) -> String
21
23
  def has_one_decls: (Array[[Symbol, Hash[untyped, untyped]]]) -> String
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbs_active_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takeshi KOMIYA
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-25 00:00:00.000000000 Z
11
+ date: 2023-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_hash
@@ -72,7 +72,7 @@ files:
72
72
  - lib/generators/rbs_active_hash/install_generator.rb
73
73
  - lib/rbs_active_hash.rb
74
74
  - lib/rbs_active_hash/active_hash.rb
75
- - lib/rbs_active_hash/associations.rb
75
+ - lib/rbs_active_hash/active_hash/parser.rb
76
76
  - lib/rbs_active_hash/rake_task.rb
77
77
  - lib/rbs_active_hash/version.rb
78
78
  - rbs_active_hash.gemspec
@@ -81,7 +81,7 @@ files:
81
81
  - sig/generators/rbs_active_hash/install_generator.rbs
82
82
  - sig/rbs_active_hash.rbs
83
83
  - sig/rbs_active_hash/active_hash.rbs
84
- - sig/rbs_active_hash/associations.rbs
84
+ - sig/rbs_active_hash/active_hash/parser.rbs
85
85
  - sig/rbs_active_hash/rake_task.rbs
86
86
  - sig/shims/rake.rbs
87
87
  - sig/shims/thor.rbs
@@ -1,91 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "rbs"
4
-
5
- module RbsActiveHash
6
- module Associations
7
- class AssociationDefinition < RBS::AST::Members::Include
8
- end
9
-
10
- class RB < RBS::Prototype::RB
11
- def process(node, decls:, comments:, context:)
12
- case node.type
13
- when :FCALL, :VCALL
14
- case node.children.first
15
- when :has_many, :has_one, :belongs_to
16
- decls << AssociationDefinition.new(
17
- name: RBS::TypeName.new(name: node.children.first, namespace: RBS::Namespace.root),
18
- args: node.children[1],
19
- annotations: [],
20
- location: nil,
21
- comment: nil
22
- )
23
- else
24
- super
25
- end
26
- else
27
- super
28
- end
29
- end
30
- end
31
-
32
- class Parser
33
- attr_reader :has_many, :has_one, :belongs_to
34
-
35
- def initialize
36
- @has_many = []
37
- @has_one = []
38
- @belongs_to = []
39
- end
40
-
41
- def parse(string, target)
42
- parser = RB.new
43
- parser.parse(string)
44
- parser.decls.each do |decl|
45
- process(decl, target)
46
- end
47
- end
48
-
49
- def process(node, target)
50
- case node
51
- when RBS::AST::Declarations::Module, RBS::AST::Declarations::Class
52
- name = node.name.split
53
- if target[...name.size] == name
54
- node.members.each do |member|
55
- process(member, target[name.size...].to_a)
56
- end
57
- end
58
- when AssociationDefinition
59
- process_association_definition(node) if target.empty?
60
- end
61
- end
62
-
63
- def process_association_definition(node)
64
- case node.name.name
65
- when :has_many
66
- association_id, args = node_to_literal(node.args)
67
- @has_many << [association_id, args.to_h]
68
- when :has_one
69
- association_id, args = node_to_literal(node.args)
70
- @has_one << [association_id, args.to_h]
71
- when :belongs_to
72
- association_id, args = node_to_literal(node.args)
73
- @belongs_to << [association_id, args.to_h]
74
- end
75
- end
76
-
77
- def node_to_literal(node)
78
- case node.type
79
- when :LIST
80
- node.children[...-1].map { |child| node_to_literal(child) }
81
- when :LIT, :STR
82
- node.children.first
83
- when :HASH
84
- Hash[*node_to_literal(node.children.first)]
85
- else
86
- node
87
- end
88
- end
89
- end
90
- end
91
- end
@@ -1,21 +0,0 @@
1
- module RbsActiveHash
2
- module Associations
3
- class AssociationDefinition < RBS::AST::Members::Include
4
- end
5
-
6
- class RB < RBS::Prototype::RB
7
- end
8
-
9
- class Parser
10
- attr_reader has_many: Array[[Symbol, Hash[untyped, untyped]]]
11
- attr_reader has_one: Array[[Symbol, Hash[untyped, untyped]]]
12
- attr_reader belongs_to: Array[[Symbol, Hash[untyped, untyped]]]
13
-
14
- def initialize: () -> void
15
- def parse: (String string, Array[Symbol] target) -> void
16
- def process: (RBS::AST::Declarations::t | RBS::AST::Members::t node, Array[Symbol] target) -> void
17
- def process_association_definition: (AssociationDefinition node) -> void
18
- def node_to_literal: (untyped node) -> untyped
19
- end
20
- end
21
- end