struct Athena::Routing::ParameterBag
- Athena::Routing::ParameterBag
- Struct
- Value
- Object
Overview
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 HTTP::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
include AED::EventListenerInterface
def self.subscribed_events : AED::SubscribedEvents
AED::SubscribedEvents{
ART::Events::Request => 0,
}
end
def call(event : ART::Events::Request, dispatcher : AED::EventDispatcherInterface) : 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 < ART::Controller
# Define an action argument with the same name of the argument stored in attributes.
#
# The argument is resolved via `ART::Arguments::Resolvers::RequestAttribute`.
get "/", my_arg : String do
my_arg
end
end
ART.run
# GET / # => "foo"
Defined in:
parameter_bag.crConstructors
Instance Method Summary
-
#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)
Returns the value of the parameter with the provided name.
-
#get?(name : String)
Returns the value of the parameter with the provided name if it exists, otherwise
nil
. -
#has?(name : String) : Bool
Returns
true
if a parameter with the provided name exists, otherwisefalse
. - #initialize
-
#remove(name : String) : Nil
Removes the parameter with the provided name.
-
#set(name : String, value : _, 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.
Constructor Detail
Instance Method Detail
Returns the value of the parameter with the provided name as a Bool
.
Returns the value of the parameter with the provided name as a String
.
Returns the value of the parameter with the provided name as a Float32
.
Returns the value of the parameter with the provided name as a Float64
.
Returns the value of the parameter with the provided name as a Int128
.
Returns the value of the parameter with the provided name as a Int16
.
Returns the value of the parameter with the provided name as a Int32
.
Returns the value of the parameter with the provided name as a Int64
.
Returns the value of the parameter with the provided name as a Int8
.
Returns the value of the parameter with the provided name as a UInt128
.
Returns the value of the parameter with the provided name as a UInt16
.
Returns the value of the parameter with the provided name as a UInt32
.
Returns the value of the parameter with the provided name as a UInt64
.
Returns the value of the parameter with the provided name as a UInt8
.
Returns the value of the parameter with the provided name.
Raises a KeyError
if no parameter with that name exists.
Returns the value of the parameter with the provided name if it exists, otherwise nil
.
Returns true
if a parameter with the provided name exists, otherwise false
.
Sets a parameter with the provided name to value, restricted to the given type.
Sets a parameter with the provided name to value.