Skip to content

struct Athena::Routing::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 ART::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"

Class methods#

.new #

View source

Methods#

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

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

View source

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

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

View source

#get(name : String, _type : Float32.class) : Float32 #

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

View source

#get(name : String, _type : Float64.class) : Float64 #

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

View source

#get(name : String, _type : Int128.class) : Int128 #

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

View source

#get(name : String, _type : Int16.class) : Int16 #

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

View source

#get(name : String, _type : Int32.class) : Int32 #

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

View source

#get(name : String, _type : Int64.class) : Int64 #

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

View source

#get(name : String, _type : Int8.class) : Int8 #

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

View source

#get(name : String, _type : UInt128.class) : UInt128 #

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

View source

#get(name : String, _type : UInt16.class) : UInt16 #

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

View source

#get(name : String, _type : UInt32.class) : UInt32 #

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

View source

#get(name : String, _type : UInt64.class) : UInt64 #

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

View source

#get(name : String, _type : UInt8.class) : UInt8 #

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

View source

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

View source

#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

#get?(name : String, type : T | ::Nil.class) : T? forall T #

Returns the value of the parameter with the provided name casted to the provied type if it exists, otherwise nil.

View source

#get?(name : String, _type : Bool | ::Nil.class) : Bool? #

Returns the value of the parameter with the provided name as a Bool, or nil if it does not exist.

View source

#get?(name : String, _type : String | ::Nil.class) : String? #

Returns the value of the parameter with the provided name as a String, or nil if it does not exist.

View source

#get?(name : String, _type : Float32 | ::Nil.class) : Float32? #

Returns the value of the parameter with the provided name as a Float32, or nil if it does not exist.

View source

#get?(name : String, _type : Float64 | ::Nil.class) : Float64? #

Returns the value of the parameter with the provided name as a Float64, or nil if it does not exist.

View source

#get?(name : String, _type : Int128 | ::Nil.class) : Int128? #

Returns the value of the parameter with the provided name as a Int128, or nil if it does not exist.

View source

#get?(name : String, _type : Int16 | ::Nil.class) : Int16? #

Returns the value of the parameter with the provided name as a Int16, or nil if it does not exist.

View source

#get?(name : String, _type : Int32 | ::Nil.class) : Int32? #

Returns the value of the parameter with the provided name as a Int32, or nil if it does not exist.

View source

#get?(name : String, _type : Int64 | ::Nil.class) : Int64? #

Returns the value of the parameter with the provided name as a Int64, or nil if it does not exist.

View source

#get?(name : String, _type : Int8 | ::Nil.class) : Int8? #

Returns the value of the parameter with the provided name as a Int8, or nil if it does not exist.

View source

#get?(name : String, _type : UInt128 | ::Nil.class) : UInt128? #

Returns the value of the parameter with the provided name as a UInt128, or nil if it does not exist.

View source

#get?(name : String, _type : UInt16 | ::Nil.class) : UInt16? #

Returns the value of the parameter with the provided name as a UInt16, or nil if it does not exist.

View source

#get?(name : String, _type : UInt32 | ::Nil.class) : UInt32? #

Returns the value of the parameter with the provided name as a UInt32, or nil if it does not exist.

View source

#get?(name : String, _type : UInt64 | ::Nil.class) : UInt64? #

Returns the value of the parameter with the provided name as a UInt64, or nil if it does not exist.

View source

#get?(name : String, _type : UInt8 | ::Nil.class) : UInt8? #

Returns the value of the parameter with the provided name as a UInt8, or nil if it does not exist.

View source

#get?(name : String) #

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

View source

#has?(name : String) : Bool #

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

View source

#remove(name : String) : Nil #

Removes the parameter with the provided name.

View source

#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

#set(name : String, value : T) : Nil forall T #

Sets a parameter with the provided name to value.

View source