EctoCommand.Middleware.Pipeline (EctoCommand v0.2.7)
View SourcePipeline 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 commandmetadata
- additional metadata, they could be used to fill internal command fieldshalted
- flag indicating whether the pipeline was halted.handler
- handler module where the "execute/1" function residesmiddlewares
- the list of middlewares to be executedresponse
- 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
Functions
Puts the key
with value equal to value
into assigns
map.
Examples
iex> pipeline = assign(%Pipeline{}, :foo, :bar)
iex> pipeline.assigns
%{foo: :bar}
Executes the middleware chain.
Sets the error
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
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
iex> pipeline = %Pipeline{}
iex> pipeline = halt(pipeline)
iex> halted?(pipeline)
true
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
Has the pipeline been halted?
Examples
iex> true = halted?(%Pipeline{halted: true})
iex> false = halted?(%Pipeline{halted: false})
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"}
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 the key
with value
Examples
iex> pipeline = set(%Pipeline{}, :command, :my_command)
iex> pipeline.command
:my_command
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"}