summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2013-02-28 23:28:55 -0600
committerJesse Luehrs <doy@tozt.net>2013-02-28 23:28:55 -0600
commit64e67cfd70f18b67948fd3f7b03e39af9b5d31c3 (patch)
treeff492011ad73bb7c93d8b1537e6d34b86d18a88f
parent457495c0ae63258c7d86b48c07b719565233e7a7 (diff)
downloadscala-path-router-64e67cfd70f18b67948fd3f7b03e39af9b5d31c3.tar.gz
scala-path-router-64e67cfd70f18b67948fd3f7b03e39af9b5d31c3.zip
cleanups
-rw-r--r--.gitignore3
-rw-r--r--src/main/scala/org/perl8/router/Router.scala (renamed from src/main/scala/router.scala)67
-rw-r--r--src/test/scala/org/perl8/router/BasicTest.scala (renamed from src/test/scala/basic.scala)7
-rw-r--r--src/test/scala/org/perl8/router/OptionalTest.scala (renamed from src/test/scala/optional.scala)7
-rw-r--r--src/test/scala/org/perl8/router/test.scala (renamed from src/test/scala/helpers.scala)4
5 files changed, 32 insertions, 56 deletions
diff --git a/.gitignore b/.gitignore
index 007798c..2f7896d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1 @@
-/target
-/project
+target/
diff --git a/src/main/scala/router.scala b/src/main/scala/org/perl8/router/Router.scala
index 4a9f446..c169b28 100644
--- a/src/main/scala/router.scala
+++ b/src/main/scala/org/perl8/router/Router.scala
@@ -1,4 +1,4 @@
-package router
+package org.perl8.router
import scala.collection.mutable.ListBuffer
import scala.util.matching.Regex
@@ -7,29 +7,19 @@ class Router[T] {
def addRoute (
path: String,
target: T,
- defaults: Map[String, String] = Map(),
- validations: Map[String, Regex] = Map()
+ defaults: Map[String, String] = Map.empty,
+ validations: Map[String, Regex] = Map.empty
) {
routes += new Route(path, defaults, validations, target)
}
def route (path: String): Option[Match[T]] = {
- def testRoutes (
- components: Seq[String],
- routes: List[Route]
- ): Option[Match[T]] = routes match {
- case r :: rs => r.route(components) match {
- case Some(found) => Some(found)
- case None => testRoutes(components, rs)
- }
- case _ => None
- }
val components = path.split("/").filter {
case "" => false
case "." => false // XXX do we want to keep this?
case _ => true
}
- testRoutes(components, routes.toList)
+ routes.view.flatMap(r => r.route(components)).headOption
}
def uriFor (mapping: Map[String, String]): Option[String] = {
@@ -40,12 +30,9 @@ class Router[T] {
// pass the validation for that component, it can't match
// - if the route contains a default value, and that component also exists
// in the mapping, then the values must match
- val possible = routes.toList.flatMap(r => {
- r.pathWithMapping(mapping) match {
- case Some(path) => Some(r -> path)
- case None => None
- }
- })
+ val possible = routes.toList.flatMap { r =>
+ r.pathWithMapping(mapping).map(p => r -> p)
+ }
possible match {
case Nil => None
@@ -76,11 +63,9 @@ class Router[T] {
val validations: Map[String, Regex],
val target: T
) {
- import Route._
-
lazy val variables = components.flatMap(getVariableName)
- def route(
+ def route (
parts: Seq[String],
components: Seq[String] = components,
mapping: Map[String, String] = defaults
@@ -128,15 +113,12 @@ class Router[T] {
val boundComponents = components.flatMap {
case Optional(v) => {
val component = (mapping get v).flatMap(validComponentValue(v, _))
- defaults get v match {
- case Some(default) => {
- component.flatMap {
- case `default` => None
- case c => Some(c)
- }
+ defaults.get(v).map { default =>
+ component.flatMap {
+ case `default` => None
+ case c => Some(c)
}
- case None => component
- }
+ }.getOrElse(component)
}
case Variable(v) => validComponentValue(v, (mapping(v)))
case literal => Some(literal)
@@ -168,31 +150,24 @@ class Router[T] {
}
private def validate (name: String, component: String) =
- validations get name match {
- case Some(rx) => rx.findFirstIn(component).nonEmpty
- case None => true
- }
+ validations.get(name).forall(_.findFirstIn(component).nonEmpty)
private def validComponentValue (name: String, component: String) =
- if (validate(name, component)) { Some(component) } else { None }
- }
+ if (validate(name, component)) Some(component) else None
- private object Route {
private val Optional = """^\?:(.*)$""".r
private val Variable = """^\??:(.*)$""".r
}
- private class AmbiguousRouteMapping(
+ private class AmbiguousRouteMapping (
mapping: Map[String, String],
paths: Seq[String]
- ) extends RuntimeException {
- override def getMessage (): String = {
- "Ambiguous path descriptor (specified keys " +
- mapping.keys.mkString(", ") +
- "): could match paths " +
+ ) extends RuntimeException(
+ "Ambiguous path descriptor (specified keys " +
+ mapping.keys.mkString(", ") +
+ "): could match paths " +
paths.mkString(", ")
- }
- }
+ )
}
class Match[T] (
diff --git a/src/test/scala/basic.scala b/src/test/scala/org/perl8/router/BasicTest.scala
index 56655e4..b00d1ae 100644
--- a/src/test/scala/basic.scala
+++ b/src/test/scala/org/perl8/router/BasicTest.scala
@@ -1,9 +1,10 @@
+package org.perl8.router
+
import org.scalatest.FunSuite
-import router.test._
-import router.Router
+import org.perl8.router.test._
-class Basic extends FunSuite {
+class BasicTest extends FunSuite {
val yearRx = """\d{4}""".r
val monthRx = """\d|10|11|12""".r
val dayRx = """\d|[12]\d|30|31""".r
diff --git a/src/test/scala/optional.scala b/src/test/scala/org/perl8/router/OptionalTest.scala
index ade237c..1073487 100644
--- a/src/test/scala/optional.scala
+++ b/src/test/scala/org/perl8/router/OptionalTest.scala
@@ -1,9 +1,10 @@
+package org.perl8.router
+
import org.scalatest.FunSuite
-import router.test._
-import router.Router
+import org.perl8.router.test._
-class Optional extends FunSuite {
+class OptionalTest extends FunSuite {
val router = new Router[Boolean]
router addRoute (
diff --git a/src/test/scala/helpers.scala b/src/test/scala/org/perl8/router/test.scala
index f2f561c..e1fff79 100644
--- a/src/test/scala/helpers.scala
+++ b/src/test/scala/org/perl8/router/test.scala
@@ -1,11 +1,11 @@
-package router
+package org.perl8.router
object test {
import language.implicitConversions
class RouterHelperOps[T] (router: Router[T]) {
private def assert (condition: Boolean, msg: String) = {
- if (condition) { None } else { Some(msg) }
+ if (condition) None else Some(msg)
}
def matches (path: String) = {