Documentation.

Portnov [2009-07-05 11:51:56]
Documentation.
Filename
Framework/README.ru
Framework/Types.hs
diff --git a/Framework/README.ru b/Framework/README.ru
index 9e8696c..b05874e 100644
--- a/Framework/README.ru
+++ b/Framework/README.ru
@@ -12,3 +12,5 @@
  * EDSL для формирования SQL-запросов
  * Templating Engine - шаблоны пишутся в отдельных файлах с синтаксисом a la Django, но при сборке приложения компилируются в результирующий бинарник
  * Подсистема обработки форм (генерация HTML формы по объекту, валидация форм, показ недозаполненной формы)
+
+Для соединения с БД используется пул соединений, тот же модуль используется для соединений с cache backend.
diff --git a/Framework/Types.hs b/Framework/Types.hs
index ab7d5e4..fb40b5b 100644
--- a/Framework/Types.hs
+++ b/Framework/Types.hs
@@ -26,31 +26,31 @@ instance Show DBConnection where
 -------------------------------------------------------------------------------------------

 data StaticConfig = HP {
-    docdir :: String,
-    hLog :: Handle,
-    dbDriver :: String,
-    dbPath :: String,
-    cacheDriver :: String,
-    cachePath :: String,
-    sessionsDriver :: String,
-    sessionsPath :: String,
-    dbpool :: MPool DBConnection,
-    cpool :: MPool CacheConnection
+    docdir :: String,              -- ^ Static content directory
+    hLog :: Handle,                -- ^ Handle to log
+    dbDriver :: String,            -- ^ DB backend name
+    dbPath :: String,              -- ^ Info for DB backend
+    cacheDriver :: String,         -- ^ Cache backend name
+    cachePath :: String,           -- ^ Info for cache backend
+    sessionsDriver :: String,      -- ^ Sessions backend name
+    sessionsPath :: String,        -- ^ Info for sessions backend
+    dbpool :: MPool DBConnection,  -- ^ DB connections pool
+    cpool :: MPool CacheConnection -- ^ Cache connections pool
     }
     deriving (Show)

 -- | Runtime controller action configuration
 data ActionConfig = ActionConfig {
-    request      :: Request,                    -- ^ HTTP request
+    request      :: Request,                      -- ^ HTTP request
     httpParams   :: StaticConfig,                 -- ^ Static (global) configuration
-    dbconnection :: DBConnection,             -- ^ DB connection
-    sessionID    :: SessionID,               -- ^ Current HTTP session ID
-    sessionMap   :: SessionMap,              -- ^ Contains session variables
-    sessionsBackend :: SessionsConnection,   -- ^ Connection to sessions backend
-    cacheBackend :: CacheConnection,            -- ^ Connection to cache backend
-    cookiesExp   :: String,                            -- ^ Cookies expiration date
-    dbpoolIndex  :: Int,
-    cpoolIndex   :: Int
+    dbconnection :: DBConnection,                 -- ^ DB connection
+    sessionID    :: SessionID,                    -- ^ Current HTTP session ID
+    sessionMap   :: SessionMap,                   -- ^ Contains session variables
+    sessionsBackend :: SessionsConnection,        -- ^ Connection to sessions backend
+    cacheBackend :: CacheConnection,              -- ^ Connection to cache backend
+    cookiesExp   :: String,                       -- ^ Cookies expiration date
+    dbpoolIndex  :: Int,                          -- ^ Index of DB connection in pool
+    cpoolIndex   :: Int                           -- ^ Index of cache connection in pool
     }
     deriving (Show)

@@ -103,20 +103,33 @@ packHeader (n,v) = (n =: v)

 -------------------------------------------------------------------------------------------

+-- | Single item to render in template.
 class TemplateOne a where
