diff --git a/CHANGELOG.md b/CHANGELOG.md index c626c9ba4..390ec80b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ * [#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). * [#3092](https://github.com/ruby-grape/grape/pull/3092): Skip posting empty Danger report comments - [@dblock](https://github.com/dblock). * [#3095](https://github.com/ruby-grape/grape/pull/3095): Freeze Grape's constants all the way down - [@ericproulx](https://github.com/ericproulx). +* [#3096](https://github.com/ruby-grape/grape/pull/3096): Define Grape's callable constants as modules rather than lambdas - [@ericproulx](https://github.com/ericproulx). * Your contribution here. #### Fixes diff --git a/lib/grape/endpoint/rack_stack.rb b/lib/grape/endpoint/rack_stack.rb index f9bf4ac8b..152a7f878 100644 --- a/lib/grape/endpoint/rack_stack.rb +++ b/lib/grape/endpoint/rack_stack.rb @@ -8,10 +8,14 @@ class Endpoint # compile time, from the endpoint's settings. module RackStack # The innermost app of every endpoint's Rack stack. The endpoint to run - # comes out of the env, so the lambda holds no state and one of them serves - # every stack, rather than one being allocated per endpoint. - RUN_ENDPOINT = ->(env) { env[Grape::Env::API_ENDPOINT].run } - private_constant :RUN_ENDPOINT + # comes out of the env, so the app holds no state and one of them serves + # every stack, rather than one being allocated per endpoint. A module + # rather than a lambda, which carries the self it was written under and + # so cannot be frozen all the way down. + module RunEndpoint + def self.call(env) = env[Grape::Env::API_ENDPOINT].run + end + private_constant :RunEndpoint module_function @@ -27,7 +31,7 @@ def build(setting) stack.use Grape::Middleware::Formatter, **formatter_middleware_options(setting, format, content_types) builder = stack.build - builder.run RUN_ENDPOINT + builder.run RunEndpoint builder.to_app end diff --git a/lib/grape/formatter.rb b/lib/grape/formatter.rb index 6d5affb34..891c076e5 100644 --- a/lib/grape/formatter.rb +++ b/lib/grape/formatter.rb @@ -6,7 +6,13 @@ module Formatter module_function - DEFAULT_LAMBDA_FORMATTER = ->(obj, _env) { obj } + # Answers the body as it is, for a format nothing is registered for. A + # module like the formatters registered here, rather than a lambda: a + # lambda carries the self it was written under, so it cannot be frozen + # all the way down. + DEFAULT_LAMBDA_FORMATTER = Module.new do + def self.call(obj, _env) = obj + end def formatter_for(api_format, formatters) return formatters[api_format] if formatters&.key?(api_format) diff --git a/lib/grape/router/route.rb b/lib/grape/router/route.rb index c2744784b..db6396b3d 100644 --- a/lib/grape/router/route.rb +++ b/lib/grape/router/route.rb @@ -3,8 +3,15 @@ module Grape class Router class Route < BaseRoute - FORWARD_MATCH_METHOD = ->(input, pattern) { input.start_with?(pattern.origin) } - NON_FORWARD_MATCH_METHOD = ->(input, pattern) { pattern.match?(input) } + # Modules rather than lambdas, which carry the self they were written + # under and so cannot be frozen all the way down. + FORWARD_MATCH_METHOD = Module.new do + def self.call(input, pattern) = input.start_with?(pattern.origin) + end + + NON_FORWARD_MATCH_METHOD = Module.new do + def self.call(input, pattern) = pattern.match?(input) + end attr_reader :app, :request_method, :index