@@ -3,23 +3,13 @@ module Grape
33  # creating Grape APIs.Users should subclass this 
44  # class in order to build an API. 
55  class  API 
6-     extend  Grape ::Middleware ::Auth ::DSL 
7- 
8-     include  Grape ::DSL ::Validations 
9-     include  Grape ::DSL ::Callbacks 
10-     include  Grape ::DSL ::Configuration 
11-     include  Grape ::DSL ::Helpers 
12-     include  Grape ::DSL ::Middleware 
13-     include  Grape ::DSL ::RequestResponse 
14-     include  Grape ::DSL ::Routing 
6+     include  Grape ::DSL ::API 
157
168    class  << self 
179      attr_reader  :instance 
18- 
1910      LOCK  =  Mutex . new 
2011
2112      def  reset! 
22-         @settings   =  Grape ::Util ::HashStack . new 
2313        @route_set  =  Rack ::Mount ::RouteSet . new 
2414        @endpoints  =  [ ] 
2515        @routes  =  nil 
@@ -47,34 +37,19 @@ def call!(env)
4737      # 
4838      # @param name [Symbol] Purely placebo, just allows to to name the scope to make the code more readable. 
4939      def  scope ( name  =  nil ,  &block ) 
50-         nest ( block ) 
40+         within_namespace  do 
41+           nest ( block ) 
42+         end 
5143      end 
5244
5345      def  cascade ( value  =  nil ) 
5446        if  value . nil? 
55-           settings . key ?( :cascade )  ? !!settings [ :cascade ]  : true 
47+           inheritable_setting . namespace_inheritable . keys . include ?( :cascade )  ? !!namespace_inheritable ( :cascade )  : true 
5648        else 
57-           set ( :cascade ,  value ) 
49+           namespace_inheritable ( :cascade ,  value ) 
5850        end 
5951      end 
6052
61-       # Set a configuration value for this namespace. 
62-       # 
63-       # @param key [Symbol] The key of the configuration variable. 
64-       # @param value [Object] The value to which to set the configuration variable. 
65-       def  set ( key ,  value ) 
66-         settings [ key . to_sym ]  =  value 
67-       end 
68- 
69-       # Add to a configuration value for this 
70-       # namespace. 
71-       # 
72-       # @param key [Symbol] The key of the configuration variable. 
73-       # @param value [Object] The value to which to set the configuration variable. 
74-       def  imbue ( key ,  value ) 
75-         settings . imbue ( key ,  value ) 
76-       end 
77- 
7853      protected 
7954
8055      def  prepare_routes 
@@ -91,10 +66,8 @@ def prepare_routes
9166      def  nest ( *blocks ,  &block ) 
9267        blocks . reject!  {  |b | b . nil?  } 
9368        if  blocks . any? 
94-           settings . push   # create a new context to eval the follow 
9569          instance_eval ( &block )  if  block_given? 
9670          blocks . each  {  |b | instance_eval ( &b )  } 
97-           settings . pop  # when finished, we pop the context 
9871          reset_validations! 
9972        else 
10073          instance_eval ( &block ) 
@@ -106,12 +79,14 @@ def inherited(subclass)
10679        subclass . logger  =  logger . clone 
10780      end 
10881
109-       def  inherit_settings ( other_stack ) 
110-         settings . prepend  other_stack 
82+       def  inherit_settings ( other_settings ) 
83+         top_level_setting . inherit_from  other_settings . point_in_time_copy 
84+ 
11185        endpoints . each  do  |e |
112-           e . settings . prepend ( other_stack ) 
113-           e . options [ :app ] . inherit_settings ( other_stack )  if  e . options [ :app ] . respond_to? ( :inherit_settings ,  true ) 
86+           e . reset_routes! 
11487        end 
88+ 
89+         @routes  =  nil 
11590      end 
11691    end 
11792
@@ -121,6 +96,7 @@ def initialize
12196      self . class . endpoints . each  do  |endpoint |
12297        endpoint . mount_in ( @route_set ) 
12398      end 
99+ 
124100      @route_set . freeze 
125101    end 
126102
@@ -139,8 +115,8 @@ def call(env)
139115    # errors from reaching upstream. This is effectivelly done by unsetting 
140116    # X-Cascade. Default :cascade is true. 
141117    def  cascade? 
142-       return  !!self . class . settings [ :cascade ]  if  self . class . settings . key ?( :cascade ) 
143-       return  !!self . class . settings [ :version_options ] [ :cascade ]  if  self . class . settings [ :version_options ]  && self . class . settings [ :version_options ] . key? ( :cascade ) 
118+       return  !!self . class . namespace_inheritable ( :cascade )  if  self . class . inheritable_setting . namespace_inheritable . keys . include ?( :cascade ) 
119+       return  !!self . class . namespace_inheritable ( :version_options ) [ :cascade ]  if  self . class . namespace_inheritable ( :version_options )  && self . class . namespace_inheritable ( :version_options ) . key? ( :cascade ) 
144120      true 
145121    end 
146122
@@ -154,6 +130,7 @@ def cascade?
154130    # cannot handle. 
155131    def  add_head_not_allowed_methods_and_options_methods 
156132      methods_per_path  =  { } 
133+ 
157134      self . class . endpoints . each  do  |endpoint |
158135        routes  =  endpoint . routes 
159136        routes . each  do  |route |
@@ -169,13 +146,14 @@ def add_head_not_allowed_methods_and_options_methods
169146      without_versioning  do 
170147        methods_per_path . each  do  |path ,  methods |
171148          allowed_methods  =  methods . dup 
172-           unless  self . class . settings [ :do_not_route_head ] 
149+           unless  self . class . namespace_inheritable ( :do_not_route_head ) 
173150            allowed_methods  |= [ 'HEAD' ]  if  allowed_methods . include? ( 'GET' ) 
174151          end 
175152
176153          allow_header  =  ( [ 'OPTIONS' ]  | allowed_methods ) . join ( ', ' ) 
177-           unless  self . class . settings [ :do_not_route_options ] 
154+           unless  self . class . namespace_inheritable ( :do_not_route_options ) 
178155            unless  allowed_methods . include? ( 'OPTIONS' ) 
156+               # require 'pry-byebug'; binding.pry 
179157              self . class . options ( path ,  { } )  do 
180158                header  'Allow' ,  allow_header 
181159                status  204 
@@ -185,7 +163,7 @@ def add_head_not_allowed_methods_and_options_methods
185163          end 
186164
187165          not_allowed_methods  =  %w( GET  PUT  POST  DELETE  PATCH  HEAD )  - allowed_methods 
188-           not_allowed_methods  << 'OPTIONS'  if  self . class . settings [ :do_not_route_options ] 
166+           not_allowed_methods  << 'OPTIONS'  if  self . class . namespace_inheritable ( :do_not_route_options ) 
189167          self . class . route ( not_allowed_methods ,  path )  do 
190168            header  'Allow' ,  allow_header 
191169            status  405 
@@ -196,9 +174,16 @@ def add_head_not_allowed_methods_and_options_methods
196174    end 
197175
198176    def  without_versioning ( &block ) 
199-       self . class . settings . push ( version : nil ,  version_options : nil ) 
177+       old_version  =  self . class . namespace_inheritable ( :version ) 
178+       old_version_options  =  self . class . namespace_inheritable ( :version_options ) 
179+ 
180+       self . class . namespace_inheritable_to_nil ( :version ) 
181+       self . class . namespace_inheritable_to_nil ( :version_options ) 
182+ 
200183      yield 
201-       self . class . settings . pop 
184+ 
185+       self . class . namespace_inheritable ( :version ,  old_version ) 
186+       self . class . namespace_inheritable ( :version_options ,  old_version_options ) 
202187    end 
203188  end 
204189end 
0 commit comments