View Source Flamel.Context (flamel v1.0.0)
A Context can be used to in a pipeline to assign data to that future functions can have access to and transform. It also includes a boolean value that signals to other functions in the pipeline whether they should process the context or not.
Example
alias Flamel.Context
context =
Context.new()
|> assign_user(user)
|> authorize()
|> perform_action()
if context.assigns[:action_performed?] do
# something you are allowed to do
end
def assign_user?(%Context{} = context, user) do
Context.assign(context, :user, user)
end
def authorize(%Context{assigns: %{user: %{type: :admin}} = context) do
context
end
def authorize(%Context{} = context) do
Context.halt!(context, "Not permitted")
end
def perform_action(%Context{halt: true}) do
# do nothing
context
end
def perform_action(%Context{assigns: %{user: user}} = context) do
# Do something
Context.assign(context, %{action_performed_by: user, action_performed?: true})
end
Summary
Functions
Merges values in a map into the assigns in the context. It does not perform a deep merge.
Assigns a value to a key in the context.
Signals to further functions in the pipeline that processing should stop
Has the context been halted?
Build a new Context
Signals to further functions in the pipeline that processing should resume
Types
Flamel.Context
Functions
@spec assign(%Flamel.Context{assigns: term(), halt: term(), reason: term()}, map()) :: %Flamel.Context{ assigns: term(), halt: term(), reason: term() }
Merges values in a map into the assigns in the context. It does not perform a deep merge.
Examples
iex> context = Flamel.Context.assign(Flamel.Context.new(), %{hello: :world})
iex> context.assigns[:hello]
:world
@spec assign( %Flamel.Context{assigns: term(), halt: term(), reason: term()}, binary() | atom(), term() ) :: %Flamel.Context{assigns: term(), halt: term(), reason: term()}
Assigns a value to a key in the context.
The assigns is meant to be used to store values in the context so that other functions in your pipeline can access them. The assigns storage is a map.
Examples
iex> context = %Flamel.Context{}
iex> context.assigns[:hello]
nil
iex> context = Flamel.Context.assign(context, :hello, :world)
iex> context.assigns[:hello]
:world
Signals to further functions in the pipeline that processing should stop
Examples
iex> context = %Flamel.Context{}
iex> context.halt
false
iex> context = Flamel.Context.halt!(context)
iex> context.halt
true
iex> context = %Flamel.Context{}
iex> context.halt
false
iex> context = Flamel.Context.halt!(context, "some error message")
iex> context.halt
true
iex> context.reason
"some error message"
Has the context been halted?
Examples
iex> context = Flamel.Context.halt!(%Flamel.Context{})
iex> Flamel.Context.halted?(context)
true
Build a new Context
Signals to further functions in the pipeline that processing should resume
Examples
iex> context = %Flamel.Context{halt: true}
iex> context.halt
true
iex> context = Flamel.Context.resume!(context)
iex> context.halt
false