Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions lib/grape/endpoint/rack_stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
8 changes: 7 additions & 1 deletion lib/grape/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions lib/grape/router/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading