From 5a835228668cc0942089ce4526185446fbb45b81 Mon Sep 17 00:00:00 2001 From: Will Leinweber Date: Fri, 18 Aug 2017 00:19:21 -0700 Subject: [PATCH] allow %w in Handler macros (#385) --- spec/handler_spec.cr | 27 +++++++++++++++++++++++++++ src/kemal/handler.cr | 4 ++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/spec/handler_spec.cr b/spec/handler_spec.cr index fb0230d..c3268d7 100644 --- a/spec/handler_spec.cr +++ b/spec/handler_spec.cr @@ -47,6 +47,26 @@ class PostExcludeHandler < Kemal::Handler end end +class ExcludeHandlerPercentW < Kemal::Handler + exclude %w[/exclude] + + def call(env) + return call_next(env) if exclude_match?(env) + env.response.print "Exclude" + call_next env + end +end + +class PostOnlyHandlerPercentW < Kemal::Handler + only %w[/only /route1 /route2], "POST" + + def call(env) + return call_next(env) unless only_match?(env) + env.response.print "Only" + call_next env + end +end + describe "Handler" do it "adds custom handler before before_*" do filter_middleware = Kemal::FilterHandler.new @@ -131,4 +151,11 @@ describe "Handler" do Kemal.config.handlers = [post_only_handler, post_exclude_handler] Kemal.config.handlers.should eq [post_only_handler, post_exclude_handler] end + + it "is able to use %w in macros" do + post_only_handler = PostOnlyHandlerPercentW.new + exclude_handler = ExcludeHandlerPercentW.new + Kemal.config.handlers = [post_only_handler, exclude_handler] + Kemal.config.handlers.should eq [post_only_handler, exclude_handler] + end end diff --git a/src/kemal/handler.cr b/src/kemal/handler.cr index 76e99be..e06ae27 100644 --- a/src/kemal/handler.cr +++ b/src/kemal/handler.cr @@ -9,14 +9,14 @@ module Kemal macro only(paths, method = "GET") class_name = {{@type.name}} - {{paths}}.each do |path| + ({{paths}}).each do |path| @@only_routes_tree.add "#{class_name}/#{{{method}}.downcase}#{path}", "/#{{{method}}.downcase}#{path}" end end macro exclude(paths, method = "GET") class_name = {{@type.name}} - {{paths}}.each do |path| + ({{paths}}).each do |path| @@exclude_routes_tree.add "#{class_name}/#{{{method}}.downcase}#{path}", "/#{{{method}}.downcase}#{path}" end end