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

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. # :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. # 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) lookup = lookup_filters_for_path_type(verb, path, type)
if lookup.found? && lookup.payload.is_a?(Array(Block)) if lookup.found? && lookup.payload.is_a?(Array(Block))
(lookup.payload as Array(Block)) << Block.new(&block) (lookup.payload as Array(Block)) << Block.new(&block)
@ -36,12 +36,12 @@ module Kemal::Middleware
end 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` # 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 _add_route_filter verb, path, :before, &block
end 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` # 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 _add_route_filter verb, path, :after, &block
end end
@ -71,9 +71,10 @@ module Kemal::Middleware
end end
class Block 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 end
def call(context) def call(context)
@ -89,7 +90,7 @@ end
ALL_METHODS = %w(get post put patch delete all) ALL_METHODS = %w(get post put patch delete all)
{% for type in ["before", "after"] %} {% for type in ["before", "after"] %}
{% for method in ALL_METHODS %} {% 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) Kemal::Middleware::Filter::INSTANCE.{{type.id}}({{method}}.upcase, path, &block)
end end
{% end %} {% end %}