lib/roda/plugins/static_routing.rb in roda-3.17.0 vs lib/roda/plugins/static_routing.rb in roda-3.18.0
- old
+ new
@@ -48,11 +48,11 @@
# As shown above, you can use Roda's routing tree methods inside the
# static_route block to have shared behavior for different request methods,
# while still handling the request methods differently.
module StaticRouting
def self.configure(app)
- app.opts[:static_routes] = {}
+ app.opts[:static_routes] ||= {}
end
module ClassMethods
# Freeze the static route metadata when freezing the app.
def freeze
@@ -94,35 +94,25 @@
private
# Add a static route for the given method.
def add_static_route(method, path, &block)
- (opts[:static_routes][path] ||= {})[method] = convert_route_block(block)
+ routes = opts[:static_routes][path] ||= {}
+ routes[method] = define_roda_method(routes[method] || "static_route_#{method}_#{path}", 1, &convert_route_block(block))
end
end
module InstanceMethods
private
# If there is a static routing method for the given path, call it
# instead having the routing tree handle the request.
def _roda_before_30__static_routing
r = @_request
- if route = self.class.static_route_for(r.request_method, r.path_info)
- r.static_route(&route)
- end
- end
- end
-
- module RequestMethods
- # Assume that this request matches a static route, setting
- # the remaining path to the emptry string and passing
- # control to the given block.
- def static_route(&block)
- @remaining_path = ''
-
- always do
- scope.instance_exec(self, &block)
+ if meth = self.class.static_route_for(r.request_method, r.path_info)
+ r.instance_variable_set(:@remaining_path, '')
+ r.send(:block_result, send(meth, r))
+ throw :halt, @_response.finish
end
end
end
end