Skip to content

Commit ec09bbb

Browse files
ericproulxclaude
andauthored
Define Grape's callable constants as modules rather than lambdas (#3096)
A proc carries the self it was written under and the variables it closes over, so freezing one does not make it shareable; a module is shareable as it is. DEFAULT_LAMBDA_FORMATTER, FORWARD_MATCH_METHOD, NON_FORWARD_MATCH_METHOD and RUN_ENDPOINT become modules answering call. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 197369a commit ec09bbb

4 files changed

Lines changed: 26 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* [#3090](https://github.com/ruby-grape/grape/pull/3090): Call Danger's workflows from danger/danger-pr-comment, where they moved - [@ericproulx](https://github.com/ericproulx).
1616
* [#3092](https://github.com/ruby-grape/grape/pull/3092): Skip posting empty Danger report comments - [@dblock](https://github.com/dblock).
1717
* [#3095](https://github.com/ruby-grape/grape/pull/3095): Freeze Grape's constants all the way down - [@ericproulx](https://github.com/ericproulx).
18+
* [#3096](https://github.com/ruby-grape/grape/pull/3096): Define Grape's callable constants as modules rather than lambdas - [@ericproulx](https://github.com/ericproulx).
1819
* Your contribution here.
1920

2021
#### Fixes

‎lib/grape/endpoint/rack_stack.rb‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ class Endpoint
88
# compile time, from the endpoint's settings.
99
module RackStack
1010
# The innermost app of every endpoint's Rack stack. The endpoint to run
11-
# comes out of the env, so the lambda holds no state and one of them serves
12-
# every stack, rather than one being allocated per endpoint.
13-
RUN_ENDPOINT = ->(env) { env[Grape::Env::API_ENDPOINT].run }
14-
private_constant :RUN_ENDPOINT
11+
# comes out of the env, so the app holds no state and one of them serves
12+
# every stack, rather than one being allocated per endpoint. A module
13+
# rather than a lambda, which carries the self it was written under and
14+
# so cannot be frozen all the way down.
15+
module RunEndpoint
16+
def self.call(env) = env[Grape::Env::API_ENDPOINT].run
17+
end
18+
private_constant :RunEndpoint
1519

1620
module_function
1721

@@ -27,7 +31,7 @@ def build(setting)
2731
stack.use Grape::Middleware::Formatter, **formatter_middleware_options(setting, format, content_types)
2832

2933
builder = stack.build
30-
builder.run RUN_ENDPOINT
34+
builder.run RunEndpoint
3135
builder.to_app
3236
end
3337

‎lib/grape/formatter.rb‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ module Formatter
66

77
module_function
88

9-
DEFAULT_LAMBDA_FORMATTER = ->(obj, _env) { obj }
9+
# Answers the body as it is, for a format nothing is registered for. A
10+
# module like the formatters registered here, rather than a lambda: a
11+
# lambda carries the self it was written under, so it cannot be frozen
12+
# all the way down.
13+
DEFAULT_LAMBDA_FORMATTER = Module.new do
14+
def self.call(obj, _env) = obj
15+
end
1016

1117
def formatter_for(api_format, formatters)
1218
return formatters[api_format] if formatters&.key?(api_format)

‎lib/grape/router/route.rb‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
module Grape
44
class Router
55
class Route < BaseRoute
6-
FORWARD_MATCH_METHOD = ->(input, pattern) { input.start_with?(pattern.origin) }
7-
NON_FORWARD_MATCH_METHOD = ->(input, pattern) { pattern.match?(input) }
6+
# Modules rather than lambdas, which carry the self they were written
7+
# under and so cannot be frozen all the way down.
8+
FORWARD_MATCH_METHOD = Module.new do
9+
def self.call(input, pattern) = input.start_with?(pattern.origin)
10+
end
11+
12+
NON_FORWARD_MATCH_METHOD = Module.new do
13+
def self.call(input, pattern) = pattern.match?(input)
14+
end
815

916
attr_reader :app, :request_method, :index
1017

0 commit comments

Comments
 (0)