Created
March 11, 2012 22:13
-
-
Save danieldietrich/2018403 to your computer and use it in GitHub Desktop.
Url Path Binding w/o Boilerplate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models | |
object Account extends BindableEnum { | |
val Company, Sales = Value // create enumerated values | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models | |
import play.api.mvc.PathBindable | |
abstract class BindableEnum extends Enumeration { | |
implicit def bindableEnum = new PathBindable[Value] { | |
def bind(key: String, value: String) = | |
values.find(_.toString.toLowerCase == value.toLowerCase) match { | |
case Some(v) => Right(v) | |
case None => Left("Unknown url path segment '" + value + "'") | |
} | |
def unbind(key: String, value: Value) = value.toString.toLowerCase | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment