Moved chooseRep into HasReps

This commit is contained in:
Michael Snoyman 2009-12-13 04:05:29 +02:00
parent 77dc6ed78b
commit 4650cf4e92

View File

@ -32,7 +32,6 @@ module Yesod.Rep
, Rep , Rep
, Reps , Reps
, HasReps (..) , HasReps (..)
, chooseRep
-- FIXME TemplateFile or some such... -- FIXME TemplateFile or some such...
-- * Specific types of representations -- * Specific types of representations
, Plain (..) , Plain (..)
@ -111,15 +110,24 @@ type Reps a = [Rep a]
-- one representation for each type. -- one representation for each type.
class HasReps a where class HasReps a where
reps :: Reps a reps :: Reps a
chooseRep :: a -> [ContentType] -> (ContentType, Content)
chooseRep = chooseRep'
instance HasReps [(ContentType, Content)] where instance HasReps [(ContentType, Content)] where
reps = [(TypeOther "FIXME", const $ Content $ cs "FIXME")] reps = error "reps of [(ContentType, Content)]"
chooseRep a cts =
case filter (\(ct, _) -> ct `elem` cts) a of
((ct, c):_) -> (ct, c)
_ -> case a of
(x:_) -> x
_ -> error "chooseRep [(ContentType, Content)] of empty"
-- FIXME done badly, needs cleanup -- FIXME done badly, needs cleanup
chooseRep :: HasReps a chooseRep' :: HasReps a
=> a => a
-> [ContentType] -> [ContentType]
-> (ContentType, Content) -> (ContentType, Content)
chooseRep a ts = chooseRep' a ts =
let choices = rs' ++ rs let choices = rs' ++ rs
helper2 (ct, f) = (ct, f a) helper2 (ct, f) = (ct, f a)
in if null rs in if null rs
@ -146,8 +154,8 @@ instance HasReps HtmlObject where
] ]
#if TEST #if TEST
caseChooseRep :: Assertion caseChooseRepHO :: Assertion
caseChooseRep = do caseChooseRepHO = do
let content = "IGNOREME" let content = "IGNOREME"
a = toHtmlObject content a = toHtmlObject content
htmlbs = Content . cs . unHtmlDoc . cs $ toHtmlObject content htmlbs = Content . cs . unHtmlDoc . cs $ toHtmlObject content
@ -157,8 +165,20 @@ caseChooseRep = do
chooseRep a [TypeHtml, TypeJson] @?= (TypeHtml, htmlbs) chooseRep a [TypeHtml, TypeJson] @?= (TypeHtml, htmlbs)
chooseRep a [TypeOther "foo", TypeJson] @?= (TypeJson, jsonbs) chooseRep a [TypeOther "foo", TypeJson] @?= (TypeJson, jsonbs)
caseChooseRepRaw :: Assertion
caseChooseRepRaw = do
let content = Content $ cs "FOO"
foo = TypeOther "foo"
bar = TypeOther "bar"
hasreps = [(TypeHtml, content), (foo, content)]
chooseRep hasreps [TypeHtml] @?= (TypeHtml, content)
chooseRep hasreps [foo, bar] @?= (foo, content)
chooseRep hasreps [bar, foo] @?= (foo, content)
chooseRep hasreps [bar] @?= (TypeHtml, content)
testSuite :: Test testSuite :: Test
testSuite = testGroup "Yesod.Rep" testSuite = testGroup "Yesod.Rep"
[ testCase "caseChooseRep" caseChooseRep [ testCase "caseChooseRep HtmlObject" caseChooseRepHO
, testCase "caseChooseRep raw" caseChooseRepRaw
] ]
#endif #endif