@@ -14,11 +14,7 @@ class Endpoint
1414
1515 class << self
1616 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
2218 end
2319
2420 def before_each ( new_setup = false , &block )
@@ -33,9 +29,7 @@ def before_each(new_setup = false, &block)
3329
3430 def run_before_each ( endpoint )
3531 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 ) }
3933 end
4034
4135 # @api private
@@ -68,6 +62,18 @@ def generate_api_method(method_name, &block)
6862 end
6963 end
7064
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
7177 def initialize ( new_settings , options = { } , &block )
7278 require_option ( options , :path )
7379 require_option ( options , :method )
@@ -96,6 +102,20 @@ def initialize(new_settings, options = {}, &block)
96102 @block = self . class . generate_api_method ( method_name , &block )
97103 end
98104
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+
99119 def require_option ( options , key )
100120 raise Grape ::Exceptions ::MissingOption . new ( key ) unless options . key? ( key )
101121 end
@@ -284,9 +304,7 @@ def build_stack(helpers)
284304
285305 def build_helpers
286306 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 } }
290308 end
291309
292310 private :build_stack , :build_helpers
@@ -301,9 +319,7 @@ def lazy_initialize!
301319 @lazy_initialize_lock . synchronize do
302320 return true if @lazy_initialized
303321
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 ) }
307323 @app = options [ :app ] || build_stack ( @helpers )
308324
309325 @lazy_initialized = true
0 commit comments