Created
May 5, 2012 09:09
-
-
Save rirakkumya/2601128 to your computer and use it in GitHub Desktop.
play2でAction内がどんどん入れ子になっていく問題を解決する方法。Action compositionにしてみた
This file contains hidden or 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 controllers | |
import play.api._ | |
import play.api.mvc._ | |
import play.api.cache._ | |
import play.api.Play.current | |
object EA { | |
case class Bind[a1,a2](x:Either[a1,a2]) { | |
def #>>[b](f:a2 => Either[a1,b]):Either[a1,b] = x.right flatMap f | |
} | |
implicit def either2Bind[a](s:Either[Result,a]) = Bind(s) | |
implicit def any2Bind[a](x:a) = Bind[Result,a](Right(x)) | |
def EitherAction( | |
f:Request[AnyContent] => Either[Result,Result] | |
):Action[AnyContent] = Action{f(_) merge} | |
} | |
object Application extends Controller { | |
import EA._ | |
def index(id:String) = EitherAction {req => | |
id #>> { | |
Cache.getAs[String](_) match { | |
case Some(r) if r.startsWith("x") => Right("foo") | |
case Some(r) => Right(r) | |
case None => Left(BadRequest) | |
} | |
} #>> { | |
Cache.getAs[String](_) match { | |
case Some(r) if r == "badcode" => Left(BadRequest) | |
case Some(r) => Right(r) | |
case None => Left(NotFound) | |
} | |
} #>> {x => Right(Ok(x))} | |
} | |
} |
This file contains hidden or 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
GET /:id controllers.Application.index(id) | |
GET /assets/*file controllers.Assets.at(path="/public", file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
コンパイルエラー解決しました!