-    showO :: a -> String
-    intField :: Int -> a -> Int
-    stringField :: Int -> a -> String
-    boolField :: Int -> a -> Bool
-
+    -- | Show
+    showO :: a -> String
+    -- | Get n'th integer field
+    intField :: Int -> a -> Int
+    -- | Get n'th string field
+    stringField :: Int -> a -> String
+    -- | Get n'th boolean field
+    boolField :: Int -> a -> Bool
+
+-- | Multiple-valued item to render in template.
 class (TemplateOne a) => TemplateItem a where
-    showT :: a -> String
-    intFields :: Int -> a -> [Int]
-    stringFields :: Int -> a -> [String]
-    boolFields :: Int -> a -> [Bool]
-    mkList :: a -> [TContainer]
-    isTrue :: a -> Bool
-
+    -- | Show
+    showT :: a -> String
+    -- | Get list of n'th integer fields in all items
+    intFields :: Int -> a -> [Int]
+    -- | Get list of n'th string fields in all items
+    stringFields :: Int -> a -> [String]
+    -- | Get list of n'th boolean fields in all items
+    boolFields :: Int -> a -> [Bool]
+    -- | Convert to a list
+    mkList :: a -> [TContainer]
+    -- | Check whether this item equivalent to True
+    isTrue :: a -> Bool
+
+-- | Show TContainer
 showC :: TContainer -> String
 showC (C x) = showT x

@@ -168,31 +181,52 @@ instance TemplateItem String where
   mkList = error "undefined mkList for String"
   isTrue = not.null

+-- | Container type for any `renderable` value
 data TContainer = forall a. (TemplateItem a) => C a
+
+-- | String function of "TContainer"
 type SFunction = forall a. (TemplateItem a) => a -> String
+
+-- | Boolean function of "TContainer"
 type BFunction = forall a. (TemplateItem a) => a -> Bool

-mapF :: String -> (M.Map String TContainer -> String) -> M.Map String TContainer -> TContainer -> String
+-- | Apply given function (render) for each item in the list (contained in TContainer).
+-- Used in Templates.
+mapF :: String                               -- ^ Name of list-item variable
+     -> (M.Map String TContainer -> String)  -- ^ Rendering function
+     -> M.Map String TContainer              -- ^ Current context (variables)
+     -> TContainer                           -- ^ A list to iterate
+     -> String
 mapF k f s (C lst) = concat $ map f [M.insert "it" (C it) $ M.insert k v s | (it,v) <- zip ([1..]::[Int]) (mkList lst)]

-tmap' :: SFunction -> TContainer -> String
-tmap' f (C x) = f x
-
+-- | Apply "SFunction" to content of TContainer
 tmap :: SFunction -> Maybe TContainer -> String
 tmap f x = maybe "" id $ (tmap' f) `fmap` x
+    where tmap' :: SFunction -> TContainer -> String
+          tmap' f (C x) = f x

-bmap' :: BFunction -> TContainer -> Bool
-bmap' f (C x) = f x
-
+-- | Apply "BFunction" to content of TContainer
 bmap :: BFunction -> Maybe TContainer -> Bool
 bmap f x = maybe False id $ (bmap' f) `fmap` x
+    where bmap' :: BFunction -> TContainer -> Bool
+          bmap' f (C x) = f x

+transformInts ::  (TemplateOne a) => Int -> (Int -> b) -> a -> [b]
 transformInts    n f = \x -> f `map` (intFields    n x)
+
+transformStrings :: (TemplateOne a) => Int -> (String -> b) -> a -> [b]
 transformStrings n f = \x -> f `map` (stringFields n x)
+
+transformBools ::  (TemplateOne a) => Int -> (Bool -> b) -> a -> [b]
 transformBools   n f = \x -> f `map` (boolFields   n x)

+transformInt ::  (TemplateOne a) => Int -> (Int -> t) -> a -> t
 transformInt    n f = \x -> f (intField    n x)
+
+transformString :: (TemplateOne a) => Int -> (String -> t) -> a -> t
 transformString n f = \x -> f (stringField n x)
+
+transformBool ::  (TemplateOne a) => Int -> (Bool -> t) -> a -> t
 transformBool   n f = \x -> f (boolField   n x)
 -------------------------------------------------------------------------------------------
ViewGit