struct Athena::Routing::ParameterBag

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.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new #

[View source]

Instance Method Detail

def get(name : String, _type : Bool.class) : Bool #

Returns the value of the parameter with the provided name as a Bool.


[View source]
def get(name : String, _type : String.class) : String #

Returns the value of the parameter with the provided name as a String.


[View source]
def get(name : String, _type : Float32.class) : Float32 #

Returns the value of the parameter with the provided name as a Float32.


[View source]
def get(name : String, _type : Float64.class) : Float64 #

Returns the value of the parameter with the provided name as a Float64.


[View source]
def get(name : String, _type : Int128.class) : Int128 #

Returns the value of the parameter with the provided name as a Int128.


[View source]
def get(name : String, _type : Int16.class) : Int16 #

Returns the value of the parameter with the provided name as a Int16.


[View source]
def get(name : String, _type : Int32.class) : Int32 #

Returns the value of the parameter with the provided name as a Int32.


[View source]
def get(name : String, _type : Int64.class) : Int64 #

Returns the value of the parameter with the provided name as a Int64.


[View source]
def get(name : String, _type : Int8.class) : Int8 #

Returns the value of the parameter with the provided name as a Int8.


[View source]
def get(name : String, _type : UInt128.class) : UInt128 #

Returns the value of the parameter with the provided name as a UInt128.


[View source]
def get(name : String, _type : UInt16.class) : UInt16 #

Returns the value of the parameter with the provided name as a UInt16.


[View source]
def get(name : String, _type : UInt32.class) : UInt32 #

Returns the value of the parameter with the provided name as a UInt32.


[View source]
def get(name : String, _type : UInt64.class) : UInt64 #

Returns the value of the parameter with the provided name as a UInt64.


[View source]
def get(name : String, _type : UInt8.class) : UInt8 #

Returns the value of the parameter with the provided name as a UInt8.


[View source]
def get(name : String) #

Returns the value of the parameter with the provided name.

Raises a KeyError if no parameter with that name exists.


[View source]
def get?(name : String) #

Returns the value of the parameter with the provided name if it exists, otherwise nil.


[View source]
def has?(name : String) : Bool #

Returns true if a parameter with the provided name exists, otherwise false.


[View source]
def initialize #

[View source]
def remove(name : String) : Nil #

Removes the parameter with the provided name.


[View source]
def set(name : String, value : _, type : T.class) : Nil forall T #

Sets a parameter with the provided name to value, restricted to the given type.


[View source]
def set(name : String, value : T) : Nil forall T #

Sets a parameter with the provided name to value.


[View source]