Use `route.namespace` instead instance_variable_get(@options) (#931)

Rename `route_instance_variable_equals?` to `route_namespace_equals?`
Refactor route_path_start_with? and route_namespace_equals? with Enumerator instead of static Arrays
Refactor determine_namespaced_routes in positive logic (select over reject)
This commit is contained in:
Eric Proulx 2024-06-11 01:33:41 +02:00 committed by GitHub
parent e7d6790f73
commit 707b00b91b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 20 additions and 15 deletions

View File

@ -45,12 +45,10 @@ module SwaggerRouting
end end
def determine_namespaced_routes(name, parent_route, routes) def determine_namespaced_routes(name, parent_route, routes)
if parent_route.nil? return routes.values.flatten if parent_route.nil?
routes.values.flatten
else parent_route.select do |route|
parent_route.reject do |route| route_path_start_with?(route, name) || route_namespace_equals?(route, name)
!route_path_start_with?(route, name) || !route_instance_variable_equals?(route, name)
end
end end
end end
@ -94,20 +92,27 @@ module SwaggerRouting
matches.nil? ? route_name : matches[0].delete('/') matches.nil? ? route_name : matches[0].delete('/')
end end
def route_instance_variable(route) def route_namespace_equals?(route, name)
route.instance_variable_get(:@options)[:namespace] patterns = Enumerator.new do |yielder|
end yielder << "/#{name}"
yielder << "/:version/#{name}"
end
def route_instance_variable_equals?(route, name) patterns.any? { |p| route.namespace == p }
route_instance_variable(route) == "/#{name}" ||
route_instance_variable(route) == "/:version/#{name}"
end end
def route_path_start_with?(route, name) def route_path_start_with?(route, name)
route_prefix = route.prefix ? "/#{route.prefix}/#{name}" : "/#{name}" patterns = Enumerator.new do |yielder|
route_versioned_prefix = route.prefix ? "/#{route.prefix}/:version/#{name}" : "/:version/#{name}" if route.prefix
yielder << "/#{route.prefix}/#{name}"
yielder << "/#{route.prefix}/:version/#{name}"
else
yielder << "/#{name}"
yielder << "/:version/#{name}"
end
end
route.path.start_with?(route_prefix, route_versioned_prefix) patterns.any? { |p| route.path.start_with?(p) }
end end
end end