Skip to content

Commit

Permalink
Use route.namespace instead instance_variable_get(@options) (#931)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
ericproulx authored Jun 10, 2024
1 parent e7d6790 commit 707b00b
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ def combine_routes(app, doc_klass)
end

def determine_namespaced_routes(name, parent_route, routes)
if parent_route.nil?
routes.values.flatten
else
parent_route.reject do |route|
!route_path_start_with?(route, name) || !route_instance_variable_equals?(route, name)
end
return routes.values.flatten if parent_route.nil?

parent_route.select do |route|
route_path_start_with?(route, name) || route_namespace_equals?(route, name)
end
end

Expand Down Expand Up @@ -94,20 +92,27 @@ def extract_parent_route(name)
matches.nil? ? route_name : matches[0].delete('/')
end

def route_instance_variable(route)
route.instance_variable_get(:@options)[:namespace]
end
def route_namespace_equals?(route, name)
patterns = Enumerator.new do |yielder|
yielder << "/#{name}"
yielder << "/:version/#{name}"
end

def route_instance_variable_equals?(route, name)
route_instance_variable(route) == "/#{name}" ||
route_instance_variable(route) == "/:version/#{name}"
patterns.any? { |p| route.namespace == p }
end

def route_path_start_with?(route, name)
route_prefix = route.prefix ? "/#{route.prefix}/#{name}" : "/#{name}"
route_versioned_prefix = route.prefix ? "/#{route.prefix}/:version/#{name}" : "/:version/#{name}"
patterns = Enumerator.new do |yielder|
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

Expand Down

0 comments on commit 707b00b

Please sign in to comment.