rbs_draper 1.0.0 → 1.1.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: 3ab0cf7b71bd6ed489df32f624962c6a8cb46e2fe3e05a6ce9a6864106465dde
4
- data.tar.gz: 4f9db1dd8274da25ff5907544647ae9c4e8160fe5ed3fbb5aa6dd1a77ab0daba
3
+ metadata.gz: 4264323a47d34195f99e7e52dfed772f191d1c1b6ff2b67aea3ae8bebcc2c207
4
+ data.tar.gz: 884b7eb9869e61ab465d63ac2f5878226491bbd40b7612a6bf9dae071d1a16c2
5
5
  SHA512:
6
- metadata.gz: c12feee4cf6e98dbfa4e172f1e569f51ecc1cf163a8e26ddd08fb72a44e5de60beaf6f8469871a2752547765ea0506335977c8da0cdc6c532f8595649914eb50
7
- data.tar.gz: 408294ee26cc2e601d40b035e6537cff035d46e6b2baf3422549665be48982a383d4cb0169d4bd5fe98cebd90bbaeac04f5f8b3b61a007948a5513203a000520
6
+ metadata.gz: b4489ad1ad8ae877970dfae0d89ae95466aa5796cedb4fa0b9af95be5fed7fc0096b6ca0266c0d1e0ef660ed80394865c19f44f7f74f338919d52a99af60e4ac
7
+ data.tar.gz: 9130a8eaa678d68be89334e8927c06f9d18a6356fb85cbf3fcd237eace146a3a4326a3b07792978f833988fd555542af307b486d1caf99c022c91a709ed7491e
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rbs_draper (1.0.0)
4
+ rbs_draper (1.1.0)
5
5
  activesupport
6
6
  draper
7
7
  fileutils
@@ -11,29 +11,29 @@ PATH
11
11
  GEM
12
12
  remote: https://rubygems.org/
13
13
  specs:
14
- actionpack (7.0.7)
15
- actionview (= 7.0.7)
16
- activesupport (= 7.0.7)
14
+ actionpack (7.0.7.2)
15
+ actionview (= 7.0.7.2)
16
+ activesupport (= 7.0.7.2)
17
17
  rack (~> 2.0, >= 2.2.4)
18
18
  rack-test (>= 0.6.3)
19
19
  rails-dom-testing (~> 2.0)
20
20
  rails-html-sanitizer (~> 1.0, >= 1.2.0)
21
- actionview (7.0.7)
22
- activesupport (= 7.0.7)
21
+ actionview (7.0.7.2)
22
+ activesupport (= 7.0.7.2)
23
23
  builder (~> 3.1)
24
24
  erubi (~> 1.4)
25
25
  rails-dom-testing (~> 2.0)
26
26
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
27
- activemodel (7.0.7)
28
- activesupport (= 7.0.7)
27
+ activemodel (7.0.7.2)
28
+ activesupport (= 7.0.7.2)
29
29
  activemodel-serializers-xml (1.0.2)
30
30
  activemodel (> 5.x)
31
31
  activesupport (> 5.x)
32
32
  builder (~> 3.1)
33
- activerecord (7.0.7)
34
- activemodel (= 7.0.7)
35
- activesupport (= 7.0.7)
36
- activesupport (7.0.7)
33
+ activerecord (7.0.7.2)
34
+ activemodel (= 7.0.7.2)
35
+ activesupport (= 7.0.7.2)
36
+ activesupport (7.0.7.2)
37
37
  concurrent-ruby (~> 1.0, >= 1.0.2)
38
38
  i18n (>= 1.6, < 2)
39
39
  minitest (>= 5.1)
@@ -91,9 +91,9 @@ GEM
91
91
  rails-html-sanitizer (1.6.0)
92
92
  loofah (~> 2.21)
93
93
  nokogiri (~> 1.14)
94
- railties (7.0.7)
95
- actionpack (= 7.0.7)
96
- activesupport (= 7.0.7)
94
+ railties (7.0.7.2)
95
+ actionpack (= 7.0.7.2)
96
+ activesupport (= 7.0.7.2)
97
97
  method_source
