Skip to content

Instantly share code, notes, and snippets.

@tkawachi

tkawachi/FilterOnly.scala

Last active Aug 29, 2015
Embed
What would you like to do?
import play.api.mvc.{ RequestHeader, EssentialAction, EssentialFilter }
/**
* Apply underlying filter only when f returns true.
*/
class FilterOnly(underlying: EssentialFilter, f: RequestHeader => Boolean) extends EssentialFilter {
override def apply(next: EssentialAction): EssentialAction = new EssentialAction {
private[this] val filteredNext = underlying(next)
override def apply(rh: RequestHeader) = if (f(rh)) filteredNext(rh) else next(rh)
}
}
object FilterOnly {
implicit class EssentialFilterFilterOnlyImplicit(filter: EssentialFilter) {
def only(f: RequestHeader => Boolean) = new FilterOnly(filter, f)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment