Skip to content

Instantly share code, notes, and snippets.

@eed3si9n
Created July 17, 2011 10:54
Show Gist options
  • Save eed3si9n/1087455 to your computer and use it in GitHub Desktop.
Save eed3si9n/1087455 to your computer and use it in GitHub Desktop.
trait CanWhere[A] {
def apply(f: PartialFunction[A, A]): Selector[Node]
}
implicit val stringToCanWhere = new CanWhere[String] {
def apply(f: PartialFunction[String, String]) =
Selector({ case Elem(p, name, a, s, c) if f.isDefinedAt(name) => Elem(p, f(name), a, s, c) })
}
implicit val nodeToCanWhere = new CanWhere[Node] {
def apply(f: PartialFunction[Node, Node]) =
Selector({ case e: Elem if f.isDefinedAt(e) => f(e) })
}
/**
* Converts [[PartialFunction]]`[A, A]` into an instance of [[com.codecommit.antixml.Selector]]
* which can then be passed to the appropriate methods on [[com.codecommit.antixml.Group]].
* For example: `ns \ where { case "name" => "alternative" }`
*/
def where[A: CanWhere](f: PartialFunction[A, A]): Selector[Node] = implicitly[CanWhere[A]].apply(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment