From 274d34cfc6f7645cf1837ee22343a0fa39298d83 Mon Sep 17 00:00:00 2001 From: Sdogruyol Date: Sun, 4 Dec 2016 01:47:09 +0300 Subject: [PATCH] Move filter methods to dsl --- src/kemal/dsl.cr | 21 ++++++++++++++++++--- src/kemal/filter_handler.cr | 12 ------------ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/kemal/dsl.cr b/src/kemal/dsl.cr index 1c8249a..fc62e43 100644 --- a/src/kemal/dsl.cr +++ b/src/kemal/dsl.cr @@ -1,7 +1,11 @@ # Kemal DSL is defined here and it's baked into global scope. -# The DSL currently consists of HTTP verbs(get post put patch delete options), -# WebSocket(ws) and custom error handler(error). -HTTP_METHODS = %w(get post put patch delete options) +# The DSL currently consists of +# - get post put patch delete options +# - WebSocket(ws) +# - before_* +# - error +HTTP_METHODS = %w(get post put patch delete options) +FILTER_METHODS = %w(get post put patch delete options all) {% for method in HTTP_METHODS %} def {{method.id}}(path, &block : HTTP::Server::Context -> _) @@ -18,3 +22,14 @@ end def error(status_code, &block : HTTP::Server::Context, Exception -> _) Kemal.config.add_error_handler status_code, &block end + +# All the helper methods available are: +# - before_all, before_get, before_post, before_put, before_patch, before_delete, before_options +# - after_all, after_get, after_post, after_put, after_patch, after_delete, after_options +{% for type in ["before", "after"] %} + {% for method in FILTER_METHODS %} + def {{type.id}}_{{method.id}}(path = "*", &block : HTTP::Server::Context -> _) + Kemal::FilterHandler::INSTANCE.{{type.id}}({{method}}.upcase, path, &block) + end + {% end %} +{% end %} diff --git a/src/kemal/filter_handler.cr b/src/kemal/filter_handler.cr index c826dc4..8c19c94 100644 --- a/src/kemal/filter_handler.cr +++ b/src/kemal/filter_handler.cr @@ -83,15 +83,3 @@ module Kemal end end end - -# All the helper methods available are: -# - before_all, before_get, before_post, before_put, before_patch, before_delete -# - after_all, after_get, after_post, after_put, after_patch, after_delete -ALL_METHODS = %w(get post put patch delete all) -{% for type in ["before", "after"] %} - {% for method in ALL_METHODS %} - def {{type.id}}_{{method.id}}(path = "*", &block : HTTP::Server::Context -> _) - Kemal::FilterHandler::INSTANCE.{{type.id}}({{method}}.upcase, path, &block) - end - {% end %} -{% end %}