@@ -14,11 +14,7 @@ class Endpoint
14
14
15
15
class << self
16
16
def new ( *args , &block )
17
- if self == Endpoint
18
- Class . new ( Endpoint ) . new ( *args , &block )
19
- else
20
- super
21
- end
17
+ self == Endpoint ? Class . new ( Endpoint ) . new ( *args , &block ) : super
22
18
end
23
19
24
20
def before_each ( new_setup = false , &block )
@@ -33,9 +29,7 @@ def before_each(new_setup = false, &block)
33
29
34
30
def run_before_each ( endpoint )
35
31
superclass . run_before_each ( endpoint ) unless self == Endpoint
36
- before_each . each do |blk |
37
- blk . call ( endpoint ) if blk . respond_to? :call
38
- end
32
+ before_each . each { |blk | blk . call ( endpoint ) if blk . respond_to? ( :call ) }
39
33
end
40
34
41
35
# @api private
@@ -68,6 +62,18 @@ def generate_api_method(method_name, &block)
68
62
end
69
63
end
70
64
65
+ # Create a new endpoint.
66
+ # @param new_settings [InheritableSetting] settings to determine the params,
67
+ # validations, and other properties from.
68
+ # @param options [Hash] attributes of this endpoint
69
+ # @option options path [String or Array] the path to this endpoint, within
70
+ # the current scope.
71
+ # @option options method [String or Array] which HTTP method(s) can be used
72
+ # to reach this endpoint.
73
+ # @option options route_options [Hash]
74
+ # @note This happens at the time of API definition, so in this context the
75
+ # endpoint does not know if it will be mounted under a different endpoint.
76
+ # @yield a block defining what your API should do when this endpoint is hit
71
77
def initialize ( new_settings , options = { } , &block )
72
78
require_option ( options , :path )
73
79
require_option ( options , :method )
@@ -96,6 +102,20 @@ def initialize(new_settings, options = {}, &block)
96
102
@block = self . class . generate_api_method ( method_name , &block )
97
103
end
98
104
105
+ # Update our settings from a given set of stackable parameters. Used when
106
+ # the endpoint's API is mounted under another one.
107
+ def inherit_settings ( namespace_stackable )
108
+ inheritable_setting . route [ :saved_validations ] += namespace_stackable [ :validations ]
109
+ parent_declared_params = namespace_stackable [ :declared_params ]
110
+
111
+ if parent_declared_params
112
+ inheritable_setting . route [ :declared_params ] ||= [ ]
113
+ inheritable_setting . route [ :declared_params ] . concat ( parent_declared_params . flatten )
114
+ end
115
+
116
+ endpoints && endpoints . each { |e | e . inherit_settings ( namespace_stackable ) }
117
+ end
118
+
99
119
def require_option ( options , key )
100
120
raise Grape ::Exceptions ::MissingOption . new ( key ) unless options . key? ( key )
101
121
end
@@ -284,9 +304,7 @@ def build_stack(helpers)
284
304
285
305
def build_helpers
286
306
helpers = namespace_stackable ( :helpers ) || [ ]
287
- Module . new do
288
- helpers . each { |mod_to_include | include mod_to_include }
289
- end
307
+ Module . new { helpers . each { |mod_to_include | include mod_to_include } }
290
308
end
291
309
292
310
private :build_stack , :build_helpers
@@ -301,9 +319,7 @@ def lazy_initialize!
301
319
@lazy_initialize_lock . synchronize do
302
320
return true if @lazy_initialized
303
321
304
- @helpers = build_helpers . tap do |mod |
305
- self . class . send ( :include , mod )
306
- end
322
+ @helpers = build_helpers . tap { |mod | self . class . send ( :include , mod ) }
307
323
@app = options [ :app ] || build_stack ( @helpers )
308
324
309
325
@lazy_initialized = true
0 commit comments