Permalink
Browse files

Added initRest convenience function

  • Loading branch information...
1 parent 2e0bc3b commit 50458bbcb700233c6aaddfa5a793e7a775850bbb @mightybyte mightybyte committed Aug 21, 2012
Showing with 16 additions and 1 deletion.
  1. +16 −1 src/Snap/Restful.hs
View
@@ -6,6 +6,7 @@ module Snap.Restful
, Resource (..)
, DBId (..)
+ , initRest
, resourceRouter
, resourceRoutes
@@ -24,6 +25,7 @@ module Snap.Restful
, resourceSplices
, itemSplices
+ , itemSplices'
, redirToItem
) where
@@ -84,6 +86,19 @@ data Resource b v a = Resource {
instance Default (Resource b v a) where
def = Resource "items" "/items" [] [] []
+
+------------------------------------------------------------------------------
+-- | One-stop convenience function to enable RESTful resources in your
+-- application. Call this function from your initializer passing it all of
+-- your resources and it will add the routes and splices for you.
+initRest :: HasHeist b => [Resource b v ()] -> Initializer b v ()
+initRest resources = do
+ let splices = concatMap resourceSplices resources
+ routes = concatMap resourceRoutes resources
+ addSplices splices
+ addRoutes routes
+
+
-------------------------------------------------------------------------------
resourceRoutes :: Resource b v a -> [(ByteString, Handler b v a)]
resourceRoutes r@Resource{..} =
@@ -196,7 +211,7 @@ rootPath = indexPath
-------------------------------------------------------------------------------
-resourceSplices :: Resource b v a -> [(Text, SnapletSplice b b)]
+resourceSplices :: Resource b v a -> [(Text, SnapletSplice b v)]
resourceSplices r@Resource{..} =
[ (T.concat [rName, "NewPath"], liftHeist . textSplice $ newPath r def)
, (T.concat [rName, "IndexPath"], liftHeist . textSplice $ indexPath r def)

0 comments on commit 50458bb

Please sign in to comment.