Skip to content

Commit 8c33c13

Browse files
committed
Remove namespace_reverse_stackable from public DSL interface
- Remove namespace_reverse_stackable method from Grape::DSL::Settings - Replace direct calls to namespace_reverse_stackable with inheritable_setting.namespace_reverse_stackable - Update rescue_from implementation to use direct inheritable_setting access - Update tests to verify actual behavior instead of mocking internal methods - Simplify test setup by extending Grape::DSL::Settings instead of manually stubbing methods
1 parent d2d7798 commit 8c33c13

5 files changed

Lines changed: 11 additions & 43 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* [#2599](https://github.com/ruby-grape/grape/pull/2599): Simplify settings DSL get_or_set method and optimize logger implementation - [@ericproulx](https://github.com/ericproulx).
1616
* [#2600](https://github.com/ruby-grape/grape/pull/2600): Refactor versioner middleware: simplify base class and improve consistency - [@ericproulx](https://github.com/ericproulx).
1717
* [#2601](https://github.com/ruby-grape/grape/pull/2601): Refactor route_setting internal usage to use inheritable_setting.route for improved consistency and performance - [@ericproulx](https://github.com/ericproulx).
18+
* [#2602](https://github.com/ruby-grape/grape/pull/2602): Remove `namespace_reverse_stackable` from public DSL interface and use direct inheritable_setting access - [@ericproulx](https://github.com/ericproulx).
1819
* Your contribution here.
1920

2021
#### Fixes

lib/grape/dsl/desc.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def desc(description, options = {}, &config_block)
5757
else
5858
options.merge(description: description)
5959
end
60-
namespace_setting :description, settings
60+
inheritable_setting.namespace[:description] = settings
6161
inheritable_setting.route[:description] = settings
6262
end
6363
end

lib/grape/dsl/request_response.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def rescue_from(*args, &block)
117117
:base_only_rescue_handlers
118118
end
119119

120-
namespace_reverse_stackable(handler_type, args.to_h { |arg| [arg, handler] })
120+
inheritable_setting.namespace_reverse_stackable[handler_type] = args.to_h { |arg| [arg, handler] }
121121
end
122122

123123
namespace_stackable(:rescue_options, options)

lib/grape/dsl/settings.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ def namespace_setting(key, value = nil)
4646
get_or_set(inheritable_setting.namespace, key, value)
4747
end
4848

49-
def namespace_reverse_stackable(key, value = nil)
50-
get_or_set(inheritable_setting.namespace_reverse_stackable, key, value)
51-
end
52-
5349
def namespace_stackable_with_hash(key)
5450
settings = namespace_stackable(key)
5551
return if settings.blank?

spec/grape/dsl/request_response_spec.rb

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,7 @@
66
let(:dummy_class) do
77
Class.new do
88
extend Grape::DSL::RequestResponse
9-
10-
def self.namespace_stackable_with_hash(_key)
11-
{}
12-
end
13-
14-
def self.namespace_stackable(key, value = nil)
15-
if value
16-
namespace_stackable_hash[key] << value
17-
else
18-
namespace_stackable_hash[key]
19-
end
20-
end
21-
22-
def self.namespace_inheritable(key, value = nil)
23-
if value
24-
namespace_inheritable_hash[key] = value
25-
else
26-
namespace_inheritable_hash[key]
27-
end
28-
end
29-
30-
def self.namespace_stackable_hash
31-
@namespace_stackable_hash ||= Hash.new do |hash, key|
32-
hash[key] = []
33-
end
34-
end
35-
36-
def self.namespace_inheritable_hash
37-
@namespace_inheritable_hash ||= {}
38-
end
9+
extend Grape::DSL::Settings
3910
end
4011
end
4112

@@ -147,7 +118,7 @@ def self.namespace_inheritable_hash
147118
it 'sets a rescue handler declared through :with option' do
148119
with_block = -> { 'hello' }
149120
expect(subject).to receive(:namespace_inheritable).with(:rescue_all, true)
150-
expect(subject).to receive(:namespace_inheritable).with(:all_rescue_handler, an_instance_of(Proc))
121+
expect(subject).to receive(:namespace_inheritable).with(:all_rescue_handler, with_block)
151122
subject.rescue_from :all, with: with_block
152123
end
153124

@@ -192,43 +163,43 @@ def self.namespace_inheritable_hash
192163
with_block = -> { 'hello' }
193164
expect(subject).to receive(:namespace_inheritable).with(:rescue_all, true)
194165
expect(subject).to receive(:namespace_inheritable).with(:rescue_grape_exceptions, true)
195-
expect(subject).to receive(:namespace_inheritable).with(:grape_exceptions_rescue_handler, an_instance_of(Proc))
166+
expect(subject).to receive(:namespace_inheritable).with(:grape_exceptions_rescue_handler, with_block)
196167
subject.rescue_from :grape_exceptions, with: with_block
197168
end
198169
end
199170

200171
describe 'list of exceptions is passed' do
201172
it 'sets hash of exceptions as rescue handlers' do
202-
expect(subject).to receive(:namespace_reverse_stackable).with(:rescue_handlers, { StandardError => nil })
203173
expect(subject).to receive(:namespace_stackable).with(:rescue_options, {})
204174
subject.rescue_from StandardError
175+
expect(subject.inheritable_setting.namespace_reverse_stackable[:rescue_handlers]).to eq([StandardError => nil])
205176
end
206177

207178
it 'rescues only base handlers if rescue_subclasses: false option is passed' do
208-
expect(subject).to receive(:namespace_reverse_stackable).with(:base_only_rescue_handlers, { StandardError => nil })
209179
expect(subject).to receive(:namespace_stackable).with(:rescue_options, { rescue_subclasses: false })
210180
subject.rescue_from StandardError, rescue_subclasses: false
181+
expect(subject.inheritable_setting.namespace_reverse_stackable[:base_only_rescue_handlers]).to eq([StandardError => nil])
211182
end
212183

213184
it 'sets given proc as rescue handler for each key in hash' do
214185
rescue_handler_proc = proc {}
215-
expect(subject).to receive(:namespace_reverse_stackable).with(:rescue_handlers, { StandardError => rescue_handler_proc })
216186
expect(subject).to receive(:namespace_stackable).with(:rescue_options, {})
217187
subject.rescue_from StandardError, rescue_handler_proc
188+
expect(subject.inheritable_setting.namespace_reverse_stackable[:rescue_handlers]).to eq([StandardError => rescue_handler_proc])
218189
end
219190

220191
it 'sets given block as rescue handler for each key in hash' do
221192
rescue_handler_proc = proc {}
222-
expect(subject).to receive(:namespace_reverse_stackable).with(:rescue_handlers, { StandardError => rescue_handler_proc })
223193
expect(subject).to receive(:namespace_stackable).with(:rescue_options, {})
224194
subject.rescue_from StandardError, &rescue_handler_proc
195+
expect(subject.inheritable_setting.namespace_reverse_stackable[:rescue_handlers]).to eq([StandardError => rescue_handler_proc])
225196
end
226197

227198
it 'sets a rescue handler declared through :with option for each key in hash' do
228199
with_block = -> { 'hello' }
229-
expect(subject).to receive(:namespace_reverse_stackable).with(:rescue_handlers, { StandardError => an_instance_of(Proc) })
230200
expect(subject).to receive(:namespace_stackable).with(:rescue_options, {})
231201
subject.rescue_from StandardError, with: with_block
202+
expect(subject.inheritable_setting.namespace_reverse_stackable[:rescue_handlers]).to eq([StandardError => with_block])
232203
end
233204
end
234205
end

0 commit comments

Comments
 (0)