В конце концов (и благодаря предложениям @ Берги) я получил что-то работающее, а затем прибрал:
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
type KeyedValuesOf a = [(String, a)]
-- | Create Metadata from basic list of key-values -- stolen from Hakyll itself.
meta :: Yaml.ToJSON a => KeyedValuesOf a -> Metadata
meta pairs = HMS.fromList [(T.pack k, Yaml.toJSON v) | (k, v) <- pairs]
-- | A 'Reader' class of our own, with a specialised environment
-- for key-value metadata pairs.
newtype MonadMetadataReader a =
MonadMetadataReader {runMonadMetadataReader :: KeyedValuesOf String -> a}
deriving (Functor, Applicative, Monad)
-- | Simple 'Metadata' holder.
-- There are never any matches found,
-- and all metadata (for all items)
-- comes from from the Reader-like environment at setup.
instance MonadMetadata MonadMetadataReader where
getMetadata identifier = MonadMetadataReader meta
getMatches pattern = return []
, что позволило простые модульные тесты, такие как:
it "enables for a value of \"false\"" $ do
let enabled = runMonadMetadataReader (unlessEnabled "key" item) [("key", "false")]
enabled `shouldBe` True
it "disables for a value of \"true\"" $ do
let enabled = runMonadMetadataReader (unlessEnabled "key" item) [("key", "true")]
enabled `shouldBe` False
it "enables if no metadata found" $ do
let enabled = runMonadMetadataReader (unlessEnabled "key" item) []
enabled `shouldBe` True