View Source EctoCommand.Middleware.Pipeline (EctoCommand v0.1.0)

Pipeline is a struct used as an argument in the callback functions of modules implementing the EctoCommand.Middleware behaviour.

This struct must be returned by each function to be used in the next middleware based on the configured middleware chain.

pipeline-fields

Pipeline fields

  • assigns - shared user data as a map.

  • command_uuid - UUID assigned to the command being executed.

  • command - command struct being executed.

  • params - raw params received to instantiate the command

  • metadata - additional metadata, they could be used to fill internal command fields

  • halted - flag indicating whether the pipeline was halted.

  • handler - handler module where the "execute/1" function resides

  • middlewares - the list of middlewares to be executed

  • response - sets the response to send back to the caller.

  • error - sets the error to send back to the caller.

Link to this section Summary

Functions

Puts the key with value equal to value into assigns map.

Executes the middleware chain.

Sets the error

Executes the function 'execute/1' in the handler module, pass the command to it. Halt the pipeline if command or handler are not set

Halts the pipeline by preventing further middleware downstream from being invoked.

Halts the pipeline by preventing further middleware downstream from being invoked.

Has the pipeline been halted?

Sets the response to be returned to the dispatch caller

Extract the response from the pipeline, return the error if it is set return the stored response otherwise return nil if no response is set

Set the key with value

Update the key with function function that receive the key value.

Link to this section Functions

Link to this function

assign(pipeline, key, value)

View Source

Puts the key with value equal to value into assigns map.

examples

Examples

iex> pipeline = assign(%Pipeline{}, :foo, :bar)
iex> pipeline.assigns
%{foo: :bar}
Link to this function

chain(pipeline, stage, middleware)

View Source

Executes the middleware chain.

Sets the error

examples

Examples

iex> pipeline = %Pipeline{}
iex> pipeline = Pipeline.error(pipeline, "an_error")
iex> Pipeline.response(pipeline)
{:error, "an_error"}

Executes the function 'execute/1' in the handler module, pass the command to it. Halt the pipeline if command or handler are not set

examples

Examples

iex> %Pipeline{halted: true} = Pipeline.execute(%Pipeline{halted: true})
iex> %Pipeline{response: {:error, "command was not initialized"}} = Pipeline.execute(%Pipeline{handler: Pipeline})
iex> %Pipeline{response: {:error, "handler was not set"}} = Pipeline.execute(%Pipeline{command: %{}})
iex> %Pipeline{response: {:ok, :result}} = Pipeline.execute(%Pipeline{handler: SampleCommand, command: %SampleCommand{}})

Halts the pipeline by preventing further middleware downstream from being invoked.

Prevents execution of the command if halt occurs in a before_execution callback.

examples

Examples

iex> pipeline = %Pipeline{}
iex> pipeline = halt(pipeline)
iex> halted?(pipeline)
true
Link to this function

halt(pipeline, response)

View Source

Halts the pipeline by preventing further middleware downstream from being invoked.

Prevents execution of the command if halt occurs in a before_execution callback.

Similar to halt/1 but allows a response to be returned to the caller.

examples

Examples

iex> pipeline = %Pipeline{}
iex> pipeline = halt(pipeline, {:error, "halted"})
iex> response(pipeline)
{:error, "halted"}
iex> halted?(pipeline)
true

Has the pipeline been halted?

examples

Examples

iex> true = halted?(%Pipeline{halted: true})
iex> false = halted?(%Pipeline{halted: false})
Link to this function

respond(pipeline, response)

View Source

Sets the response to be returned to the dispatch caller

examples

Examples

iex> pipeline = %Pipeline{}
iex> pipeline = Pipeline.respond(pipeline, {:error, "halted"})
iex> Pipeline.response(pipeline)
{:error, "halted"}

Extract the response from the pipeline, return the error if it is set return the stored response otherwise return nil if no response is set

examples

Examples

iex> pipeline = %Pipeline{}
iex> pipeline = Pipeline.error(pipeline, "halted")
iex> Pipeline.response(pipeline)
{:error, "halted"}
Link to this function

set(pipeline, key, value)

View Source

Set the key with value

examples

Examples

iex> pipeline = set(%Pipeline{}, :command, :my_command)
iex> pipeline.command
:my_command
Link to this function

update!(pipeline, key, function)

View Source

Update the key with function function that receive the key value.

examples

Examples

iex> pipeline = %Pipeline{command: %{name: "original"}}
iex> pipeline = update!(pipeline, :command, fn command -> %{command | name: "updated"} end)
iex> pipeline.command
%{name: "updated"}