consistent lower-case aliases for working with Ledgers, and examples

This commit is contained in:
Simon Michael 2009-04-04 22:38:18 +00:00
parent 1572622ae6
commit 7d1c01ec8a
3 changed files with 86 additions and 13 deletions

View File

@ -127,7 +127,7 @@ showBalanceReport opts args l = acctsstr ++ totalstr
| not (Empty `elem` opts) && isZeroMixedAmount total = "" | not (Empty `elem` opts) && isZeroMixedAmount total = ""
| otherwise = printf "--------------------\n%s\n" $ padleft 20 $ showMixedAmount total | otherwise = printf "--------------------\n%s\n" $ padleft 20 $ showMixedAmount total
where where
total = sum $ map abalance $ topAccounts l total = sum $ map abalance $ ledgerTopAccounts l
-- | Display one line of the balance report with appropriate indenting and eliding. -- | Display one line of the balance report with appropriate indenting and eliding.
showInterestingAccount :: Ledger -> [AccountName] -> AccountName -> String showInterestingAccount :: Ledger -> [AccountName] -> AccountName -> String
@ -157,5 +157,5 @@ isInteresting opts l a
numinterestingsubs = length $ filter isInterestingTree subtrees numinterestingsubs = length $ filter isInterestingTree subtrees
where where
isInterestingTree t = treeany (isInteresting opts l . aname) t isInterestingTree t = treeany (isInteresting opts l . aname) t
subtrees = map (fromJust . ledgerAccountTreeAt l) $ subAccounts l $ ledgerAccount l a subtrees = map (fromJust . ledgerAccountTreeAt l) $ ledgerSubAccounts l $ ledgerAccount l a

View File

