IntPlayEnum

enumeratum.values.IntPlayEnum
trait IntPlayEnum[EnumEntry <: IntEnumEntry] extends IntEnum[EnumEntry], IntPlayPathBindableValueEnum[EnumEntry], IntPlayQueryBindableValueEnum[EnumEntry], IntPlayFormValueEnum[EnumEntry], IntPlayJsonValueEnum[EnumEntry]

An IntEnum that has a lot of the Play-related implicits built-in so you can avoid boilerplate.

Things included are:

  • implicit PathBindable (for binding from request path)
  • implicit QueryStringBindable (for binding from query strings)
  • formField for doing things like Form("hello" -> MyEnum.formField)
  • implicit Json format

Example:

scala> import enumeratum.values._
scala> import play.api.data.Form
scala> import play.api.libs.json._

scala> sealed abstract class Greeting(val value:Int) extends IntEnumEntry

scala> object Greeting extends IntPlayEnum[Greeting] {
   |   val values = findValues
   |   case object Hello   extends Greeting(1)
   |   case object GoodBye extends Greeting(2)
   |   case object Hi      extends Greeting(3)
   |   case object Bye     extends Greeting(4)
   | }

scala> val form = Form("greeting" -> Greeting.formField)
scala> form.bind(Map("greeting" -> "1")).value
res0: Option[Greeting] = Some(Hello)

scala> Json.toJson(Greeting.Hello) res1: JsValue = 1

Attributes

Graph
Supertypes
trait IntPlayJsonValueEnum[EnumEntry]
trait PlayJsonValueEnum[Int, EnumEntry]
trait IntPlayFormValueEnum[EnumEntry]
trait PlayFormValueEnum[Int, EnumEntry]
trait PlayQueryBindableValueEnum[Int, EnumEntry]
trait IntPlayPathBindableValueEnum[EnumEntry]
trait PlayPathBindableValueEnum[Int, EnumEntry]
trait IntEnum[EnumEntry]
trait ValueEnum[Int, EnumEntry]
class Object
trait Matchable
class Any
Show all

Members list

Value members

Inherited methods

inline protected def findValues: IndexedSeq[A]

Returns a Seq of A objects that the macro was able to find.

Returns a Seq of A objects that the macro was able to find.

You will want to use this in some way to implement your values method. In fact, if you aren't using this method...why are you even bothering with this lib?

Attributes

Inherited from:
IntEnumCompat (hidden)
def values: IndexedSeq[EntryType]

The sequence of values for your Enum. You will typically want to implement this in your extending class as a val so that withValue and friends are as efficient as possible.

The sequence of values for your Enum. You will typically want to implement this in your extending class as a val so that withValue and friends are as efficient as possible.

Feel free to implement this however you'd like (including messing around with ordering, etc) if that fits your needs better.

Attributes

Inherited from:
ValueEnum
def withValue(i: Int): EntryType

Tries to get an EntryType by the supplied value. The value corresponds to the .value of the case objects implementing EntryType

Tries to get an EntryType by the supplied value. The value corresponds to the .value of the case objects implementing EntryType

Like Enumeration 's withValue, this method will throw if the value does not match any of the values' .value values.

Attributes

Inherited from:
ValueEnum
def withValueEither(i: Int): Either[NoSuchMember[ValueType, ValueEnumEntry[ValueType]], EntryType]

Returns an [[Right[EntryType]] ] for a given value, or a [[Left[NoSuchMember]] ] if the value does not match any of the values' .value values.

Returns an [[Right[EntryType]] ] for a given value, or a [[Left[NoSuchMember]] ] if the value does not match any of the values' .value values.

Attributes

Inherited from:
ValueEnum
def withValueOpt(i: Int): Option[EntryType]

Optionally returns an EntryType for a given value.

Optionally returns an EntryType for a given value.

Attributes

Inherited from:
ValueEnum

Inherited fields

protected val baseFormatter: Formatter[Int]

The Formatter for binding the ValueType of this ValueEnum.

The Formatter for binding the ValueType of this ValueEnum.

Used for building the Formatter for the entries

Attributes

Inherited from:
IntPlayFormValueEnum
lazy val formField: Mapping[EnumEntry]

Field for mapping this enum in Forms

Field for mapping this enum in Forms

Attributes

Inherited from:
PlayFormValueEnum
lazy val fromPath: PathBindableExtractor[EnumEntry]

Binder for play.api.routing.sird router

Binder for play.api.routing.sird router

Example:

scala> import play.api.routing.sird._
scala> import play.api.routing._
scala> import play.api.mvc._

scala> sealed abstract class Greeting(val value: Int) extends IntEnumEntry

scala> object Greeting extends IntPlayEnum[Greeting] {
   |   val values = findValues
   |   case object Hello   extends Greeting(1)
   |   case object GoodBye extends Greeting(2)
   |   case object Hi      extends Greeting(3)
   |   case object Bye     extends Greeting(4)
   | }

scala> val router = Router.from {
   |   case GET(p"/hello/${Greeting.fromPath(greeting)}") => Action {
   |     Results.Ok(s"$greeting")
   |   }
   | }
scala> router.routes
res0: Router.Routes = <function1>

Attributes

Inherited from:
PlayPathBindableValueEnum
final lazy val valuesToEntriesMap: Map[Int, EnumEntry]

Map of ValueType to EntryType members

Map of ValueType to EntryType members

Attributes

Inherited from:
ValueEnum

Implicits

Inherited implicits

implicit val format: Format[EnumEntry]

Implicit JSON format for the entries of this enum

Implicit JSON format for the entries of this enum

Attributes

Inherited from:
IntPlayJsonValueEnum
implicit val pathBindable: PathBindable[EnumEntry]

Implicit path binder for Play's default router

Implicit path binder for Play's default router

Attributes

Inherited from:
IntPlayPathBindableValueEnum
implicit val queryBindable: QueryStringBindable[EnumEntry]

Implicit path binder for Play's default router

Implicit path binder for Play's default router

Attributes

Inherited from:
IntPlayQueryBindableValueEnum