grape_sorbet 0.0.2 → 0.0.3

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: 3aadd0dde4fcc5037da59fbc26226c5d54d94686cc4024875e9cf9e21e547df8
4
- data.tar.gz: 9ca6394902e1aea0a6facfcb584c3bac8c03ff580f5a3c8501962fcb737d7154
3
+ metadata.gz: afca4010268efe01d37b64b7cfd4bbd7f6266779a3a135ab04fe14869ab71daf
4
+ data.tar.gz: ee494b0147c0b9d32ae90a5a1fba81218687b26396131a56ff9a1bb16306d693
5
5
  SHA512:
6
- metadata.gz: 212aad3e25427f5d9329061e20d308d54daea04584c65d6cd0c5f8d542b7396287cb43a76f93999fea67c24883610e6f9d56e399bf59f7a65737ad51b21b4b41
7
- data.tar.gz: 56f4c190b13f6763ad0e2a4b051346011eb976c3664de34e5b915f886a2c2970054b8e1946b5738fa2f439c1ecb88fc1f094d80e63145529a0e9b861fb0faa69
6
+ metadata.gz: 122aa7dab2572d6df1cfc79a3ec7a80b652f5ad7d5d51d87b0e9077385e3fb8041fd75b190f76ce4c58b4f1aea575dd1194d505b21c5d9ff32d80b9fca1aa33a
7
+ data.tar.gz: 4eb298722d0e14cb745a20917899825463fd50d6b87b0e1d9541566c9bc5961effec782c7ec767cacc9bcf37536596e0f885c3ae9eb5bdf60717f32d1833c341
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.0.3 (2024-06-05)
2
+
3
+ - Better support for callbacks
4
+
1
5
  ## 0.0.2 (2024-06-05)
2
6
 
3
7
  - Added support for callbacks (`before`, `after`, etc.)
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- grape_sorbet (0.0.2)
4
+ grape_sorbet (0.0.3)
5
5
  activesupport
6
6
  grape (~> 2.0)
7
7
  sorbet-runtime (~> 0.5.10741)
data/README.md CHANGED
@@ -90,7 +90,29 @@ module Twitter
90
90
  end
