@@ -11,17 +11,12 @@ class Endpoint
1111 include Grape ::DSL ::Headers
1212 include Grape ::DSL ::InsideRoute
1313
14- attr_accessor :block , :source , :options
15- attr_reader :env , :request
14+ attr_reader :env , :request , :source , :options
1615
1716 def_delegators :request , :params , :headers , :cookies
1817 def_delegator :cookies , :response_cookies
1918
2019 class << self
21- def new ( ...)
22- self == Endpoint ? Class . new ( Endpoint ) . new ( ...) : super
23- end
24-
2520 def before_each ( new_setup = false , &block )
2621 @before_each ||= [ ]
2722 if new_setup == false
@@ -82,17 +77,19 @@ def initialize(new_settings, options = {}, &block)
8277 @stream = nil
8378 @body = nil
8479
85- return unless block
86-
87- @source = block
88- @block = lambda do | endpoint_instance |
89- ActiveSupport :: Notifications . instrument ( 'endpoint_render.grape' , endpoint : endpoint_instance ) do
90- endpoint_instance . instance_exec ( & block )
91- rescue LocalJumpError => e
92- Grape . deprecator . warn 'Using ` return` in an endpoint has been deprecated.'
93- return e . exit_value
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
9489 end
9590 end
91+
92+ @helpers = build_helpers
9693 end
9794
9895 # Update our settings from a given set of stackable parameters. Used when
@@ -194,6 +191,8 @@ def call(env)
194191 def call! ( env )
195192 env [ Grape ::Env ::API_ENDPOINT ] = self
196193 @env = env
194+ # this adds the helpers only to the instance
195+ singleton_class . include ( @helpers ) if @helpers
197196 @app . call ( env )
198197 end
199198
@@ -259,19 +258,13 @@ def execute
259258 @block &.call ( self )
260259 end
261260
262- def helpers
263- lazy_initialize! && @helpers
264- end
265-
266261 def lazy_initialize!
267262 return true if @lazy_initialized
268263
269264 @lazy_initialize_lock . synchronize do
270265 return true if @lazy_initialized
271266
272- @helpers = build_helpers &.tap { |mod | self . class . include mod }
273- @app = options [ :app ] || build_stack ( @helpers )
274-
267+ @app = options [ :app ] || build_stack
275268 @lazy_initialized = true
276269 end
277270 end
@@ -323,16 +316,15 @@ def options?
323316
324317 private
325318
326- def build_stack ( helpers )
319+ def build_stack
327320 stack = Grape ::Middleware ::Stack . new
328321
329322 content_types = namespace_stackable_with_hash ( :content_types )
330323 format = namespace_inheritable ( :format )
331324
332325 stack . use Rack ::Head
333326 stack . use Rack ::Lint if lint?
334- stack . use Class . new ( Grape ::Middleware ::Error ) ,
335- helpers : helpers ,
327+ stack . use Grape ::Middleware ::Error ,
336328 format : format ,
337329 content_types : content_types ,
338330 default_status : namespace_inheritable ( :default_error_status ) ,
0 commit comments