Created
January 16, 2012 14:52
-
-
Save ripper234/1621212 to your computer and use it in GitHub Desktop.
Fluently build Play! routes
This file contains 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
import play.mvc.Controller; | |
import play.mvc.Router; | |
import java.util.Map; | |
import static com.google.common.base.Preconditions.checkArgument; | |
import static com.google.common.collect.Maps.newHashMap; | |
public class RouteBuilder { | |
private final Class<? extends Controller> controller; | |
private final Map<String, Object> params = newHashMap(); | |
private final String action; | |
public RouteBuilder(Class<? extends Controller> controller, String action) { | |
this.controller = controller; | |
this.action = action; | |
} | |
public String route() { | |
return Router.reverse(controller.getName() + "." + action, params).url; | |
} | |
public RouteBuilder addParam(String key, Object value) { | |
checkArgument(!params.containsKey(key)); | |
params.put(key, value); | |
return this; | |
} | |
} |
This file contains 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
new RouteBuilder(Application.class, "index") | |
.addParam("name", "Ron") | |
.addParam("age", 30) | |
.route() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment