User defined context store types (#339)

allow the context storage to handle any types
This commit is contained in:
Rimas Silkaitis 2017-04-06 11:43:41 -07:00 committed by Serdar Dogruyol
parent 48128696c6
commit 0b4856b741
4 changed files with 43 additions and 2 deletions

View file

@ -4,8 +4,13 @@
# Instances of this class are passed to an `HTTP::Server` handler.
class HTTP::Server
class Context
alias StoreTypes = Nil | String | Int32 | Int64 | Float64 | Bool
getter store = {} of String => StoreTypes
# :nodoc:
STORE_MAPPINGS = [ Nil, String, Int32, Int64, Float64, Bool ]
macro finished
alias StoreTypes = Union({{ *STORE_MAPPINGS }})
getter store = {} of String => StoreTypes
end
def params
@request.url_params ||= route_lookup.params

View file

@ -76,3 +76,15 @@ macro halt(env, status_code = 200, response = "")
{{env}}.response.close
next
end
# Extends context storage with user defined types.
#
# class User
# property name
# end
#
# add_context_storage_type(User)
#
macro add_context_storage_type(type)
{{ HTTP::Server::Context::STORE_MAPPINGS.push(type) }}
end