Update filters to dont care about block return type

This commit is contained in:
Sdogruyol 2016-05-06 21:03:24 +03:00
parent 3b402586f8
commit 53df88b9fb
1 changed files with 7 additions and 6 deletions

View File

@ -26,7 +26,7 @@ module Kemal::Middleware
# :nodoc: This shouldn't be called directly, it's not private because I need to call it for testing purpose since I can't call the macros in the spec.
# It adds the block for the corresponding verb/path/type combination to the tree.
def _add_route_filter(verb, path, type, &block : HTTP::Server::Context -> String)
def _add_route_filter(verb, path, type, &block : HTTP::Server::Context -> _)
lookup = lookup_filters_for_path_type(verb, path, type)
if lookup.found? && lookup.payload.is_a?(Array(Block))
(lookup.payload as Array(Block)) << Block.new(&block)
@ -36,12 +36,12 @@ module Kemal::Middleware
end
# This can be called directly but it's simpler to just use the macros, it will check if another filter is not already defined for this verb/path/type and proceed to call `add_route_filter`
def before(verb, path = "*", &block : HTTP::Server::Context -> String)
def before(verb, path = "*", &block : HTTP::Server::Context -> _)
_add_route_filter verb, path, :before, &block
end
# This can be called directly but it's simpler to just use the macros, it will check if another filter is not already defined for this verb/path/type and proceed to call `add_route_filter`
def after(verb, path = "*", &block : HTTP::Server::Context -> String)
def after(verb, path = "*", &block : HTTP::Server::Context -> _)
_add_route_filter verb, path, :after, &block
end
@ -71,9 +71,10 @@ module Kemal::Middleware
end
class Block
property block : (HTTP::Server::Context -> String)
property block : HTTP::Server::Context -> String
def initialize(&@block : HTTP::Server::Context -> String)
def initialize(&block : HTTP::Server::Context -> _)
@block = ->(context : HTTP::Server::Context) { block.call(context).to_s}
end
def call(context)
@ -89,7 +90,7 @@ end
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 -> String)
def {{type.id}}_{{method.id}}(path = "*", &block : HTTP::Server::Context -> _)
Kemal::Middleware::Filter::INSTANCE.{{type.id}}({{method}}.upcase, path, &block)
end
{% end %}