Skip to content

Commit 3e14d24

Browse files
committed
Remove namespace_stackable_with_hash from public interface
- Move namespace_stackable_with_hash method from DSL::Settings to InheritableSetting - Update all internal usage to use inheritable_setting.namespace_stackable_with_hash - Remove public method from DSL::Settings module - Update tests to reflect the new internal structure - Increase RuboCop AbcSize limit to accommodate refactored code
1 parent 8c33c13 commit 3e14d24

12 files changed

Lines changed: 47 additions & 78 deletions

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Style/Send:
3535
Enabled: true
3636

3737
Metrics/AbcSize:
38-
Max: 45
38+
Max: 50
3939

4040
Metrics/BlockLength:
4141
Max: 30

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
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).
1818
* [#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).
19+
* [#2603](https://github.com/ruby-grape/grape/pull/2603): Remove `namespace_stackable_with_hash` from public interface and move to internal InheritableSetting - [@ericproulx](https://github.com/ericproulx).
1920
* Your contribution here.
2021

2122
#### Fixes

lib/grape/dsl/inside_route.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -389,22 +389,22 @@ def route
389389
# @return [Class] the located Entity class, or nil if none is found
390390
def entity_class_for_obj(object, options)
391391
entity_class = options.delete(:with)
392-
393-
if entity_class.nil?
394-
# entity class not explicitly defined, auto-detect from relation#klass or first object in the collection
395-
object_class = if object.respond_to?(:klass)
396-
object.klass
397-
else
398-
object.respond_to?(:first) ? object.first.class : object.class
399-
end
400-
401-
object_class.ancestors.each do |potential|
402-
entity_class ||= (namespace_stackable_with_hash(:representations) || {})[potential]
403-
end
404-
405-
entity_class ||= object_class.const_get(:Entity) if object_class.const_defined?(:Entity) && object_class.const_get(:Entity).respond_to?(:represent)
392+
return entity_class if entity_class
393+
394+
# entity class not explicitly defined, auto-detect from relation#klass or first object in the collection
395+
object_class = if object.respond_to?(:klass)
396+
object.klass
397+
else
398+
object.respond_to?(:first) ? object.first.class : object.class
399+
end
400+
401+
representations = inheritable_setting.namespace_stackable_with_hash(:representations)
402+
if representations
403+
potential = object_class.ancestors.detect { |potential| representations.key?(potential) }
404+
entity_class = representations[potential] if potential
406405
end
407406

407+
entity_class = object_class.const_get(:Entity) if !entity_class && object_class.const_defined?(:Entity) && object_class.const_get(:Entity).respond_to?(:represent)
408408
entity_class
409409
end
410410

lib/grape/dsl/parameters.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def build_with(build_with)
5454
# end
5555
# end
5656
def use(*names)
57-
named_params = @api.namespace_stackable_with_hash(:named_params) || {}
57+
named_params = @api.inheritable_setting.namespace_stackable_with_hash(:named_params) || {}
5858
options = names.extract_options!
5959
names.each do |name|
6060
params_block = named_params.fetch(name) do

lib/grape/dsl/request_response.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def content_type(key, val)
6060

6161
# All available content types.
6262
def content_types
63-
c_types = namespace_stackable_with_hash(:content_types)
63+
c_types = inheritable_setting.namespace_stackable_with_hash(:content_types)
6464
Grape::ContentTypes.content_types_for c_types
6565
end
6666

lib/grape/dsl/routing.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,17 @@ def mount(mounts, *opts)
142142
# end
143143
def route(methods, paths = ['/'], route_options = {}, &block)
144144
method = methods == :any ? '*' : methods
145-
description = inheritable_setting.route[:description] || {}
145+
endpoint_params = inheritable_setting.namespace_stackable_with_hash(:params) || {}
146+
endpoint_description = inheritable_setting.route[:description]
147+
all_route_options = { params: endpoint_params }
148+
all_route_options.deep_merge!(endpoint_description) if endpoint_description
149+
all_route_options.deep_merge!(route_options) if route_options&.any?
150+
146151
endpoint_options = {
147152
method: method,
148153
path: paths,
149154
for: self,
150-
route_options: {
151-
params: namespace_stackable_with_hash(:params) || {}
152-
}.deep_merge(description).deep_merge(route_options || {})
155+
route_options: all_route_options
153156
}
154157

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

lib/grape/dsl/settings.rb

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

49-
def namespace_stackable_with_hash(key)
50-
settings = namespace_stackable(key)
51-
return if settings.blank?
52-
53-
settings.each_with_object({}) { |value, result| result.deep_merge!(value) }
54-
end
55-
5649
private
5750

5851
# Execute the block within a context where our inheritable settings are forked

lib/grape/endpoint.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ def options?
321321
def build_stack
322322
stack = Grape::Middleware::Stack.new
323323

324-
content_types = namespace_stackable_with_hash(:content_types)
324+
content_types = inheritable_setting.namespace_stackable_with_hash(:content_types)
325325
format = namespace_inheritable(:format)
326326

327327
stack.use Rack::Head
@@ -333,10 +333,10 @@ def build_stack
333333
rescue_all: namespace_inheritable(:rescue_all),
334334
rescue_grape_exceptions: namespace_inheritable(:rescue_grape_exceptions),
335335
default_error_formatter: namespace_inheritable(:default_error_formatter),
336-
error_formatters: namespace_stackable_with_hash(:error_formatters),
337-
rescue_options: namespace_stackable_with_hash(:rescue_options),
336+
error_formatters: inheritable_setting.namespace_stackable_with_hash(:error_formatters),
337+
rescue_options: inheritable_setting.namespace_stackable_with_hash(:rescue_options),
338338
rescue_handlers: rescue_handlers,
339-
base_only_rescue_handlers: namespace_stackable_with_hash(:base_only_rescue_handlers),
339+
base_only_rescue_handlers: inheritable_setting.namespace_stackable_with_hash(:base_only_rescue_handlers),
340340
all_rescue_handler: namespace_inheritable(:all_rescue_handler),
341341
grape_exceptions_rescue_handler: namespace_inheritable(:grape_exceptions_rescue_handler)
342342

@@ -354,8 +354,8 @@ def build_stack
354354
format: format,
355355
default_format: namespace_inheritable(:default_format) || :txt,
356356
content_types: content_types,
357-
formatters: namespace_stackable_with_hash(:formatters),
358-
parsers: namespace_stackable_with_hash(:parsers)
357+
formatters: inheritable_setting.namespace_stackable_with_hash(:formatters),
358+
parsers: inheritable_setting.namespace_stackable_with_hash(:parsers)
359359

360360
builder = stack.build
361361
builder.run ->(env) { env[Grape::Env::API_ENDPOINT].run }

lib/grape/util/inheritable_setting.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ def to_hash
9595
namespace_reverse_stackable: namespace_reverse_stackable.to_hash
9696
}
9797
end
98+
99+
def namespace_stackable_with_hash(key)
100+
data = namespace_stackable[key]
101+
return if data.blank?
102+
103+
data.each_with_object({}) { |value, result| result.deep_merge!(value) }
104+
end
98105
end
99106
end
100107
end

spec/grape/api_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@
116116
end
117117

118118
it 'adds the association to the :representations setting' do
119-
klass = Class.new
120-
subject.represent Object, with: klass
121-
expect(subject.namespace_stackable_with_hash(:representations)[Object]).to eq(klass)
119+
dummy_presenter_klass = Class.new
120+
represent_object = Class.new
121+
subject.represent represent_object, with: dummy_presenter_klass
122+
expect(subject.inheritable_setting.namespace_stackable[:representations]).to eq([represent_object => dummy_presenter_klass])
122123
end
123124
end
124125

0 commit comments

Comments
 (0)