Skip to content

Commit d2d7798

Browse files
authored
Merge pull request #2601 from ruby-grape/refactor_route_setting_internal_usage
Refactor route_setting internal usage to use inheritable_setting.route
2 parents dd4fa3a + a8b9a8c commit d2d7798

File tree

8 files changed

+19
-37
lines changed

8 files changed

+19
-37
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* [#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).
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).
17+
* [#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).
1718
* Your contribution here.
1819

1920
#### Fixes

lib/grape/dsl/desc.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def desc(description, options = {}, &config_block)
5858
options.merge(description: description)
5959
end
6060
namespace_setting :description, settings
61-
route_setting :description, settings
61+
inheritable_setting.route[:description] = settings
6262
end
6363
end
6464
end

lib/grape/dsl/inside_route.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def declared_hash(passed_params, options, declared_params, params_nested_path)
5555
end
5656

5757
def declared_hash_attr(passed_params, options, declared_param, params_nested_path, memo)
58-
renamed_params = route_setting(:renamed_params) || {}
58+
renamed_params = inheritable_setting.route[:renamed_params] || {}
5959
if declared_param.is_a?(Hash)
6060
declared_param.each_pair do |declared_parent_param, declared_children_params|
6161
params_nested_path_dup = params_nested_path.dup
@@ -119,7 +119,7 @@ def optioned_param_key(declared_param, options)
119119
def optioned_declared_params(include_parent_namespaces)
120120
declared_params = if include_parent_namespaces
121121
# Declared params including parent namespaces
122-
route_setting(:declared_params)
122+
inheritable_setting.route[:declared_params]
123123
else
124124
# Declared params at current namespace
125125
namespace_stackable(:declared_params).last || []

lib/grape/dsl/routing.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,15 @@ def mount(mounts, *opts)
141141
# end
142142
# end
143143
def route(methods, paths = ['/'], route_options = {}, &block)
144-
methods = '*' if methods == :any
144+
method = methods == :any ? '*' : methods
145+
description = inheritable_setting.route[:description] || {}
145146
endpoint_options = {
146-
method: methods,
147+
method: method,
147148
path: paths,
148149
for: self,
149150
route_options: {
150151
params: namespace_stackable_with_hash(:params) || {}
151-
}.deep_merge(route_setting(:description) || {}).deep_merge(route_options || {})
152+
}.deep_merge(description).deep_merge(route_options || {})
152153
}
153154

154155
new_endpoint = Grape::Endpoint.new(inheritable_setting, endpoint_options, &block)

lib/grape/endpoint.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ def initialize(new_settings, options = {}, &block)
5555
# now +namespace_stackable(:declared_params)+ contains all params defined for
5656
# this endpoint and its parents, but later it will be cleaned up,
5757
# see +reset_validations!+ in lib/grape/dsl/validations.rb
58-
route_setting(:declared_params, namespace_stackable(:declared_params).flatten)
59-
route_setting(:saved_validations, namespace_stackable(:validations))
58+
inheritable_setting.route[:declared_params] = namespace_stackable(:declared_params).flatten
59+
inheritable_setting.route[:saved_validations] = namespace_stackable(:validations)
6060

6161
namespace_stackable(:representations, []) unless namespace_stackable(:representations)
6262
namespace_inheritable(:default_error_status, 500) unless namespace_inheritable(:default_error_status)
@@ -302,9 +302,11 @@ def run_filters(filters, type = :other)
302302
end
303303

304304
def validations
305+
saved_validations = inheritable_setting.route[:saved_validations]
306+
return if saved_validations.nil?
305307
return enum_for(:validations) unless block_given?
306308

307-
route_setting(:saved_validations)&.each do |saved_validation|
309+
saved_validations.each do |saved_validation|
308310
yield Grape::Validations::ValidatorFactory.create_validator(saved_validation)
309311
end
310312
end

lib/grape/validations/params_scope.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,10 @@ def full_path
211211
# @param new_name [String, Symbol] the new name of the parameter (the
212212
# renamed name, with the +as: ...+ semantic)
213213
def push_renamed_param(path, new_name)
214-
base = @api.route_setting(:renamed_params) || {}
214+
api_route_setting = @api.inheritable_setting.route
215+
base = api_route_setting[:renamed_params] || {}
215216
base[Array(path).map(&:to_s)] = new_name.to_s
216-
@api.route_setting(:renamed_params, base)
217+
api_route_setting[:renamed_params] = base
217218
end
218219

219220
def require_required_and_optional_fields(context, opts)

spec/grape/dsl/desc_spec.rb

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,7 @@
66
let(:dummy_class) do
77
Class.new do
88
extend Grape::DSL::Desc
9-
10-
def self.namespace_setting(key, value = nil)
11-
if value
12-
namespace_setting_hash[key] = value
13-
else
14-
namespace_setting_hash[key]
15-
end
16-
end
17-
18-
def self.route_setting(key, value = nil)
19-
if value
20-
route_setting_hash[key] = value
21-
else
22-
route_setting_hash[key]
23-
end
24-
end
25-
26-
def self.namespace_setting_hash
27-
@namespace_setting_hash ||= {}
28-
end
29-
30-
def self.route_setting_hash
31-
@route_setting_hash ||= {}
32-
end
9+
extend Grape::DSL::Settings
3310
end
3411
end
3512

spec/grape/dsl/routing_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@
123123
end
124124

125125
it 'generates correct endpoint options' do
126-
allow(subject).to receive(:route_setting).with(:description).and_return(fiz: 'baz')
127-
allow(subject).to receive(:namespace_stackable_with_hash).and_return(nuz: 'naz')
126+
subject.inheritable_setting.route[:description] = { fiz: 'baz' }
127+
subject.inheritable_setting.namespace_stackable[:params] = { nuz: 'naz' }
128128

129129
expect(Grape::Endpoint).to receive(:new) do |_inheritable_setting, endpoint_options|
130130
expect(endpoint_options[:method]).to eq :get

0 commit comments

Comments
 (0)