Skip to content

Commit

Permalink
Merge bd015ff into 8e7bcc6
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydmeta committed Apr 18, 2015
2 parents 8e7bcc6 + bd015ff commit 98cd9da
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 42 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ Compatible with Scala 2.10.x and 2.11.x
For basic enumeratum (with no Play support):
```scala
libraryDependencies ++= Seq(
"com.beachape" %% "enumeratum" % "1.1.0"
"com.beachape" %% "enumeratum" % "1.2.0"
)
```

For enumeratum with Play JSON:
```scala
libraryDependencies ++= Seq(
"com.beachape" %% "enumeratum" % "1.1.0",
"com.beachape" %% "enumeratum-play-json" % "1.1.0"
"com.beachape" %% "enumeratum" % "1.2.0",
"com.beachape" %% "enumeratum-play-json" % "1.2.0"
)
```

For enumeratum with full Play support:
```scala
libraryDependencies ++= Seq(
"com.beachape" %% "enumeratum" % "1.1.0",
"com.beachape" %% "enumeratum-play" % "1.1.0"
"com.beachape" %% "enumeratum" % "1.2.0",
"com.beachape" %% "enumeratum-play" % "1.2.0"
)
```

Expand All @@ -51,9 +51,9 @@ the `values` method. If you don't care about ordinality, just pass `findValues`

```scala

import enumeratum.Enum
import enumeratum._

sealed trait Greeting
sealed trait Greeting extends EnumEntry

object Greeting extends Enum[Greeting] {

Expand Down Expand Up @@ -107,9 +107,9 @@ For example:
```scala
package enums._

import enumeratum.PlayEnum
import enumeratum._

sealed trait Greeting
sealed trait Greeting extends EnumEntry

object Greeting extends PlayEnum[Greeting] {

Expand Down
12 changes: 6 additions & 6 deletions enumeratum-core/src/main/scala/enumeratum/Enum.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import scala.language.postfixOps
* This is yet another one.
*
* Oh yeah, [[Enum]] is BYOO (bring your own ordinality). Take care of that when
* you implement the value method.
* you implement the values method.
*
* How to use:
*
Expand All @@ -34,7 +34,7 @@ import scala.language.postfixOps
* }}}
* @tparam A The sealed trait
*/
trait Enum[A] {
trait Enum[A <: EnumEntry] {

/**
* The sequence of values for your [[Enum]]. You will typically want
Expand All @@ -57,12 +57,12 @@ trait Enum[A] {
/**
* Map of [[A]] object names to [[A]]s
*/
lazy final val namesToValuesMap: Map[String, A] = values map (v => v.toString -> v) toMap
lazy final val namesToValuesMap: Map[String, A] = values map (v => v.entryName -> v) toMap

/**
* Map of [[A]] object names in lower case to [[A]]s for case-insensitive comparison
*/
lazy final val lowerCaseNamesToValuesMap: Map[String, A] = values map (v => v.toString.toLowerCase -> v) toMap
lazy final val lowerCaseNamesToValuesMap: Map[String, A] = values map (v => v.entryName.toLowerCase -> v) toMap

/**
* Optionally returns an [[A]] for a given name.
Expand All @@ -75,11 +75,11 @@ trait Enum[A] {
def withNameInsensitiveOption(name: String): Option[A] = lowerCaseNamesToValuesMap get name.toLowerCase

/**
* Tries to get an [[A]] by the supplied name. The name corresponds to the .toString
* Tries to get an [[A]] by the supplied name. The name corresponds to the .name
* of the case objects implementing [[A]]
*
* Like [[Enumeration]]'s `withName`, this method will throw if the name does not match any of the values'
* .toString names.
* .entryName values.
*/
def withName(name: String): A =
withNameOption(name) getOrElse (throw new NoSuchElementException(s"$name is not a member of Enum $this"))
Expand Down
18 changes: 18 additions & 0 deletions enumeratum-core/src/main/scala/enumeratum/EnumEntry.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package enumeratum

/**
* Base type for an enum entry for [[Enum]]
*
* By default, the entryName method used for serialising and deseralising Enum values uses
* toString, but feel free to override to fit your needs
*/
trait EnumEntry {

/**
* String representation of this Enum Entry.
*
* Override in your implementation if needed
*/
def entryName: String = toString

}
8 changes: 4 additions & 4 deletions enumeratum-core/src/test/scala/enumeratum/EnumSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class EnumSpec extends FunSpec with Matchers {

it("should fail to compile for unsealed traits") {
"""
trait NotSealed
trait NotSealed extends EnumEntry
object NotSealed extends Enum[NotSealed] {
val values = findValues
Expand All @@ -142,7 +142,7 @@ class EnumSpec extends FunSpec with Matchers {

it("should fail to compile for unsealed abstract classes") {
"""
abstract class Abstract
abstract class Abstract extends EnumEntry
object Abstract extends Enum[Abstract] {
val values = findValues
Expand All @@ -152,7 +152,7 @@ class EnumSpec extends FunSpec with Matchers {

it("should fail to compile for classes") {
"""
class Class
class Class extends EnumEntry
object Class extends Enum[Class] {
val values = findValues
Expand All @@ -162,7 +162,7 @@ class EnumSpec extends FunSpec with Matchers {

it("should fail to compile if the enum is not an object") {
"""
sealed trait Foo
sealed trait Foo extends EnumEntry
class Class extends Enum[Foo] {
val values = findValues
Expand Down
6 changes: 3 additions & 3 deletions enumeratum-core/src/test/scala/enumeratum/Models.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package enumeratum

sealed trait DummyEnum
sealed trait DummyEnum extends EnumEntry

object DummyEnum extends Enum[DummyEnum] {

Expand All @@ -14,7 +14,7 @@ object DummyEnum extends Enum[DummyEnum] {

object Wrapper {

sealed trait SmartEnum
sealed trait SmartEnum extends EnumEntry

object SmartEnum extends Enum[SmartEnum] {

Expand All @@ -29,7 +29,7 @@ object Wrapper {
}

object InTheWoods {
sealed abstract class Mushroom(val toxic: Boolean)
sealed abstract class Mushroom(val toxic: Boolean) extends EnumEntry

object Mushroom extends Enum[Mushroom] {

Expand Down
8 changes: 4 additions & 4 deletions enumeratum-play-json/src/main/scala/enumeratum/Json.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object Json {
* @param enum The enum
* @param insensitive bind in a case-insensitive way, defaults to false
*/
def reads[A](enum: Enum[A], insensitive: Boolean = false): Reads[A] = new Reads[A] {
def reads[A <: EnumEntry](enum: Enum[A], insensitive: Boolean = false): Reads[A] = new Reads[A] {
def reads(json: JsValue): JsResult[A] = json match {
case JsString(s) => {
val maybeBound = if (insensitive) enum.withNameInsensitiveOption(s) else enum.withNameOption(s)
Expand All @@ -29,8 +29,8 @@ object Json {
/**
* Returns a Json writes for a given enum [[Enum]]
*/
def writes[A](enum: Enum[A]): Writes[A] = new Writes[A] {
def writes(v: A): JsValue = JsString(v.toString)
def writes[A <: EnumEntry](enum: Enum[A]): Writes[A] = new Writes[A] {
def writes(v: A): JsValue = JsString(v.entryName)
}

/**
Expand All @@ -39,7 +39,7 @@ object Json {
* @param enum The enum
* @param insensitive bind in a case-insensitive way, defaults to false
*/
def formats[A](enum: Enum[A], insensitive: Boolean = false): Format[A] = {
def formats[A <: EnumEntry](enum: Enum[A], insensitive: Boolean = false): Format[A] = {
Format(reads(enum, insensitive), writes(enum))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package enumeratum

import play.api.libs.json.Format

trait PlayJsonEnum[A] { self: Enum[A] =>
trait PlayJsonEnum[A <: EnumEntry] { self: Enum[A] =>
implicit val jsonFormat: Format[A] = Json.formats(this)
}
2 changes: 1 addition & 1 deletion enumeratum-play-json/src/test/scala/enumeratum/Dummy.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package enumeratum
/**
* Created by Lloyd on 2/4/15.
*/
sealed trait Dummy
sealed trait Dummy extends EnumEntry
object Dummy extends Enum[Dummy] with PlayJsonEnum[Dummy] {
case object A extends Dummy
case object B extends Dummy
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 @@ -19,15 +19,15 @@ object Forms {
* @param enum The enum
* @param insensitive bind in a case-insensitive way, defaults to false
*/
def enum[A](enum: Enum[A], insensitive: Boolean = false): Mapping[A] = PlayForms.of(format(enum, insensitive))
def enum[A <: EnumEntry](enum: Enum[A], insensitive: Boolean = false): Mapping[A] = PlayForms.of(format(enum, insensitive))

/**
* Returns a Formatter for [[Enum]]
*
* @param enum The enum
* @param insensitive bind in a case-insensitive way, defaults to false
*/
private[enumeratum] def format[A](enum: Enum[A], insensitive: Boolean = false): Formatter[A] = new Formatter[A] {
private[enumeratum] 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 @@ -37,7 +37,7 @@ object Forms {
}
}
}
def unbind(key: String, value: A) = Map(key -> value.toString)
def unbind(key: String, value: A) = Map(key -> value.entryName)
}

}
2 changes: 1 addition & 1 deletion enumeratum-play/src/main/scala/enumeratum/PlayEnum.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ package enumeratum
* - formField for doing things like `Form("hello" -> MyEnum.formField)`
*
*/
trait PlayEnum[A] extends Enum[A]
trait PlayEnum[A <: EnumEntry] extends Enum[A]
with PlayJsonEnum[A]
with PlayPathBindableEnum[A]
with PlayQueryBindableEnum[A]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package enumeratum

import play.api.data.Mapping

trait PlayFormFieldEnum[A] { self: Enum[A] =>
trait PlayFormFieldEnum[A <: EnumEntry] { self: Enum[A] =>
val formField: Mapping[A] = Forms.enum(this)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package enumeratum

import play.api.mvc.PathBindable

trait PlayPathBindableEnum[A] { self: Enum[A] =>
trait PlayPathBindableEnum[A <: EnumEntry] { self: Enum[A] =>
implicit val pathBindable: PathBindable[A] = UrlBinders.pathBinder(this)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package enumeratum

import play.api.mvc.QueryStringBindable

trait PlayQueryBindableEnum[A] { self: Enum[A] =>
trait PlayQueryBindableEnum[A <: EnumEntry] { self: Enum[A] =>
implicit val queryBindable: QueryStringBindable[A] = UrlBinders.queryBinder(this)
}
8 changes: 4 additions & 4 deletions enumeratum-play/src/main/scala/enumeratum/UrlBinders.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ object UrlBinders {
* @param enum The enum
* @param insensitive bind in a case-insensitive way, defaults to false
*/
def pathBinder[A](enum: Enum[A], insensitive: Boolean = false): PathBindable[A] = new PathBindable[A] {
def unbind(key: String, value: A): String = value.toString
def pathBinder[A <: EnumEntry](enum: Enum[A], insensitive: Boolean = false): PathBindable[A] = new PathBindable[A] {
def unbind(key: String, value: A): String = value.entryName
def bind(key: String, value: String): Either[String, A] = {
val maybeBound = if (insensitive) enum.withNameInsensitiveOption(value) else enum.withNameOption(value)
maybeBound match {
Expand All @@ -31,10 +31,10 @@ object UrlBinders {
* @param enum The enum
* @param insensitive bind in a case-insensitive way, defaults to false
*/
def queryBinder[A](enum: Enum[A], insensitive: Boolean = false): QueryStringBindable[A] =
def queryBinder[A <: EnumEntry](enum: Enum[A], insensitive: Boolean = false): QueryStringBindable[A] =
new QueryStringBindable[A] {

def unbind(key: String, value: A): String = key + "=" + value.toString
def unbind(key: String, value: A): String = key + "=" + value.entryName

def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, A]] = {
params.get(key).flatMap(_.headOption).map { p =>
Expand Down
2 changes: 1 addition & 1 deletion enumeratum-play/src/test/scala/enumeratum/Dummy.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package enumeratum
/**
* Created by Lloyd on 2/4/15.
*/
sealed trait Dummy
sealed trait Dummy extends EnumEntry
object Dummy extends Enum[Dummy] {
case object A extends Dummy
case object B extends Dummy
Expand Down
2 changes: 1 addition & 1 deletion enumeratum-play/src/test/scala/enumeratum/PlayDummy.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package enumeratum
/**
* Created by Lloyd on 2/4/15.
*/
sealed trait PlayDummy
sealed trait PlayDummy extends EnumEntry

object PlayDummy extends PlayEnum[PlayDummy] {
case object A extends PlayDummy
Expand Down
2 changes: 1 addition & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.typesafe.sbt.SbtGit.{GitKeys => git}

object Enumeratum extends Build {

lazy val theVersion = "1.1.1-SNAPSHOT"
lazy val theVersion = "1.2.0-SNAPSHOT"
lazy val theScalaVersion = "2.11.6"
lazy val scalaVersions = Seq("2.10.5", "2.11.6")

Expand Down

0 comments on commit 98cd9da

Please sign in to comment.