struct Athena::Routing::Requirement::Enum(EnumType)

Overview

Provides an easier way to define a [route requirement][Athena::Routing::Route--parameter-validation] 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.

Defined in:

requirement/enum.cr

Constructors

Instance Method Summary

Constructor Detail

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

[View source]
def self.new(*cases : EnumType) #

[View source]

Instance Method Detail

def members : Set(EnumType) | Nil #

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


[View source]