Created
November 23, 2012 07:47
-
-
Save phillip-haydon/4134416 to your computer and use it in GitHub Desktop.
Granular Route Extensions for Nancy
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
public class HomeModule : NancyModule | |
{ | |
public HomeModule() | |
{ | |
this.RequireClaim("stuff") | |
.For("GET") | |
.On("/test/{claim}") | |
.With(o => | |
{ | |
return "YAY"; | |
}); | |
} | |
public RouteBuilder RegisterRoute(string verb) | |
{ | |
return new RouteBuilder(verb, this); | |
} | |
} | |
public class Builder : IForVerb, IOnRoute, IWithImplementation | |
{ | |
public Builder(NancyModule module) | |
{ | |
Module = module; | |
} | |
public string Claim { get; set; } | |
private string ForVerb { get; set; } | |
private string ForRoute { get; set; } | |
private NancyModule Module { get; set; } | |
public IOnRoute For(string verb) | |
{ | |
ForVerb = verb; | |
return this; | |
} | |
public IWithImplementation On(string route) | |
{ | |
ForRoute = route; | |
return this; | |
} | |
public void With(Func<dynamic, dynamic> impl) | |
{ | |
((HomeModule)Module).RegisterRoute(ForVerb)[ForRoute] = _ => | |
{ | |
if (_.claim != "stuff") | |
{ | |
return "fail"; | |
} | |
return impl(_); | |
}; | |
} | |
} | |
public static class Extensions | |
{ | |
public static IForVerb RequireClaim(this NancyModule module, string claim) | |
{ | |
return new Builder(module) | |
{ | |
Claim = claim | |
}; | |
} | |
} | |
public interface IForVerb | |
{ | |
IOnRoute For(string verb); | |
} | |
public interface IOnRoute | |
{ | |
IWithImplementation On(string route); | |
} | |
public interface IWithImplementation | |
{ | |
void With(Func<dynamic, dynamic> impl); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment