Skip to content

Commit 5340722

Browse files
committed
within_namespace is part on our internal api and should be exposed publicly. Also remove namespace_start and namespace_end
1 parent 7cfc817 commit 5340722

File tree

4 files changed

+66
-157
lines changed

4 files changed

+66
-157
lines changed

lib/grape/dsl/routing.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def route(methods, paths = ['/'], route_options = {}, &block)
154154
new_endpoint = Grape::Endpoint.new(inheritable_setting, endpoint_options, &block)
155155
endpoints << new_endpoint unless endpoints.any? { |e| e.equals?(new_endpoint) }
156156

157-
route_end
157+
inheritable_setting.route_end
158158
reset_validations!
159159
end
160160

lib/grape/dsl/settings.rb

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ module DSL
77
# matter where they're defined, and inheritable settings which apply only
88
# in the current scope and scopes nested under it.
99
module Settings
10-
extend Forwardable
11-
1210
attr_writer :inheritable_setting, :top_level_setting
1311

14-
def_delegators :inheritable_setting, :route_end
15-
1612
# Fetch our top-level settings, which apply to all endpoints in the API.
1713
def top_level_setting
1814
@top_level_setting ||= Grape::Util::InheritableSetting.new.tap do |setting|
@@ -102,27 +98,20 @@ def namespace_reverse_stackable_with_hash(key)
10298
end
10399
end
104100

105-
# Fork our inheritable settings to a new instance, copied from our
106-
# parent's, but separate so we won't modify it. Every call to this
107-
# method should have an answering call to #namespace_end.
108-
def namespace_start
109-
@inheritable_setting = Grape::Util::InheritableSetting.new.tap { |new_settings| new_settings.inherit_from inheritable_setting }
110-
end
111-
112-
# Set the inheritable settings pointer back up by one level.
113-
def namespace_end
114-
route_end
115-
@inheritable_setting = inheritable_setting.parent
116-
end
101+
private
117102

118103
# Execute the block within a context where our inheritable settings are forked
119104
# to a new copy (see #namespace_start).
120-
def within_namespace(&block)
121-
namespace_start
105+
def within_namespace
106+
new_inheritable_settings = Grape::Util::InheritableSetting.new
107+
new_inheritable_settings.inherit_from inheritable_setting
122108

123-
result = yield if block
109+
@inheritable_setting = new_inheritable_settings
124110

125-
namespace_end
111+
result = yield
112+
113+
inheritable_setting.route_end
114+
@inheritable_setting = inheritable_setting.parent
126115
reset_validations!
127116

128117
result

spec/grape/dsl/routing_spec.rb

Lines changed: 17 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,62 +6,8 @@
66
let(:dummy_class) do
77
Class.new do
88
extend Grape::DSL::Routing
9-
10-
def self.namespace_stackable(key, value = nil)
11-
if value
12-
namespace_stackable_hash[key] << value
13-
else
14-
namespace_stackable_hash[key]
15-
end
16-
end
17-
18-
def self.namespace_stackable_with_hash(key, value = nil)
19-
if value
20-
namespace_stackable_with_hash_hash[key] = value
21-
else
22-
namespace_stackable_with_hash_hash[key]
23-
end
24-
end
25-
26-
def self.namespace_inheritable(key, value = nil)
27-
if value
28-
namespace_inheritable_hash[key] = value
29-
else
30-
namespace_inheritable_hash[key]
31-
end
32-
end
33-
34-
def self.route_setting(key, value = nil)
35-
if value
36-
route_setting_hash[key] = value
37-
else
38-
route_setting_hash[key]
39-
end
40-
end
41-
42-
def self.inheritable_setting
43-
@inheritable_setting ||= Grape::Util::InheritableSetting.new
44-
end
45-
46-
def self.namespace_stackable_hash
47-
@namespace_stackable_hash ||= Hash.new do |hash, key|
48-
hash[key] = []
49-
end
50-
end
51-
52-
def self.namespace_stackable_with_hash_hash
53-
@namespace_stackable_with_hash_hash ||= Hash.new do |hash, key|
54-
hash[key] = []
55-
end
56-
end
57-
58-
def self.namespace_inheritable_hash
59-
@namespace_inheritable_hash ||= {}
60-
end
61-
62-
def self.route_setting_hash
63-
@route_setting_hash ||= {}
64-
end
9+
extend Grape::DSL::Settings
10+
extend Grape::DSL::Validations
6511
end
6612
end
6713

@@ -87,9 +33,20 @@ def self.route_setting_hash
8733
end
8834

8935
describe '.scope' do
36+
let(:root_app) do
37+
Class.new(Grape::API) do
38+
scope :my_scope do
39+
get :my_endpoint do
40+
return_no_content
41+
end
42+
end
43+
end
44+
end
45+
9046
it 'create a scope without affecting the URL' do
91-
expect(subject).to receive(:within_namespace)
92-
subject.scope {}
47+
env = Rack::MockRequest.env_for('/my_endpoint', method: Rack::GET)
48+
response = Rack::MockResponse[*root_app.call(env)]
49+
expect(response).to be_no_content
9350
end
9451
end
9552

@@ -140,12 +97,12 @@ def self.route_setting_hash
14097
describe '.route' do
14198
before do
14299
allow(subject).to receive(:endpoints).and_return([])
143-
allow(subject).to receive(:route_end)
100+
allow(subject.inheritable_setting).to receive(:route_end)
144101
allow(subject).to receive(:reset_validations!)
145102
end
146103

147104
it 'marks end of the route' do
148-
expect(subject).to receive(:route_end)
105+
expect(subject.inheritable_setting).to receive(:route_end)
149106
subject.route(:any)
150107
end
151108

@@ -233,7 +190,6 @@ def self.route_setting_hash
233190
let(:new_namespace) { Object.new }
234191

235192
it 'creates a new namespace with given name and options' do
236-
expect(subject).to receive(:within_namespace).and_yield
237193
expect(subject).to receive(:nest).and_yield
238194
expect(Grape::Namespace).to receive(:new).with(:foo, { foo: 'bar' }).and_return(new_namespace)
239195
expect(subject).to receive(:namespace_stackable).with(:namespace, new_namespace)

spec/grape/dsl/settings_spec.rb

Lines changed: 39 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
Class.new do
88
include Grape::DSL::Settings
99

10+
def with_namespace(&block)
11+
within_namespace(&block)
12+
end
13+
1014
def reset_validations!; end
1115
end
1216
end
@@ -50,7 +54,7 @@ def reset_validations!; end
5054
subject.route_setting :some_thing, :foo_bar
5155
expect(subject.route_setting(:some_thing)).to eq :foo_bar
5256

53-
subject.route_end
57+
subject.inheritable_setting.route_end
5458

5559
expect(subject.route_setting(:some_thing)).to be_nil
5660
end
@@ -63,31 +67,22 @@ def reset_validations!; end
6367
end
6468

6569
it 'sets a value until the end of a namespace' do
66-
subject.namespace_start
67-
68-
subject.namespace_setting :some_thing, :foo_bar
69-
expect(subject.namespace_setting(:some_thing)).to eq :foo_bar
70-
71-
subject.namespace_end
72-
70+
subject.with_namespace do
71+
subject.namespace_setting :some_thing, :foo_bar
72+
expect(subject.namespace_setting(:some_thing)).to eq :foo_bar
73+
end
7374
expect(subject.namespace_setting(:some_thing)).to be_nil
7475
end
7576

7677
it 'resets values after leaving nested namespaces' do
77-
subject.namespace_start
78-
79-
subject.namespace_setting :some_thing, :foo_bar
80-
expect(subject.namespace_setting(:some_thing)).to eq :foo_bar
81-
82-
subject.namespace_start
83-
84-
expect(subject.namespace_setting(:some_thing)).to be_nil
85-
86-
subject.namespace_end
87-
expect(subject.namespace_setting(:some_thing)).to eq :foo_bar
88-
89-
subject.namespace_end
90-
78+
subject.with_namespace do
79+
subject.namespace_setting :some_thing, :foo_bar
80+
expect(subject.namespace_setting(:some_thing)).to eq :foo_bar
81+
subject.with_namespace do
82+
expect(subject.namespace_setting(:some_thing)).to be_nil
83+
end
84+
expect(subject.namespace_setting(:some_thing)).to eq :foo_bar
85+
end
9186
expect(subject.namespace_setting(:some_thing)).to be_nil
9287
end
9388
end
@@ -99,22 +94,16 @@ def reset_validations!; end
9994
end
10095

