Skip to content

Commit e35d292

Browse files
authored
Merge pull request #2598 from ruby-grape/remove_settings_unset_functions
Remove Settings Unset Functions
2 parents f35caa0 + c947351 commit e35d292

7 files changed

Lines changed: 24 additions & 52 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* [#2582](https://github.com/ruby-grape/grape/pull/2582): Fix leaky slash when normalizing - [@ericproulx](https://github.com/ericproulx).
1212
* [#2583](https://github.com/ruby-grape/grape/pull/2583): Optimize api parameter documentation and memory usage - [@ericproulx](https://github.com/ericproulx).
1313
* [#2589](https://github.com/ruby-grape/grape/pull/2589): Replace `send` by `__send__` in codebase - [@ericproulx](https://github.com/ericproulx).
14+
* [#2598](https://github.com/ruby-grape/grape/pull/2598): Refactor settings DSL to use explicit methods instead of dynamic generation - [@ericproulx](https://github.com/ericproulx).
1415
* Your contribution here.
1516

1617
#### Fixes

lib/grape/api/instance.rb

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -232,22 +232,19 @@ def collect_route_config_per_pattern(all_routes)
232232
end
233233
end
234234

235+
ROOT_PREFIX_VERSIONING_KEY = %i[version version_options root_prefix].freeze
236+
private_constant :ROOT_PREFIX_VERSIONING_KEY
237+
235238
# Allows definition of endpoints that ignore the versioning configuration
236239
# used by the rest of your API.
237240
def without_root_prefix_and_versioning
238-
old_version = self.class.namespace_inheritable(:version)
239-
old_version_options = self.class.namespace_inheritable(:version_options)
240-
old_root_prefix = self.class.namespace_inheritable(:root_prefix)
241-
242-
self.class.namespace_inheritable_to_nil(:version)
243-
self.class.namespace_inheritable_to_nil(:version_options)
244-
self.class.namespace_inheritable_to_nil(:root_prefix)
245-
241+
inheritable_setting = self.class.inheritable_setting
242+
deleted_values = inheritable_setting.namespace_inheritable.delete(*ROOT_PREFIX_VERSIONING_KEY)
246243
yield
247-
248-
self.class.namespace_inheritable(:version, old_version)
249-
self.class.namespace_inheritable(:version_options, old_version_options)
250-
self.class.namespace_inheritable(:root_prefix, old_root_prefix)
244+
ensure
245+
ROOT_PREFIX_VERSIONING_KEY.each_with_index do |key, index|
246+
inheritable_setting.namespace_inheritable[key] = deleted_values[index]
247+
end
251248
end
252249
end
253250
end

lib/grape/dsl/settings.rb

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ def inheritable_setting
2626
@inheritable_setting ||= Grape::Util::InheritableSetting.new.tap { |new_settings| new_settings.inherit_from top_level_setting }
2727
end
2828

29-
# @param type [Symbol]
30-
# @param key [Symbol]
31-
def unset(type, key)
32-
setting = inheritable_setting.__send__(type)
33-
setting.delete key
34-
end
35-
3629
# @param type [Symbol]
3730
# @param key [Symbol]
3831
# @param value [Object] will be stored if the value is currently empty
@@ -56,12 +49,6 @@ def get_or_set(type, key, value)
5649
end
5750
end
5851

59-
def unset_namespace_stackable(*keys)
60-
keys.each do |key|
61-
unset :namespace_stackable, key
62-
end
63-
end
64-
6552
# defines the following methods:
6653
# - global_setting
6754
# - route_setting
@@ -73,11 +60,6 @@ def unset_namespace_stackable(*keys)
7360
end
7461
end
7562

76-
# @param key [Symbol]
77-
def namespace_inheritable_to_nil(key)
78-
inheritable_setting.namespace_inheritable[key] = nil
79-
end
80-
8163
def namespace_reverse_stackable(key, value = nil)
8264
get_or_set :namespace_reverse_stackable, key, value
8365
end

lib/grape/dsl/validations.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module Validations
2222
# # whatever
2323
# end
2424
def reset_validations!
25-
unset_namespace_stackable :declared_params, :params, :validations
25+
inheritable_setting.namespace_stackable.delete(:declared_params, :params, :validations)
2626
end
2727

2828
# Opens a root-level ParamsScope, defining parameter coercions and

lib/grape/util/base_inheritable.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ def initialize(inherited_values = nil)
1414
@new_values = {}
1515
end
1616

17-
def delete(key)
18-
new_values.delete key
17+
def delete(*keys)
18+
keys.map do |key|
19+
# since delete returns the deleted value, seems natural to `map` the result
20+
new_values.delete key
21+
end
1922
end
2023

2124
def initialize_copy(other)

spec/grape/dsl/settings_spec.rb

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ def reset_validations!; end
1515
end
1616
end
1717

18-
describe '#unset' do
19-
it 'deletes a key from settings' do
20-
subject.namespace_setting :dummy, 1
21-
expect(subject.inheritable_setting.namespace.keys).to include(:dummy)
22-
23-
subject.unset :namespace, :dummy
24-
expect(subject.inheritable_setting.namespace.keys).not_to include(:dummy)
25-
end
26-
end
27-
2818
describe '#get_or_set' do
2919
it 'sets a values' do
3020
subject.get_or_set :namespace, :dummy, 1
@@ -126,13 +116,6 @@ def reset_validations!; end
126116
end
127117
end
128118

129-
describe '#unset_namespace_stackable' do
130-
it 'delegates to unset' do
131-
expect(subject).to receive(:unset).with(:namespace_stackable, :dummy)
132-
subject.unset_namespace_stackable(:dummy)
133-
end
134-
end
135-
136119
describe 'complex scenario' do
137120
it 'plays well' do
138121
obj1 = dummy_class.new

spec/grape/dsl/validations_spec.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,23 @@
55

66
let(:dummy_class) do
77
Class.new do
8+
extend Grape::DSL::Settings
89
extend Grape::DSL::Validations
9-
def self.unset_namespace_stackable(*_keys); end
1010
end
1111
end
1212

1313
describe '.reset_validations' do
1414
subject { dummy_class.reset_validations! }
1515

16+
before do
17+
%i[declared_params params validations other].each do |key|
18+
dummy_class.inheritable_setting.namespace_stackable[key] = key
19+
end
20+
end
21+
1622
it 'calls unset_namespace_stackable properly' do
17-
expect(dummy_class).to receive(:unset_namespace_stackable).with(:declared_params, :params, :validations)
1823
subject
24+
expect(dummy_class.inheritable_setting.namespace_stackable.to_hash).to eq(other: [:other])
1925
end
2026
end
2127

0 commit comments

Comments
 (0)