Skip to content

struct Athena::Routing::Requirement::Enum(EnumType)
inherits Struct #

Provides an easier way to define a route requirement for all, or a subset of, Enum members.

For example:

require "athena"

enum Color
  Red
  Blue
  Green
  Black
end

class ExampleController < ATH::Controller
  @[ARTA::Get(
    "/color/{color}",
    requirements: {"color" => ART::Requirement::Enum(Color).new},
  )]
  def get_color(color : Color) : Color
    color
  end

  @[ARTA::Get(
    "/rgb-color/{color}",
    requirements: {"color" => ART::Requirement::Enum(Color).new(:red, :green, :blue)},
  )]
  def get_rgb_color(color : Color) : Color
    color
  end
end

ATH.run

# GET /color/red  # => "red"
# GET /color/pink # => 404
#
# GET /rgb-color/red   # => "red"
# GET /rgb-color/green # => "green"
# GET /rgb-color/blue  # => "blue"
# GET /rgb-color/black # => 404

Note

This type ONLY supports the string representation of enum members.

Constructors#

.new(members : Set(EnumType) | Nil = nil)#

View source

.new(*cases : EnumType)#

View source

Methods#

#members : Set(EnumType) | ::Nil#

Returns the set of allowed enum members, or nil if all members are allowed.

View source