@@ -37,33 +37,6 @@ def run_before_each(endpoint)
37
37
superclass . run_before_each ( endpoint ) unless self == Endpoint
38
38
before_each . each { |blk | blk . try ( :call , endpoint ) }
39
39
end
40
-
41
- # @api private
42
- #
43
- # Create an UnboundMethod that is appropriate for executing an endpoint
44
- # route.
45
- #
46
- # The unbound method allows explicit calls to +return+ without raising a
47
- # +LocalJumpError+. The method will be removed, but a +Proc+ reference to
48
- # it will be returned. The returned +Proc+ expects a single argument: the
49
- # instance of +Endpoint+ to bind to the method during the call.
50
- #
51
- # @param [String, Symbol] method_name
52
- # @return [Proc]
53
- # @raise [NameError] an instance method with the same name already exists
54
- def generate_api_method ( method_name , &block )
55
- raise NameError . new ( "method #{ method_name . inspect } already exists and cannot be used as an unbound method name" ) if method_defined? ( method_name )
56
-
57
- define_method ( method_name , &block )
58
- method = instance_method ( method_name )
59
- remove_method ( method_name )
60
-
61
- proc do |endpoint_instance |
62
- ActiveSupport ::Notifications . instrument ( 'endpoint_render.grape' , endpoint : endpoint_instance ) do
63
- method . bind_call ( endpoint_instance )
64
- end
65
- end
66
- end
67
40
end
68
41
69
42
# Create a new endpoint.
@@ -108,12 +81,18 @@ def initialize(new_settings, options = {}, &block)
108
81
@status = nil
109
82
@stream = nil
110
83
@body = nil
111
- @proc = nil
112
84
113
85
return unless block
114
86
115
87
@source = block
116
- @block = self . class . generate_api_method ( method_name , &block )
88
+ @block = @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
94
+ end
95
+ end
117
96
end
118
97
119
98
# Update our settings from a given set of stackable parameters. Used when
@@ -131,14 +110,6 @@ def require_option(options, key)
131
110
raise Grape ::Exceptions ::MissingOption . new ( key ) unless options . key? ( key )
132
111
end
133
112
134
- def method_name
135
- [ options [ :method ] ,
136
- Namespace . joined_space ( namespace_stackable ( :namespace ) ) ,
137
- ( namespace_stackable ( :mount_path ) || [ ] ) . join ( '/' ) ,
138
- options [ :path ] . join ( '/' ) ]
139
- . join ( ' ' )
140
- end
141
-
142
113
def routes
143
114
@routes ||= endpoints &.collect ( &:routes ) &.flatten || to_routes
144
115
end
0 commit comments