Created
March 8, 2016 01:49
-
-
Save luizamboni/989082173d0a19e27120 to your computer and use it in GitHub Desktop.
Eval Route class to Eval and contruct paths for 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 java.util.Arrays; | |
public class Routes { | |
public static void main(String[] args) | |
{ | |
Route editRoute = new Route("admin/user/edit/:id").withMethod("GET").dispatch(ExampleController.class).withAction("edit"); | |
System.out.println(editRoute); | |
System.out.println(editRoute.match("admin/user/edit/34")); | |
System.out.println(editRoute.match("admin/user/show/12")); | |
System.out.println(editRoute.mount("12")); | |
} | |
} | |
public class ExampleController{ | |
public void edit(){ | |
} | |
} | |
public class Route{ | |
protected String path; | |
protected String method; | |
protected Class controller; | |
protected String action; | |
public Route(String path){ | |
this.path = path; | |
} | |
public Route withMethod(String method){ | |
this.method = method; | |
return this; | |
} | |
public Route dispatch(Class controller){ | |
this.controller = controller; | |
return this; | |
} | |
public String mount(String...values){ | |
String mountedPath = this.path; | |
for(String value : Arrays.asList(values)){ | |
System.out.println(value); | |
mountedPath = mountedPath.replaceFirst(":.*/?",value); | |
} | |
return mountedPath; | |
} | |
public Route withAction(String actionName){ | |
this.action = actionName; | |
return this; | |
} | |
@Override | |
public String toString(){ | |
return method + " "+ path + " " +controller.toString()+ "#" + action; | |
} | |
public Boolean match(String path){ | |
System.out.println(path); | |
System.out.println(this.path); | |
System.out.println(this.path.indexOf(":id")); | |
return this.path.regionMatches(0,path,0,this.path.indexOf(":id") ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment