diff options
author | Jesse Luehrs <doy@tozt.net> | 2013-02-14 15:52:04 -0600 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2013-02-14 17:44:27 -0600 |
commit | edb531391b56eb964e4cfe513df9f4ac5e6fbcf4 (patch) | |
tree | eacc1a01d2cc3bbfcf55e5586d9402a3d931ba59 | |
parent | ee54a6bcf88208d01af0cb6501304f68f647c467 (diff) | |
download | scala-path-router-edb531391b56eb964e4cfe513df9f4ac5e6fbcf4.tar.gz scala-path-router-edb531391b56eb964e4cfe513df9f4ac5e6fbcf4.zip |
helper method to get a real path out of a route
-rw-r--r-- | src/main/scala/router.scala | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/main/scala/router.scala b/src/main/scala/router.scala index eece34d..2bf8bfd 100644 --- a/src/main/scala/router.scala +++ b/src/main/scala/router.scala @@ -92,6 +92,20 @@ class Route[T] ( override def toString = path + def pathWithMapping (mapping: Map[String, String]): Option[String] = { + if (requiredVariableComponents.forall(mapping.isDefinedAt)) { + val boundComponents = components.flatMap { + case Optional(v) => (mapping get v).map(validComponentValue(v, _)) + case Variable(v) => validComponentValue(v, (mapping(v))) + case literal => Some(literal) + } + Some(boundComponents.mkString("/")) + } + else { + None + } + } + private lazy val components = path.split("/").filter(_.length > 0) @@ -124,6 +138,13 @@ class Route[T] ( case Some(rx) => rx.findFirstIn(component).nonEmpty case None => true } + + private def validComponentValue ( + name: String, + component: String + ): Option[String] = { + if (validate(name, component)) { Some(component) } else { None } + } } class Match[T] ( |