Skip to content

Commit

Permalink
adding respondsTo
Browse files Browse the repository at this point in the history
  • Loading branch information
alsonkemp committed Feb 10, 2009
1 parent 5a8de78 commit de5565f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
9 changes: 1 addition & 8 deletions App/Controllers/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,5 @@ show = do id' <- getSetting_u "id"
p <- find id'
setViewDataValue "page-title" (title p)
setViewDataValue "page-content" (content p)
handleFormat
respondTo "format" []

handleFormat :: Controller ()
handleFormat = do f <- getSetting "format"
case f of
Nothing -> return ()
Just f' -> do clearLayout
a <- getSetting_u "action"
setSetting "action" $ a ++ f'
2 changes: 2 additions & 0 deletions Turbinado/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module Turbinado.Controller (
module Data.Maybe,

module Config.Master,
module Turbinado.Controller.Routes,
module Turbinado.Environment.CodeStore,
module Turbinado.Environment.Cookie,
module Turbinado.Environment.Header,
Expand Down Expand Up @@ -55,6 +56,7 @@ import Turbinado.Environment.Settings
import Turbinado.Environment.Types
import Turbinado.Environment.ViewData
import Turbinado.Controller.Monad
import Turbinado.Controller.Routes
import Turbinado.Utility.General
import Turbinado.Server.StandardResponse

Expand Down
29 changes: 29 additions & 0 deletions Turbinado/Controller/Routes.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-----------------------------------------------------------------------------
-- |
-- Module : Turbinado.Controller.Routes
-- Copyright : (c) Alson Kemp 2009
-- License : BSD-style (see the file LICENSE)
--
-- Maintainer : Alson Kemp (alson@alsonkemp.com)
-- Stability : experimental
-----------------------------------------------------------------------------
module Turbinado.Controller.Routes (
respondTo
) where

import Data.Maybe
import Turbinado.Environment.Settings
import Turbinado.Controller.Monad

-- | Automates the process of responding to various file formats
respondTo :: String -> [(String, Controller ())] -> Controller ()
respondTo f'' actions = do f' <- getSetting f''
case f' of
Nothing -> error $ "Routes.respondTo: There is no setting called '" ++ f'' ++ "' for me to respondTo"
Just f -> do let a' = lookup f actions
clearLayout
oldAction <- getSetting_u "action"
setSetting "action" (oldAction ++ f)
case a' of
Nothing -> return ()
Just a -> do a
2 changes: 1 addition & 1 deletion Turbinado/Environment/Routes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ runRoutes = do debugM $ " Routes.runRoutes : starting"
let Routes rs = fromJust' "Routes : runRoutes : getRoutes" $ getRoutes e
r = fromJust' "Routes : runRoutes : getRequest" $ getRequest e
p = URI.uriPath $ HTTP.rqURI r
sets = msum $ map (\(r, k) -> maybe [] (zip k) (matchRegex r p)) rs
sets = head $ filter (not . null) $ map (\(r, k) -> maybe [] (zip k) (matchRegex r p)) rs
case sets of
[] -> throwController $ ParameterLookupFailed $ "No routes matched for " ++ p
_ -> do mapM (\(k, v) -> setSetting k v) sets
Expand Down
1 change: 0 additions & 1 deletion Turbinado/Environment/Settings.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ getSetting_u s = do v <- getSetting s
-- 'show' to convert to a String).
setSetting :: (HasEnvironment m, Typeable a) => String -> a -> m ()
setSetting k v = do e <- getEnvironment
debugM $ " setSetting : " ++ k
setEnvironment $ e { getSettings = Just (M.insert k (toDyn v) (fromJust' "Settings : setSetting" $ getSettings e))}

-- | Unsets a setting. If the key does not exist, no error is thrown.
Expand Down

0 comments on commit de5565f

Please sign in to comment.