Skip to content

struct Athena::Framework::Controller::ValueResolvers::Enum
inherits Struct #

Handles resolving an Enum member from a string value that is stored in the request's ATH::Request#attributes. This resolver supports both numeric and string based parsing, returning a proper error response if the provided value does not map to any valid member.

require "athena"

enum Color
  Red
  Blue
  Green
end

class ExampleController < ATH::Controller
  @[ARTA::Get("/numeric/{color}")]
  def get_color_numeric(color : Color) : Color
    color
  end

  @[ARTA::Get("/string/{color}")]
  def get_color_string(color : Color) : Color
    color
  end
end

ATH.run

# GET /numeric/1 # => "blue"
# GET /string/red # => "red"

Tip

Checkout ART::Requirement::Enum for an easy way to restrict routing to an enum's members, or a subset of them.

Included modules

Athena::Framework::Controller::ValueResolvers::Interface

Constructors#

Methods#

#initialize#

View source

#resolve(request : ATH::Request, parameter : ATH::Controller::ParameterMetadata)#

Returns a value resolved from the provided request and parameter if possible, otherwise returns nil if no parameter could be resolved.

View source