EctoCommand.Middleware.Pipeline (EctoCommand v0.2.7)

View Source

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

  • 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.

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.

Types

t()

@type t() :: %EctoCommand.Middleware.Pipeline{
  assigns: map(),
  command: struct() | nil,
  error: any() | nil,
  halted: boolean(),
  handler: atom(),
  metadata: map(),
  middlewares: [tuple()],
  params: map(),
  response: any() | nil
}

Functions

assign(pipeline, key, value)

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

Examples

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

chain(pipeline, stage, middleware)

Executes the middleware chain.

error(pipeline, error)

Sets the error

Examples

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

execute(pipeline)

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

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{}})

halt(pipeline)

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

iex> pipeline = %Pipeline{}
iex> pipeline = halt(pipeline)
iex> halted?(pipeline)
true

halt(pipeline, response)

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

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

halted?(pipeline)

Has the pipeline been halted?

Examples

iex> true = halted?(%Pipeline{halted: true})
iex> false = halted?(%Pipeline{halted: false})

respond(pipeline, response)

Sets the response to be returned to the dispatch caller

Examples

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

response(pipeline)

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

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

set(pipeline, key, value)

Set the key with value

Examples

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

update!(pipeline, key, function)

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

Examples

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