ExLedger.Predicate.Evaluator (ex_ledger v0.6.1)

Evaluates predicate ASTs against transaction/posting contexts.

Takes an AST produced by ExLedger.Predicate.Parser and evaluates it against a context containing transaction and posting data.

Example

iex> ast = {:account_regex, ~r/^Expenses:/}
iex> ctx = Evaluator.context(transaction, posting)
iex> Evaluator.matches?(ast, ctx)
true

Summary

Functions

Creates an evaluation context from a transaction and posting.

Parses a predicate string and evaluates it against a context.

Finds all postings in a transaction that match a predicate.

Evaluates a predicate AST against an evaluation context.

Types

ast()

Functions

context(transaction, posting, opts \\ [])

Creates an evaluation context from a transaction and posting.

Options

  • :posting_index - Index of the posting in the transaction (default: 0)

evaluate(predicate, context)

@spec evaluate(String.t(), ExLedger.Predicate.Evaluator.Context.t()) ::
  {:ok, boolean()} | {:error, term()}

Parses a predicate string and evaluates it against a context.

Combines Parser.parse/1 and matches?/2 for convenience.

find_matching_postings(predicate, transaction)

@spec find_matching_postings(String.t() | ast(), map()) ::
  {:ok, [{map(), non_neg_integer()}]} | {:error, term()}

Finds all postings in a transaction that match a predicate.

Returns a list of {posting, index} tuples for matching postings.

matches?(arg, context)

Evaluates a predicate AST against an evaluation context.

Returns true if the predicate matches, false otherwise.