91
91
  ```
92
92
 
93
- At this point Sorbet is still reporting errors in the `get :home_timeline` block, because it doesn't know that these blocks are executed in a context where the `Helpers` module methods will be available. The Tapioca compiler provided by this gem will fix that. Run `bundle exec tapioca dsl`. This should generate a `sorbet/rbi/dsl/twitter/api.rbi` file (assuming the source file is in `lib/twitter/api.rb`). After this,
93
+ At this point Sorbet is still reporting errors in the `get :home_timeline` block, because it doesn't know that these blocks are executed in a context where the `Helpers` module methods will be available. The Tapioca compiler provided by this gem will fix that. Run `bundle exec tapioca dsl`. This should generate a `sorbet/rbi/dsl/twitter/api.rbi` file (assuming the source file is in `lib/twitter/api.rb`).
94
+
95
+ After this, Sorbet should no longer report any errors.
96
+
97
+ ## Limitations and known issues
98
+
99
+ ### Subclassing from `Grape::API::Instance` instead of `Grape::API`
100
+
101
+ Grape overrides `Grape::API.new` and uses the `inherited` hook so that subclasses of `Grape::API` are really subclasses of `Grape::API::Instance`.
102
+
103
+ This might be fixable in a future update of grape_sorbet, but is very low priority.
104
+
105
+ ### Not being able to call `helpers` with block arguments
106
+
107
+ Possibly fixable in a future update.
108
+
109
+ ### Having to use `T.bind(self, T.any(Grape::Endpoint, <HelpersModuleName>))` in helper methods
110
+
111
+ Possibly fixable in a future update.
112
+
113
+ ### Having to use `T.bind(self, T.any(Grape::Endpoint, <HelpersModuleName>))` in `before` and `after` callback blocks
114
+
115
+ The compiler already generates proper signatures for callback methods so `T.bind` should not be needed (and is in fact unneeded for the other callback methods, `before_validation`, `after_validation` and `finally`). The reason it doesn't work for `before` and `after` is because of a [bug](https://github.com/sorbet/sorbet/issues/7950) in Sorbet itself.
94
116
 
95
117
  ## Development
96
118
 
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module GrapeSorbet
5
- VERSION = "0.0.2"
5
+ VERSION = "0.0.3"
6
6
  end
@@ -80,13 +80,13 @@ module Tapioca
80
80
  end
81
81
  end
82
82
 
83
- HTTP_VERB_METHODS = T.let(
84
- [:get, :post, :put, :patch, :delete, :head, :options].freeze,
83
+ CALLBACKS_METHODS = T.let(
84
+ [:before, :before_validation, :after_validation, :after, :finally].freeze,
85
85
  T::Array[Symbol],
86
86
  )
87
87
 
88
- CALLBACKS_METHODS = T.let(
89
- [:before, :before_validation, :after_validation, :after, :finally].freeze,
88
+ HTTP_VERB_METHODS = T.let(
89
+ [:get, :post, :put, :patch, :delete, :head, :options].freeze,
90
90
  T::Array[Symbol],
91
91
  )
92
92
 
@@ -100,6 +100,14 @@ module Tapioca
100
100
  )
101
101
  end
102
102
 
103
+ sig { returns(RBI::Scope) }
104
+ def callbacks_methods_module
105
+ @callbacks_methods_module ||= T.let(
106
+ api.create_module(CallbacksMethodsModuleName),
107
+ T.nilable(RBI::Scope),
108
+ )
109
+ end
110
+
103
111
  sig { returns(RBI::Scope) }
104
112
  def routing_methods_module
105
113
  @routing_methods_module ||= T.let(
@@ -110,6 +118,7 @@ module Tapioca
110
118
 
111
119
  sig { void }
112
120
  def create_classes_and_includes
121
+ api.create_extend(CallbacksMethodsModuleName)
113
122
  api.create_extend(RoutingMethodsModuleName)
114
123
  create_api_class
115
124
  create_endpoint_class
@@ -141,6 +150,19 @@ module Tapioca
141
150
  end
142
151
  end
143
152
 
153
+ sig { void }
154
+ def create_callbacks_methods
155
+ CALLBACKS_METHODS.each do |callback|
156
+ callbacks_methods_module.create_method(
157
+ callback.to_s,
158
+ parameters: [
159
+ create_block_param("block", type: "T.proc.bind(#{EndpointClassName}).void"),
160
+ ],
161
+ return_type: "void",
162
+ )
163
+ end
164
+ end
165
+
144
166
  sig { void }
145
167
  def create_routing_methods
146
168
  HTTP_VERB_METHODS.each do |verb|
@@ -148,7 +170,7 @@ module Tapioca
148
170
  verb.to_s,
149
171
  parameters: [
150
172
  create_rest_param("args", type: "T.untyped"),
151
- create_block_param("blk", type: "T.nilable(T.proc.bind(#{EndpointClassName}).void)"),
173
+ create_block_param("block", type: "T.nilable(T.proc.bind(#{EndpointClassName}).void)"),
152
174
  ],
153
175
  return_type: "void",
154
176
  )
@@ -158,26 +180,12 @@ module Tapioca
158
180
  "route_param",
159
181
  parameters: [
160
182
  create_param("param", type: "Symbol"),
161
- create_opt_param("options", type: "T.nilable(T::Hash[Symbol, T.untyped])", default: "nil"),
162
- create_block_param("blk", type: "T.nilable(T.proc.bind(T.class_of(#{APIInstanceClassName})).void)"),
183
+ create_opt_param("options", type: "T::Hash[Symbol, T.untyped]", default: "{}"),
184
+ create_block_param("block", type: "T.nilable(T.proc.bind(T.class_of(#{APIInstanceClassName})).void)"),
163
185
  ],
164
186
  return_type: "void",
165
187
  )
166
188
  end
167
-
168
- sig { void }
169
- def create_callbacks_methods
170
- CALLBACKS_METHODS.each do |callback|
171
- routing_methods_module.create_method(
172
- callback.to_s,
173
- parameters: [
174
- create_rest_param("args", type: "T.untyped"),
175
- create_block_param("blk", type: "T.nilable(T.proc.bind(#{EndpointClassName}).void)"),
176
- ],
177
- return_type: "void",
178
- )
179
- end
180
- end
181
189
  end
182
190
  end
183
191
  end
@@ -7,6 +7,7 @@ module Tapioca
7
7
  module GrapeConstantsHelper
8
8
  extend T::Sig
9
9
 
10
+ CallbacksMethodsModuleName = "GeneratedCallbacksMethods"
10
11
  RoutingMethodsModuleName = "GeneratedRoutingMethods"
11
12
 
12
13
  APIInstanceClassName = "PrivateAPIInstance"
data/rbi/grape.rbi CHANGED
@@ -2,11 +2,6 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Grape
5
- module DSL::Callbacks::ClassMethods
6
- sig { params(block: T.proc.bind(Grape::Endpoint).void).void }
7
- def before(&block); end
8
- end
9
-
10
5
  module DSL::Desc
11
6
  # grape evaluates config_block in the context of a dynamically created module that implements the DSL it exposes
12
7
  # at runtime. There's no good way to represent this statically, so block is just typed as T.untyped to prevent
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape_sorbet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thatch Health, Inc.