DoubleEntryLedger.Command.Helper (double_entry_ledger v0.4.0)

View Source

Helper functions

Summary

Types

account_action()

@type account_action() :: :update_account | :create_account

transaction_action()

@type transaction_action() :: :update_transaction | :create_transaction

Functions

action_to_mod(command_map)

actions(atom)

@spec actions(:transaction) :: [transaction_action()]
@spec actions(:account) :: [account_action()]

address_regex()

See DoubleEntryLedger.Account.address_regex/0.

fetch_action(attrs)

@spec fetch_action(map()) :: atom() | nil

Fetches and normalizes the action value from a map.

Accepts both atom and string keys ("action" and :action). When the action is a string, it is converted using String.to_existing_atom/1. Returns nil when no action is present.

This function is useful for handling incoming data that may have string or atom keys, which is common when dealing with external APIs or JSON data.

Parameters

  • attrs - Map containing potential action data

Returns

  • atom() - The normalized action as an atom
  • nil - When no action is found

Examples

iex> alias DoubleEntryLedger.Command.Helper
iex> # Ensure atoms exist for to_existing_atom/1
iex> :create_transaction
:create_transaction
iex> :update_transaction
:update_transaction
iex> Helper.fetch_action(%{"action" => "create_transaction"})
:create_transaction
iex> Helper.fetch_action(%{action: :update_transaction})
:update_transaction
iex> Helper.fetch_action(%{})
nil
iex> Helper.fetch_action(%{"other_key" => "value"})
nil

source_regex()

@spec source_regex() :: Regex.t()