struct Athena::Framework::ParameterBag
inherits Struct
#
A container for storing key/value pairs. Can be used to store arbitrary data within the context of a request.
It can be accessed via ATH::Request#attributes
.
Example#
For example, an artbirary value can be stored in the attributes, and later provided as an action argument.
require "athena"
# Define a request listener to add our value before the action is executed.
@[ADI::Register]
struct TestListener
@[AEDA::AsEventListener]
def on_request(event : ATH::Events::Request) : Nil
# Store our value within the request's attributes, restricted to a `String`.
event.request.attributes.set "my_arg", "foo", String
end
end
class ExampleController < ATH::Controller
# Define an action parameter with the same name of the parameter stored in attributes.
#
# The argument to pass is resolved via `ATHR::RequestAttribute`.
get "/", my_arg : String do
my_arg
end
end
ATH.run
# GET / # => "foo"
Constructors#
Methods#
#get(name : String, _type : Bool.class) : Bool
#
Returns the value of the parameter with the provided name as a Bool
.
#get(name : String, _type : String.class) : String
#
Returns the value of the parameter with the provided name as a String
.
#get(name : String, _type : Float32.class) : Float32
#
Returns the value of the parameter with the provided name as a Float32
.
#get(name : String, _type : Float64.class) : Float64
#
Returns the value of the parameter with the provided name as a Float64
.
#get(name : String, _type : Int128.class) : Int128
#
Returns the value of the parameter with the provided name as a Int128
.
#get(name : String, _type : Int16.class) : Int16
#
Returns the value of the parameter with the provided name as a Int16
.
#get(name : String, _type : Int32.class) : Int32
#
Returns the value of the parameter with the provided name as a Int32
.
#get(name : String, _type : Int64.class) : Int64
#
Returns the value of the parameter with the provided name as a Int64
.
#get(name : String, _type : Int8.class) : Int8
#
Returns the value of the parameter with the provided name as a Int8
.
#get(name : String, _type : UInt128.class) : UInt128
#
Returns the value of the parameter with the provided name as a UInt128
.
#get(name : String, _type : UInt16.class) : UInt16
#
Returns the value of the parameter with the provided name as a UInt16
.
#get(name : String, _type : UInt32.class) : UInt32
#
Returns the value of the parameter with the provided name as a UInt32
.
#get(name : String, _type : UInt64.class) : UInt64
#
Returns the value of the parameter with the provided name as a UInt64
.
#get(name : String, _type : UInt8.class) : UInt8
#
Returns the value of the parameter with the provided name as a UInt8
.
#get(name : String, type : T.class) : T forall T
#
Returns the value of the parameter with the provided name, casted to the provided type.
Raises a KeyError
if no parameter with that name exists.
#get(name : String)
#
Returns the value of the parameter with the provided name.
Raises a KeyError
if no parameter with that name exists.
#get?(name : String, _type : (Bool | ::Nil).class) : Bool | Nil
#
Returns the value of the parameter with the provided name as a Bool
, or nil
if it does not exist.
#get?(name : String, _type : (String | ::Nil).class) : String | Nil
#
Returns the value of the parameter with the provided name as a String
, or nil
if it does not exist.
#get?(name : String, _type : (Float32 | ::Nil).class) : Float32 | Nil
#
Returns the value of the parameter with the provided name as a Float32
, or nil
if it does not exist.
#get?(name : String, _type : (Float64 | ::Nil).class) : Float64 | Nil
#
Returns the value of the parameter with the provided name as a Float64
, or nil
if it does not exist.
#get?(name : String, _type : (Int128 | ::Nil).class) : Int128 | Nil
#
Returns the value of the parameter with the provided name as a Int128
, or nil
if it does not exist.
#get?(name : String, _type : (Int16 | ::Nil).class) : Int16 | Nil
#
Returns the value of the parameter with the provided name as a Int16
, or nil
if it does not exist.
#get?(name : String, _type : (Int32 | ::Nil).class) : Int32 | Nil
#
Returns the value of the parameter with the provided name as a Int32
, or nil
if it does not exist.
#get?(name : String, _type : (Int64 | ::Nil).class) : Int64 | Nil
#
Returns the value of the parameter with the provided name as a Int64
, or nil
if it does not exist.
#get?(name : String, _type : (Int8 | ::Nil).class) : Int8 | Nil
#
Returns the value of the parameter with the provided name as a Int8
, or nil
if it does not exist.
#get?(name : String, _type : (UInt128 | ::Nil).class) : UInt128 | Nil
#
Returns the value of the parameter with the provided name as a UInt128
, or nil
if it does not exist.
#get?(name : String, _type : (UInt16 | ::Nil).class) : UInt16 | Nil
#
Returns the value of the parameter with the provided name as a UInt16
, or nil
if it does not exist.
#get?(name : String, _type : (UInt32 | ::Nil).class) : UInt32 | Nil
#
Returns the value of the parameter with the provided name as a UInt32
, or nil
if it does not exist.
#get?(name : String, _type : (UInt64 | ::Nil).class) : UInt64 | Nil
#
Returns the value of the parameter with the provided name as a UInt64
, or nil
if it does not exist.
#get?(name : String, _type : (UInt8 | ::Nil).class) : UInt8 | Nil
#
Returns the value of the parameter with the provided name as a UInt8
, or nil
if it does not exist.
#get?(name : String, type : (T | ::Nil).class) : T | Nil forall T
#
Returns the value of the parameter with the provided name casted to the provided type if it exists, otherwise nil
.
#get?(name : String)
#
Returns the value of the parameter with the provided name if it exists, otherwise nil
.
#has?(name : String, type : T.class) : Bool forall T
#
Returns true
if a parameter with the provided name exists and is of the provided type, otherwise false
.
#has?(name : String) : Bool
#
Returns true
if a parameter with the provided name exists, otherwise false
.
#set(name : String, value : T, type : T.class) : Nil forall T
#
Sets a parameter with the provided name to value, restricted to the given type.
#set(name : String, value : T) : Nil forall T
#
Sets a parameter with the provided name to value.