Skip to content

Commit

Permalink
Make format/s non private (#49)
Browse files Browse the repository at this point in the history
* Make format/s non private

* Updated README.md explaining the change of making the format method non private

* Clarified README.md a bit more

* Fixed minor typo
  • Loading branch information
mdedetrich authored and lloydmeta committed Jul 20, 2016
1 parent 972bc40 commit 32f83d3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,34 @@ object GreetingForm {
}
```

Another alternative (if for example your `Enum` can't extend `PlayEnum` or `PlayFormFieldEnum`) is to create an implicit `Format`
and bring it into scope using Play's `of`, i.e.

```scala
import play.api.data.Form
import play.api.data.Forms._

object Formats {
implicit val greetingFormat = enumeratum.Forms.format(Greeting)
}

object GreetingForm {
import Formats._

val form = Form(
mapping(
"name" -> nonEmptyText,
"greeting" -> of[Greeting]
)(Data.apply)(Data.unapply)
)

case class Data(
name: String,
greeting: Greeting)

}
```

## Play JSON

The `enumeratum-play-json` project is published separately and gives you access to Play's auto-generated boilerplate
Expand Down
6 changes: 3 additions & 3 deletions enumeratum-play/src/main/scala/enumeratum/Forms.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ object Forms {
* @param enum The enum
* @param insensitive bind in a case-insensitive way, defaults to false
*/
private[enumeratum] def format[A <: EnumEntry](enum: Enum[A], insensitive: Boolean = false): Formatter[A] = new Formatter[A] {
def format[A <: EnumEntry](enum: Enum[A], insensitive: Boolean = false): Formatter[A] = new Formatter[A] {
def bind(key: String, data: Map[String, String]) = {
play.api.data.format.Formats.stringFormat.bind(key, data).right.flatMap { s =>
val maybeBound = if (insensitive) enum.withNameInsensitiveOption(s) else enum.withNameOption(s)
Expand All @@ -70,7 +70,7 @@ object Forms {
*
* @param enum The enum
*/
private[enumeratum] def formatLowercaseOnly[A <: EnumEntry](enum: Enum[A]): Formatter[A] = new Formatter[A] {
def formatLowercaseOnly[A <: EnumEntry](enum: Enum[A]): Formatter[A] = new Formatter[A] {
def bind(key: String, data: Map[String, String]) = {
play.api.data.format.Formats.stringFormat.bind(key, data).right.flatMap { s =>
enum.withNameLowercaseOnlyOption(s) match {
Expand All @@ -87,7 +87,7 @@ object Forms {
*
* @param enum The enum
*/
private[enumeratum] def formatUppercaseOnly[A <: EnumEntry](enum: Enum[A]): Formatter[A] = new Formatter[A] {
def formatUppercaseOnly[A <: EnumEntry](enum: Enum[A]): Formatter[A] = new Formatter[A] {
def bind(key: String, data: Map[String, String]) = {
play.api.data.format.Formats.stringFormat.bind(key, data).right.flatMap { s =>
enum.withNameUppercaseOnlyOption(s) match {
Expand Down

0 comments on commit 32f83d3

Please sign in to comment.