Bookk.NaiveState (bookk v0.1.4)
View SourceA state struct that holds multiple ledgers. It's considered "naive" because it doesn't hold any information regarding the journal entries that put the state into its current value.
Related
Summary
Functions
Produces a empty naive state.
Get's a ledger from the state by its name. If the ledger doesn't exist in the state yet, then a new empty ledger will be returned.
Produces a new state struct from a set of ledgers.
Posts a Bookk.InterledgerEntry
to the state, appling changes in
balance to multiple accounts accross multiple ledgers.
Types
@type t() :: %Bookk.NaiveState{ ledgers_by_name: %{required(name :: String.t()) => Bookk.Ledger.t()} }
The struct representing a naive state.
Fields
ledgers_by_name
: the ledgers known by the state, grouped by their name.
Functions
@spec empty() :: t()
Produces a empty naive state.
@spec get_ledger(t(), String.t()) :: Bookk.Ledger.t()
Get's a ledger from the state by its name. If the ledger doesn't exist in the state yet, then a new empty ledger will be returned.
Examples
Returns an empty ledger when requested ledger doesn't exist in state:
iex> Bookk.NaiveState.get_ledger(%Bookk.NaiveState{}, "acme")
%Bookk.Ledger{name: "acme"}
Returns the ledger when it exists in state:
iex> state = %Bookk.NaiveState{
iex> ledgers_by_name: %{
iex> "foo" => %Bookk.Ledger{
iex> accounts_by_name: %{
iex> "cash" => %Bookk.Account{}
iex> }
iex> }
iex> }
iex> }
iex>
iex> Bookk.NaiveState.get_ledger(state, "foo")
%Bookk.Ledger{
accounts_by_name: %{
"cash" => %Bookk.Account{}
}
}
@spec new([Bookk.Ledger.t()]) :: t()
Produces a new state struct from a set of ledgers.
@spec post(t(), Bookk.InterledgerEntry.t()) :: t()
Posts a Bookk.InterledgerEntry
to the state, appling changes in
balance to multiple accounts accross multiple ledgers.
Examples
iex> import Bookk.Notation, only: [journalize!: 2]
iex>
iex> user_id = "123"
iex> deposited_amount = 500_00
iex>
iex> journal_entry =
iex> journalize! using: TestChartOfAccounts do
iex> on ledger(:acme) do
iex> debit account(:cash), deposited_amount
iex> credit account({:unspent_cash, {:user, user_id}}), deposited_amount
iex> end
iex>
iex> on ledger({:user, user_id}) do
iex> debit account(:cash), deposited_amount
iex> credit account(:deposits), deposited_amount
iex> end
iex> end
iex>
iex> Bookk.NaiveState.empty()
iex> |> Bookk.NaiveState.post(journal_entry)
%Bookk.NaiveState{
ledgers_by_name: %{
"acme" => %Bookk.Ledger{
name: "acme",
accounts_by_name: %{
fixture_account_head(:cash).name => %Bookk.Account{
head: fixture_account_head(:cash),
balance: 500_00
},
fixture_account_head({:unspent_cash, {:user, "123"}}).name => %Bookk.Account{
head: fixture_account_head({:unspent_cash, {:user, "123"}}),
balance: 500_00
}
}
},
"user(123)" => %Bookk.Ledger{
name: "user(123)",
accounts_by_name: %{
fixture_account_head(:cash).name => %Bookk.Account{
head: fixture_account_head(:cash),
balance: 500_00
},
fixture_account_head(:deposits).name => %Bookk.Account{
head: fixture_account_head(:deposits),
balance: 500_00
}
}
}
}
}