DoubleEntryLedger.Command.Helper (double_entry_ledger v0.2.0)
View SourceHelper functions
Summary
Functions
Fetches and normalizes the action value from a map.
Types
Functions
@spec actions(:transaction) :: [transaction_action()]
@spec actions(:account) :: [account_action()]
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 atomnil- 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
@spec source_regex() :: Regex.t()