more ledger-handling-functions cleanup

This commit is contained in:
Simon Michael 2008-10-01 12:48:52 +00:00
parent 9ea32d3f13
commit 8b117e1581
3 changed files with 13 additions and 13 deletions

View File

@ -38,8 +38,8 @@ instance Show Ledger where
-- 1. filter based on account/description patterns, if any -- 1. filter based on account/description patterns, if any
-- 2. cache per-account info -- 2. cache per-account info
-- also, figure out the precision(s) to use -- also, figure out the precision(s) to use
cacheLedger :: (Regex,Regex) -> LedgerFile -> Ledger cacheLedger :: LedgerFile -> (Regex,Regex) -> Ledger
cacheLedger pats l = cacheLedger l pats =
let let
lprecision = maximum $ map (precision . amount) $ rawLedgerTransactions l lprecision = maximum $ map (precision . amount) $ rawLedgerTransactions l
l' = filterLedgerEntries pats l l' = filterLedgerEntries pats l

View File

@ -284,7 +284,7 @@ ledger7 = LedgerFile
] ]
"" ""
l7 = cacheLedger (parsePatternArgs []) ledger7 l7 = cacheLedger ledger7 (wildcard,wildcard)
timelogentry1_str = "i 2007/03/11 16:19:00 hledger\n" timelogentry1_str = "i 2007/03/11 16:19:00 hledger\n"
timelogentry1 = TimeLogEntry 'i' "2007/03/11 16:19:00" "hledger" timelogentry1 = TimeLogEntry 'i' "2007/03/11 16:19:00" "hledger"
@ -375,7 +375,7 @@ test_ledgerAccountNames =
(rawLedgerAccountNames ledger7) (rawLedgerAccountNames ledger7)
test_cacheLedger = test_cacheLedger =
assertEqual' 15 (length $ Map.keys $ accounts $ cacheLedger (parsePatternArgs []) ledger7) assertEqual' 15 (length $ Map.keys $ accounts $ cacheLedger ledger7 (wildcard,wildcard))
test_showLedgerAccounts = test_showLedgerAccounts =
assertEqual' 4 (length $ lines $ showLedgerAccounts l7 1) assertEqual' 4 (length $ lines $ showLedgerAccounts l7 1)

View File

@ -69,15 +69,15 @@ parseLedgerAndDo :: [Flag] -> (Regex,Regex) -> (Ledger -> IO ()) -> IO ()
parseLedgerAndDo opts pats cmd = do parseLedgerAndDo opts pats cmd = do
path <- ledgerFilePath opts path <- ledgerFilePath opts
parsed <- parseLedgerFile path parsed <- parseLedgerFile path
doWithParsedLedger pats cmd parsed withParsedLedgerOrErrorDo parsed pats cmd
doWithParsedLedger :: (Regex,Regex) -> (Ledger -> IO ()) -> (Either ParseError LedgerFile) -> IO () withParsedLedgerOrErrorDo :: (Either ParseError LedgerFile) -> (Regex,Regex) -> (Ledger -> IO ()) -> IO ()
doWithParsedLedger pats cmd parsed = do withParsedLedgerOrErrorDo parsed pats cmd = do
case parsed of Left e -> parseError e case parsed of Left err -> parseError err
Right l -> cacheLedgerAndDo pats l cmd Right l -> cacheLedgerAndDo l pats cmd
cacheLedgerAndDo :: (Regex,Regex) -> LedgerFile -> (Ledger -> IO ()) -> IO () cacheLedgerAndDo :: LedgerFile -> (Regex,Regex) -> (Ledger -> IO ()) -> IO ()
cacheLedgerAndDo pats l cmd = do cmd $ cacheLedger pats l cacheLedgerAndDo l pats cmd = do cmd $ cacheLedger l pats
type Command = [Flag] -> (Regex,Regex) -> IO () type Command = [Flag] -> (Regex,Regex) -> IO ()
@ -124,14 +124,14 @@ myledger :: IO Ledger
myledger = do myledger = do
parsed <- ledgerFilePath [] >>= parseLedgerFile parsed <- ledgerFilePath [] >>= parseLedgerFile
let ledgerfile = either (\_ -> LedgerFile [] [] [] "") id parsed let ledgerfile = either (\_ -> LedgerFile [] [] [] "") id parsed
return $ cacheLedger (wildcard,wildcard) ledgerfile return $ cacheLedger ledgerfile (wildcard,wildcard)
-- | return a Ledger parsed from the given file path -- | return a Ledger parsed from the given file path
ledgerfromfile :: String -> IO Ledger ledgerfromfile :: String -> IO Ledger
ledgerfromfile f = do ledgerfromfile f = do
parsed <- ledgerFilePath [File f] >>= parseLedgerFile parsed <- ledgerFilePath [File f] >>= parseLedgerFile
let ledgerfile = either (\_ -> LedgerFile [] [] [] "") id parsed let ledgerfile = either (\_ -> LedgerFile [] [] [] "") id parsed
return $ cacheLedger (wildcard,wildcard) ledgerfile return $ cacheLedger ledgerfile (wildcard,wildcard)
accountnamed :: AccountName -> IO Account accountnamed :: AccountName -> IO Account
accountnamed a = myledger >>= (return . fromMaybe nullacct . Map.lookup a . accounts) accountnamed a = myledger >>= (return . fromMaybe nullacct . Map.lookup a . accounts)