Patterns.Utils (patterns v0.0.1)

Copy Markdown View Source

Utilities for building pattern modules.

Summary

Functions

Returns a value from the current scoped context.

Defines delegates for every public function exported by module.

Runs block with scoped process-local context.

Functions

ctx(key)

@spec ctx(term()) :: term()

Returns a value from the current scoped context.

See with_ctx/2 for more information.

Example

ctx(:binding)
#=> nil

with_ctx binding: :references do
  ctx(:binding)
  #=> :references
end

ctx(:binding)
#=> nil

defdelegate_all(module)

(macro)

Defines delegates for every public function exported by module.

Example

defmodule MyDelegates do
  import Patterns.Utils

  defdelegate_all String
end

MyDelegates.upcase("hello")

Private functions are ignored because only public functions are returned by module.__info__(:functions).

with_ctx(ctx, list)

(macro)

Runs block with scoped process-local context.

ctx may be a map or keyword list. It is converted to a map and merged into the current context for the duration of the block. Nested calls override duplicate keys while preserving sibling keys.

Process-local context

Context is stored in the process dictionary. It is visible only to the current process and does not cross Task, spawned process, Dataloader, or other async boundaries.

The previous context is restored after the block exits, including when the block raises, throws, or exits.

Example

ctx(:binding)
#=> nil

with_ctx binding: :references do
  ctx(:binding)
  #=> :references
end

ctx(:binding)
#=> nil