10196
it 'inherits values from surrounding namespace' do
102-
subject.namespace_start
103-
104-
subject.namespace_inheritable(:some_thing, :foo_bar)
105-
expect(subject.namespace_inheritable(:some_thing)).to eq :foo_bar
106-
107-
subject.namespace_start
108-
109-
expect(subject.namespace_inheritable(:some_thing)).to eq :foo_bar
110-
111-
subject.namespace_inheritable(:some_thing, :foo_bar_2)
112-
113-
expect(subject.namespace_inheritable(:some_thing)).to eq :foo_bar_2
114-
115-
subject.namespace_end
116-
expect(subject.namespace_inheritable(:some_thing)).to eq :foo_bar
117-
subject.namespace_end
97+
subject.with_namespace do
98+
subject.namespace_inheritable(:some_thing, :foo_bar)
99+
expect(subject.namespace_inheritable(:some_thing)).to eq :foo_bar
100+
subject.with_namespace do
101+
expect(subject.namespace_inheritable(:some_thing)).to eq :foo_bar
102+
subject.namespace_inheritable(:some_thing, :foo_bar_2)
103+
expect(subject.namespace_inheritable(:some_thing)).to eq :foo_bar_2
104+
end
105+
expect(subject.namespace_inheritable(:some_thing)).to eq :foo_bar
106+
end
118107
end
119108
end
120109

@@ -125,22 +114,15 @@ def reset_validations!; end
125114
end
126115

127116
it 'stacks values from surrounding namespace' do
128-
subject.namespace_start
129-
130-
subject.namespace_stackable(:some_thing, :foo_bar)
131-
expect(subject.namespace_stackable(:some_thing)).to eq [:foo_bar]
132-
133-
subject.namespace_start
134-
135-
expect(subject.namespace_stackable(:some_thing)).to eq [:foo_bar]
136-
137-
subject.namespace_stackable(:some_thing, :foo_bar_2)
138-
139-
expect(subject.namespace_stackable(:some_thing)).to eq %i[foo_bar foo_bar_2]
140-
141-
subject.namespace_end
142-
expect(subject.namespace_stackable(:some_thing)).to eq [:foo_bar]
143-
subject.namespace_end
117+
subject.with_namespace do
118+
subject.namespace_stackable(:some_thing, :foo_bar)
119+
expect(subject.namespace_stackable(:some_thing)).to eq [:foo_bar]
120+
subject.with_namespace do
121+
subject.namespace_stackable(:some_thing, :foo_bar_2)
122+
expect(subject.namespace_stackable(:some_thing)).to eq %i[foo_bar foo_bar_2]
123+
end
124+
expect(subject.namespace_stackable(:some_thing)).to eq [:foo_bar]
125+
end
144126
end
145127
end
146128

@@ -151,24 +133,6 @@ def reset_validations!; end
151133
end
152134
end
153135

154-
describe '#within_namespace' do
155-
it 'calls start and end for a namespace' do
156-
expect(subject).to receive :namespace_start
157-
expect(subject).to receive :namespace_end
158-
159-
subject.within_namespace do
160-
end
161-
end
162-
163-
it 'returns the last result' do
164-
result = subject.within_namespace do
165-
1
166-
end
167-
168-
expect(result).to eq 1
169-
end
170-
end
171-
172136
describe 'complex scenario' do
173137
it 'plays well' do
174138
obj1 = dummy_class.new
@@ -179,7 +143,7 @@ def reset_validations!; end
179143
obj2_copy = nil
180144
obj3_copy = nil
181145

182-
obj1.within_namespace do
146+
obj1.with_namespace do
183147
obj1.namespace_stackable(:some_thing, :obj1)
184148
expect(obj1.namespace_stackable(:some_thing)).to eq [:obj1]
185149
obj1_copy = obj1.inheritable_setting.point_in_time_copy
@@ -188,7 +152,7 @@ def reset_validations!; end
188152
expect(obj1.namespace_stackable(:some_thing)).to eq []
189153
expect(obj1_copy.namespace_stackable[:some_thing]).to eq [:obj1]
190154

191-
obj2.within_namespace do
155+
obj2.with_namespace do
192156
obj2.namespace_stackable(:some_thing, :obj2)
193157
expect(obj2.namespace_stackable(:some_thing)).to eq [:obj2]
194158
obj2_copy = obj2.inheritable_setting.point_in_time_copy
@@ -197,7 +161,7 @@ def reset_validations!; end
197161
expect(obj2.namespace_stackable(:some_thing)).to eq []
198162
expect(obj2_copy.namespace_stackable[:some_thing]).to eq [:obj2]
199163

200-
obj3.within_namespace do
164+
obj3.with_namespace do
201165
obj3.namespace_stackable(:some_thing, :obj3)
202166
expect(obj3.namespace_stackable(:some_thing)).to eq [:obj3]
203167
obj3_copy = obj3.inheritable_setting.point_in_time_copy

0 commit comments

Comments
 (0)