Move filter methods to dsl

This commit is contained in:
Sdogruyol 2016-12-04 01:47:09 +03:00
parent eb64b52f84
commit 274d34cfc6
2 changed files with 18 additions and 15 deletions

View file

@ -1,7 +1,11 @@
# Kemal DSL is defined here and it's baked into global scope. # 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), # The DSL currently consists of
# WebSocket(ws) and custom error handler(error). # - get post put patch delete options
HTTP_METHODS = %w(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 %} {% for method in HTTP_METHODS %}
def {{method.id}}(path, &block : HTTP::Server::Context -> _) def {{method.id}}(path, &block : HTTP::Server::Context -> _)
@ -18,3 +22,14 @@ end
def error(status_code, &block : HTTP::Server::Context, Exception -> _) def error(status_code, &block : HTTP::Server::Context, Exception -> _)
Kemal.config.add_error_handler status_code, &block Kemal.config.add_error_handler status_code, &block
end 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 %}

View file

@ -83,15 +83,3 @@ module Kemal
end end
end 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 %}