allow %w in Handler macros (#385)

This commit is contained in:
Will Leinweber 2017-08-18 00:19:21 -07:00 committed by Serdar Dogruyol
parent fc4c97aafd
commit 5a83522866
2 changed files with 29 additions and 2 deletions

View file

@ -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

View file

@ -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