@ -13,6 +13,42 @@ contains:
- the full text of the journal file, when available - the full text of the journal file, when available
This is the main object you'll deal with as a user of the Ledger
library. The most useful functions also have shorter, lower-case
aliases for easier interaction. Here's an example:
> > import Ledger
> > l <- readLedger "sample.ledger"
> > accountnames l
> ["assets","assets:bank","assets:bank:checking","assets:bank:saving",...
> > accounts l
> [Account assets with 0 txns and $-1 balance,Account assets:bank with...
> > topaccounts l
> [Account assets with 0 txns and $-1 balance,Account expenses with...
> > account l "assets"
> Account assets with 0 txns and $-1 balance
> > accountsmatching ["ch"] l
> accountsmatching ["ch"] l
> [Account assets:bank:checking with 4 txns and $0 balance]
> > subaccounts l (account l "assets")
> subaccounts l (account l "assets")
> [Account assets:bank with 0 txns and $1 balance,Account assets:cash...
> > head $ transactions l
> 2008/01/01 income assets:bank:checking $1 RegularPosting
> > accounttree 2 l
> Node {rootLabel = Account top with 0 txns and 0 balance, subForest = [...
> > accounttreeat l (account l "assets")
> Just (Node {rootLabel = Account assets with 0 txns and $-1 balance, ...
> > datespan l
> DateSpan (Just 2008-01-01) (Just 2009-01-01)
> > rawdatespan l
> DateSpan (Just 2008-01-01) (Just 2009-01-01)
> > ledgeramounts l
> [$1,$-1,$1,$-1,$1,$-1,$1,$1,$-2,$1,$-1]
> > commodities l
> [Commodity {symbol = "$", side = L, spaced = False, comma = False, ...
-} -}
module Ledger.Ledger module Ledger.Ledger
@ -96,28 +132,28 @@ filtertxns :: [String] -> [Transaction] -> [Transaction]
filtertxns apats ts = filter (matchpats apats . taccount) ts filtertxns apats ts = filter (matchpats apats . taccount) ts
-- | List a ledger's account names. -- | List a ledger's account names.
accountnames :: Ledger -> [AccountName] ledgerAccountNames :: Ledger -> [AccountName]
accountnames l = drop 1 $ flatten $ accountnametree l ledgerAccountNames l = drop 1 $ flatten $ accountnametree l
-- | Get the named account from a ledger. -- | Get the named account from a ledger.
ledgerAccount :: Ledger -> AccountName -> Account ledgerAccount :: Ledger -> AccountName -> Account
ledgerAccount l a = (accountmap l) ! a ledgerAccount l a = (accountmap l) ! a
-- | List a ledger's accounts, in tree order -- | List a ledger's accounts, in tree order
accounts :: Ledger -> [Account] ledgerAccounts :: Ledger -> [Account]
accounts l = drop 1 $ flatten $ ledgerAccountTree 9999 l ledgerAccounts l = drop 1 $ flatten $ ledgerAccountTree 9999 l
-- | List a ledger's top-level accounts, in tree order -- | List a ledger's top-level accounts, in tree order
topAccounts :: Ledger -> [Account] ledgerTopAccounts :: Ledger -> [Account]
topAccounts l = map root $ branches $ ledgerAccountTree 9999 l ledgerTopAccounts l = map root $ branches $ ledgerAccountTree 9999 l
-- | Accounts in ledger whose name matches the pattern, in tree order. -- | Accounts in ledger whose name matches the pattern, in tree order.
accountsMatching :: [String] -> Ledger -> [Account] ledgerAccountsMatching :: [String] -> Ledger -> [Account]
accountsMatching pats l = filter (matchpats pats . aname) $ accounts l ledgerAccountsMatching pats l = filter (matchpats pats . aname) $ accounts l
-- | List a ledger account's immediate subaccounts -- | List a ledger account's immediate subaccounts
subAccounts :: Ledger -> Account -> [Account] ledgerSubAccounts :: Ledger -> Account -> [Account]
subAccounts l Account{aname=a} = ledgerSubAccounts l Account{aname=a} =
map (ledgerAccount l) $ filter (`isSubAccountNameOf` a) $ accountnames l map (ledgerAccount l) $ filter (`isSubAccountNameOf` a) $ accountnames l
-- | List a ledger's transactions. -- | List a ledger's transactions.
@ -140,3 +176,40 @@ ledgerDateSpan l
| otherwise = DateSpan (Just $ tdate $ head ts) (Just $ addDays 1 $ tdate $ last ts) | otherwise = DateSpan (Just $ tdate $ head ts) (Just $ addDays 1 $ tdate $ last ts)
where where
ts = sortBy (comparing tdate) $ ledgerTransactions l ts = sortBy (comparing tdate) $ ledgerTransactions l
-- | Convenience aliases.
accountnames :: Ledger -> [AccountName]
accountnames = ledgerAccountNames
account :: Ledger -> AccountName -> Account
account = ledgerAccount
accounts :: Ledger -> [Account]
accounts = ledgerAccounts
topaccounts :: Ledger -> [Account]
topaccounts = ledgerTopAccounts
accountsmatching :: [String] -> Ledger -> [Account]
accountsmatching = ledgerAccountsMatching
subaccounts :: Ledger -> Account -> [Account]
subaccounts = ledgerSubAccounts
transactions :: Ledger -> [Transaction]
transactions = ledgerTransactions
accounttree :: Int -> Ledger -> Tree Account
accounttree = ledgerAccountTree
accounttreeat :: Ledger -> Account -> Maybe (Tree Account)
accounttreeat = ledgerAccountTreeAt
datespan :: Ledger -> DateSpan
datespan = ledgerDateSpan
rawdatespan :: Ledger -> DateSpan
rawdatespan = rawLedgerDateSpan . rawledger
ledgeramounts :: Ledger -> [MixedAmount]
ledgeramounts = rawLedgerAmounts . rawledger

View File

@ -877,7 +877,7 @@ tests = [
,"subAccounts" ~: do ,"subAccounts" ~: do
l <- sampleledger l <- sampleledger
let a = ledgerAccount l "assets" let a = ledgerAccount l "assets"
(map aname $ subAccounts l a) `is` ["assets:bank","assets:cash"] (map aname $ ledgerSubAccounts l a) `is` ["assets:bank","assets:cash"]
,"summariseTransactionsInDateSpan" ~: do ,"summariseTransactionsInDateSpan" ~: do
let (b,e,tnum,depth,showempty,ts) `gives` summaryts = let (b,e,tnum,depth,showempty,ts) `gives` summaryts =