Durable.Helpers (Durable v0.1.0-rc)

View Source

Helper functions for working with workflow data in the pipeline model.

These functions provide a convenient, pipe-friendly API for updating the data that flows through workflow steps.

Usage

use Durable.Helpers

This imports all helper functions into your workflow module.

Examples

step :process, fn data ->
  {:ok, data
  |> assign(:status, :processing)
  |> assign(%{started_at: DateTime.utc_now()})
  |> increment(:step_count)}
end

Summary

Functions

Injects helper functions into the calling module.

Appends a value to a list in the data.

Merges a map into the data.

Assigns a value to a key in the data map.

Deletes a key from the data.

Gets a value from the data.

Gets a value from the data with a default.

Checks if a key exists in the data.

Increments a numeric value in the data.

Updates an existing value in the data using a function.

Updates a value in the data using a function.

Functions

__using__(opts)

(macro)

Injects helper functions into the calling module.

append(data, key, value)

@spec append(map(), atom() | String.t(), term()) :: map()

Appends a value to a list in the data.

If the key doesn't exist, creates a new list with the value.

Examples

append(data, :items, new_item)
append(data, :events, %{type: :clicked, time: DateTime.utc_now()})

assign(data, map)

@spec assign(map(), map()) :: map()

Merges a map into the data.

Examples

assign(data, %{order_id: 123, status: :pending})

assign(data, key, value)

@spec assign(map(), atom() | String.t(), term()) :: map()

Assigns a value to a key in the data map.

Examples

assign(data, :order_id, 123)
assign(data, "order_id", 123)

delete(data, key)

@spec delete(map(), atom() | String.t()) :: map()

Deletes a key from the data.

Examples

delete(data, :temporary_value)

get(data, key)

@spec get(map(), atom() | String.t()) :: term()

Gets a value from the data.

Returns nil if the key doesn't exist.

Examples

order_id = get(data, :order_id)

get(data, key, default)

@spec get(map(), atom() | String.t(), term()) :: term()

Gets a value from the data with a default.

Examples

count = get(data, :retry_count, 0)

has_key?(data, key)

@spec has_key?(map(), atom() | String.t()) :: boolean()

Checks if a key exists in the data.

Examples

if has_key?(data, :order_id) do
  # ...
end

increment(data, key, amount \\ 1)

@spec increment(map(), atom() | String.t(), number()) :: map()

Increments a numeric value in the data.

If the key doesn't exist, starts from 0.

Examples

increment(data, :retry_count)
increment(data, :total, 100)

update(data, key, fun)

@spec update(map(), atom() | String.t(), (term() -> term())) :: map()

Updates an existing value in the data using a function.

Raises if the key doesn't exist.

Examples

update(data, :count, &(&1 + 1))

update(data, key, default, fun)

@spec update(map(), atom() | String.t(), term(), (term() -> term())) :: map()

Updates a value in the data using a function.

If the key doesn't exist, uses the default value.

Examples

update(data, :count, 0, &(&1 + 1))
update(data, :items, [], &[new_item | &1])