Skip to content

Commit 1c6195b

Browse files
committed
Update UPGRADING notes regarding return usage and simplify block calling in endpoint.
1 parent a28218b commit 1c6195b

File tree

3 files changed

+11
-18
lines changed

3 files changed

+11
-18
lines changed

UPGRADING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ See [#2617](https://github.com/ruby-grape/grape/pull/2617) for more information.
2323

2424
#### Endpoint execution simplified and `return` deprecated
2525

26-
Executing a endpoint's block has been simplified and calling `return` in it has been deprecated.
26+
Executing a endpoint's block has been simplified and calling `return` in it has been deprecated. Use `next` instead.
2727

2828
See [#2577](https://github.com/ruby-grape/grape/pull/2577) for more information.
2929

lib/grape/endpoint.rb

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,10 @@ def initialize(new_settings, options = {}, &block)
7171

7272
@lazy_initialize_lock = Mutex.new
7373
@lazy_initialized = nil
74-
@block = nil
75-
7674
@status = nil
7775
@stream = nil
7876
@body = nil
79-
80-
if block
81-
@source = block
82-
@block = lambda do |endpoint_instance|
83-
ActiveSupport::Notifications.instrument('endpoint_render.grape', endpoint: endpoint_instance) do
84-
endpoint_instance.instance_exec(&block)
85-
rescue LocalJumpError => e
86-
Grape.deprecator.warn 'Using `return` in an endpoint has been deprecated.'
87-
return e.exit_value
88-
end
89-
end
90-
end
91-
77+
@source = block
9278
@helpers = build_helpers
9379
end
9480

@@ -255,7 +241,14 @@ def run
255241
end
256242

257243
def execute
258-
@block&.call(self)
244+
return unless @source
245+
246+
ActiveSupport::Notifications.instrument('endpoint_render.grape', endpoint: self) do
247+
instance_exec(&@source)
248+
rescue LocalJumpError => e
249+
Grape.deprecator.warn 'Using `return` in an endpoint has been deprecated. Use `next` instead'
250+
return e.exit_value
251+
end
259252
end
260253

261254
def lazy_initialize!

spec/grape/endpoint_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ def memoized
776776
return 'Hello'
777777
end
778778

779-
expect(Grape.deprecator).to receive(:warn).with('Using `return` in an endpoint has been deprecated.')
779+
expect(Grape.deprecator).to receive(:warn).with('Using `return` in an endpoint has been deprecated. Use `next` instead')
780780

781781
get '/home'
782782
expect(last_response.status).to eq(200)

0 commit comments

Comments
 (0)