98
98
  rake (>= 12.2)
99
99
  thor (~> 1.0)
@@ -127,7 +127,7 @@ GEM
127
127
  diff-lcs (>= 1.2.0, < 2.0)
128
128
  rspec-support (~> 3.12.0)
129
129
  rspec-support (3.12.0)
130
- rubocop (1.56.0)
130
+ rubocop (1.56.1)
131
131
  base64 (~> 0.1.1)
132
132
  json (~> 2.3)
133
133
  language_server-protocol (>= 3.17.0)
data/README.md CHANGED
@@ -14,6 +14,13 @@ After the installation, please run rake task generator:
14
14
 
15
15
  bundle exec rails g rbs_draper:install
16
16
 
17
+ If you have decorators having different names from models, you can specify the mapping from
18
+ decorator names to model names in `lib/tasks/rbs_draper.rake`. For example:
19
+
20
+ RbsDraper::RakeTask.new do |task|
21
+ task.mapping = -> { { AdminUserDecorator: User } }
22
+ end
23
+
17
24
  ## Usage
18
25
 
19
26
  1. Run `rbs:draper:clean` at first
@@ -8,7 +8,13 @@ module RbsDraper
8
8
  create_file "lib/tasks/rbs_draper.rake", <<~RUBY
9
9
  begin
10
10
  require 'rbs_draper/rake_task'
11
- RbsDraper::RakeTask.new
11
+
12
+ RbsDraper::RakeTask.new do |task|
13
+ # If you have decorators having different names from models, you can specify the mapping from decorator names to model names.
14
+ # The value should be a lambda that returns a hash to load decorators and models easily.
15
+ #
16
+ # task.mapping = -> { { AdminUserDecorator: User } }
17
+ end
12
18
  rescue LoadError
13
19
  # failed to load rbs_draper. Skip to load rbs_draper tasks.
14
20
  end
@@ -6,21 +6,22 @@ require "rbs_rails"
6
6
 
7
7
  module RbsDraper
8
8
  module Decorator
9
- def self.class_to_rbs(klass, rbs_builder)
10
- Generator.new(klass, rbs_builder).generate
9
+ def self.class_to_rbs(klass, rbs_builder, decorated_class: nil)
10
+ Generator.new(klass, rbs_builder, decorated_class: decorated_class).generate
11
11
  end
12
12
 
13
13
  class Generator
14
- attr_reader :klass, :klass_name, :rbs_builder
14
+ attr_reader :klass, :klass_name, :rbs_builder, :decorated_class
15
15
 
16
- def initialize(klass, rbs_builder)
16
+ def initialize(klass, rbs_builder, decorated_class: nil)
17
17
  @klass = klass
18
18
  @klass_name = RbsRails::Util.module_name(klass)
19
19
  @rbs_builder = rbs_builder
20
+ @decorated_class = decorated_class
20
21
  end
21
22
 
22
23
  def generate
23
- return if decorated_class.blank?
24
+ return if decorated_class_def.blank?
24
25
 
25
26
  RbsRails::Util.format_rbs klass_decl
26
27
  end
@@ -58,7 +59,7 @@ module RbsDraper
58
59
  end
59
60
 
60
61
  def object_method_decls
61
- object_name = klass_name.to_s.sub(/Decorator$/, "")
62
+ object_name = decorated_class&.name || klass.name.to_s.sub(/Decorator$/, "")
62
63
  if object_name.include?("::")
63
64
  "def object: () -> #{object_name}"
64
65
  else
@@ -87,9 +88,10 @@ module RbsDraper
87
88
  end
88
89
  end
89
90
 
90
- def decorated_class
91
- type_name = RBS::TypeName(klass.name.to_s.sub(/Decorator$/, "")).absolute!
92
- @decorated_class ||= rbs_builder.build_instance(type_name)
91
+ def decorated_class_def
92
+ class_name = decorated_class&.name || klass.name.to_s.sub(/Decorator$/, "")
93
+ type_name = RBS::TypeName(class_name).absolute!
94
+ @decorated_class_def ||= rbs_builder.build_instance(type_name)
93
95
  rescue StandardError
94
96
  nil
95
97
  end
@@ -97,7 +99,7 @@ module RbsDraper
97
99
  def delegated_methods
98
100
  return [] unless klass.ancestors.include? ::Draper::AutomaticDelegation
99
101
 
100
- decorated_klass = decorated_class
102
+ decorated_klass = decorated_class_def
101
103
  return [] unless decorated_klass
102
104
 
103
105
  decorated_klass.methods.keys.sort.filter_map do |name|
@@ -8,13 +8,14 @@ require "rake/tasklib"
8
8
 
9
9
  module RbsDraper
10
10
  class RakeTask < Rake::TaskLib
11
- attr_accessor :name, :signature_root_dir
11
+ attr_accessor :name, :signature_root_dir, :mapping
12
12
 
13
13
  def initialize(name = :'rbs:draper', &block)
14
14
  super()
15
15
 
16
16
  @name = name
17
17
  @signature_root_dir = Pathname(Rails.root / "sig/draper")
18
+ @mapping = -> { {} }
18
19
 
19
20
  block&.call(self)
20
21
 
@@ -74,10 +75,11 @@ module RbsDraper
74
75
  task("#{name}:decorators:generate": :environment) do
75
76
  Rails.application.eager_load!
76
77
 
78
+ decorated_classes = mapping.call
77
79
  Draper::Decorator.descendants.each do |klass|
78
80
  path = signature_root_dir / "app/decorators/#{klass.name.underscore}.rbs"
79
81
  path.dirname.mkpath
80
- rbs = RbsDraper::Decorator.class_to_rbs(klass, rbs_builder)
82
+ rbs = RbsDraper::Decorator.class_to_rbs(klass, rbs_builder, decorated_class: decorated_classes[klass])
81
83
  path.write rbs if rbs
82
84
  end
83
85
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RbsDraper
4
- VERSION = "1.0.0"
4
+ VERSION = "1.1.0"
5
5
  end
@@ -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: activemodel
@@ -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: activerecord
@@ -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: activesupport
@@ -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: ast
@@ -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: base64
@@ -72,7 +72,7 @@ gems:
72
72
  source:
73
73
  type: git
74
74
  name: ruby/gem_rbs_collection
75
- revision: a4c633634493ab7ae73219022f56acff56ab69af
75
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
76
76
  remote: https://github.com/ruby/gem_rbs_collection.git
77
77
  repo_dir: gems
78
78
  - name: date
@@ -88,7 +88,7 @@ gems:
88
88
  source:
89
89
  type: git
90
90
  name: ruby/gem_rbs_collection
91
- revision: a4c633634493ab7ae73219022f56acff56ab69af
91
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
92
92
  remote: https://github.com/ruby/gem_rbs_collection.git
93
93
  repo_dir: gems
94
94
  - name: json
@@ -116,7 +116,7 @@ gems:
116
116
  source:
117
117
  type: git
118
118
  name: ruby/gem_rbs_collection
119
- revision: a4c633634493ab7ae73219022f56acff56ab69af
119
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
120
120
  remote: https://github.com/ruby/gem_rbs_collection.git
121
121
  repo_dir: gems
122
122
  - name: optparse
@@ -128,7 +128,7 @@ gems:
128
128
  source:
129
129
  type: git
130
130
  name: ruby/gem_rbs_collection
131
- revision: a4c633634493ab7ae73219022f56acff56ab69af
131
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
132
132
  remote: https://github.com/ruby/gem_rbs_collection.git
133
133
  repo_dir: gems
134
134
  - name: pathname
@@ -140,7 +140,7 @@ gems:
140
140
  source:
141
141
  type: git
142
142
  name: ruby/gem_rbs_collection
143
- revision: a4c633634493ab7ae73219022f56acff56ab69af
143
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
144
144
  remote: https://github.com/ruby/gem_rbs_collection.git
145
145
  repo_dir: gems
