Packages
reactor
0.13.1
1.0.2
1.0.1
1.0.0
0.17.0
0.16.0
0.15.6
0.15.5
0.15.4
0.15.3
0.15.2
0.15.1
0.15.0
0.14.0
0.13.3
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.0
0.10.3
0.10.2
0.10.1
0.10.0
0.9.1
0.9.0
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.0
0.6.0
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.0
An asynchronous, graph-based execution engine
Current section
Files
Jump to
Current section
Files
lib/reactor/dsl/guard.ex
defmodule Reactor.Dsl.Guard do
@moduledoc """
A struct used to store the `guard` DSL entity.
See `d:Reactor.step.guard`
"""
defstruct __identifier__: nil, description: nil, fun: nil
alias Reactor.Guard
@type t :: %__MODULE__{
__identifier__: any,
description: nil | String.t(),
fun: (Reactor.inputs(), Reactor.context() -> :cont | {:halt, Reactor.Step.run_result()})
}
@doc false
def __entity__,
do: %Spark.Dsl.Entity{
name: :guard,
describe: """
Provides a flexible method for conditionally executing a step, or replacing it's result.
Expects a two arity function which takes the step's arguments and context and returns one of the following:
- `:cont` - the guard has passed.
- `{:halt, result}` - the guard has failed - instead of executing the step use the provided result.
""",
examples: [
"""
step :read_file_via_cache do
argument :path, input(:path)
run &File.read(&1.path)
guard fn %{path: path}, %{cache: cache} ->
case Cache.get(cache, path) do
{:ok, content} -> {:halt, {:ok, content}}
_ -> :cont
end
end
end
"""
],
args: [:fun],
target: __MODULE__,
schema: [
fun: [
type: {:mfa_or_fun, 2},
required: true,
doc: """
The guard function.
"""
],
description: [
type: :string,
required: false,
doc: """
An optional description of the guard.
"""
]
]
}
defimpl Guard.Build do
@doc false
def build(guard) do
{:ok, [%Guard{fun: guard.fun}]}
end
end
end