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