146
146
  - name: rails-dom-testing
@@ -148,7 +148,7 @@ gems:
148
148
  source:
149
149
  type: git
150
150
  name: ruby/gem_rbs_collection
151
- revision: a4c633634493ab7ae73219022f56acff56ab69af
151
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
152
152
  remote: https://github.com/ruby/gem_rbs_collection.git
153
153
  repo_dir: gems
154
154
  - name: railties
@@ -156,7 +156,7 @@ gems:
156
156
  source:
157
157
  type: git
158
158
  name: ruby/gem_rbs_collection
159
- revision: a4c633634493ab7ae73219022f56acff56ab69af
159
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
160
160
  remote: https://github.com/ruby/gem_rbs_collection.git
161
161
  repo_dir: gems
162
162
  - name: rainbow
@@ -164,7 +164,7 @@ gems:
164
164
  source:
165
165
  type: git
166
166
  name: ruby/gem_rbs_collection
167
- revision: a4c633634493ab7ae73219022f56acff56ab69af
167
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
168
168
  remote: https://github.com/ruby/gem_rbs_collection.git
169
169
  repo_dir: gems
170
170
  - name: rbs
@@ -184,7 +184,7 @@ gems:
184
184
  source:
185
185
  type: git
186
186
  name: ruby/gem_rbs_collection
187
- revision: a4c633634493ab7ae73219022f56acff56ab69af
187
+ revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
188
188
  remote: https://github.com/ruby/gem_rbs_collection.git
189
189
  repo_dir: gems
190
190
  - name: securerandom
@@ -1,16 +1,17 @@
1
1
  module RbsDraper
2
2
  module Decorator
3
- def self.class_to_rbs: (singleton(Draper::Decorator) klass, RBS::DefinitionBuilder rbs_builder) -> String?
3
+ def self.class_to_rbs: (singleton(Draper::Decorator) klass, RBS::DefinitionBuilder rbs_builder, ?decorated_class: singleton(Class)?) -> String?
4
4
 
5
5
  class Generator
6
6
  attr_reader klass: singleton(Draper::Decorator)
7
7
  attr_reader klass_name: String
8
8
  attr_reader rbs_builder: RBS::DefinitionBuilder
9
+ attr_reader decorated_class: singleton(Class)?
9
10
 
10
- @decorated_class: RBS::Definition
11
+ @decorated_class_def: RBS::Definition
11
12
  @user_defined_class: RBS::Definition
12
13
 
13
- def initialize: (singleton(Draper::Decorator) klass, RBS::DefinitionBuilder rbs_builder) -> void
14
+ def initialize: (singleton(Draper::Decorator) klass, RBS::DefinitionBuilder rbs_builder, ?decorated_class: singleton(Class)?) -> void
14
15
  def generate: () -> String?
15
16
 
16
17
  private
@@ -21,7 +22,7 @@ module RbsDraper
21
22
  def method_decls: () -> String?
22
23
  def footer: () -> String
23
24
  def module_names: () -> Array[String]
24
- def decorated_class: () -> RBS::Definition?
25
+ def decorated_class_def: () -> RBS::Definition?
25
26
  def delegated_methods: () -> Array[[Symbol, RBS::Definition::Method]]
26
27
  def user_defined_class: () -> RBS::Definition?
27
28
  end
@@ -2,10 +2,12 @@ module RbsDraper
2
2
  class RakeTask < Rake::TaskLib
3
3
  attr_accessor name: Symbol
4
4
  attr_accessor signature_root_dir: Pathname
5
+ attr_accessor mapping: ^() -> Hash[singleton(Class), singleton(Class)]
5
6
 
6
7
  @name: Symbol
7
8
  @rbs_builder: RBS::DefinitionBuilder
8
9
  @signature_root_dir: Pathname
10
+ @mapping: ^() -> Hash[singleton(Class), singleton(Class)]
9
11
 
10
12
  def initialize: (?Symbol name) ?{ (RakeTask) -> void } -> void
11
13
  def define_setup_task: () -> void
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbs_draper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.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: activesupport