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/where.ex
defmodule Reactor.Dsl.Where do
@moduledoc """
A struct used to store the `where` DSL entity.
See `d:Reactor.step.where`.
"""
defstruct __identifier__: nil, description: nil, predicate: nil
alias Reactor.Guard
@type t :: %__MODULE__{
__identifier__: any,
description: nil | String.t(),
predicate:
(Reactor.inputs() -> boolean) | (Reactor.inputs(), Reactor.context() -> boolean)
}
@doc false
def __entity__,
do: %Spark.Dsl.Entity{
name: :where,
describe: """
Only execute the surrounding step if the predicate function returns true.
This is a simple version of `guard` which provides more flexibility at the cost of complexity.
""",
examples: [
"""
step :read_file do
argument :path, input(:path)
run &File.read(&1.path)
where &File.exists?(&1.path)
end
"""
],
args: [:predicate],
target: __MODULE__,
schema: [
predicate: [
type: {:or, [{:mfa_or_fun, 1}, {:mfa_or_fun, 2}]},
required: true,
doc: """
Provide a function which takes the step arguments and optionally the context and returns a boolean value.
"""
],
description: [
type: :string,
required: false,
doc: """
An optional description of the guard.
"""
]
]
}
@doc false
def eval(arguments, context, where) do
if eval_predicate(arguments, context, where) do
:cont
else
{:halt, {:ok, nil}}
end
end
defp eval_predicate(arguments, context, where) when is_function(where.predicate, 2),
do: where.predicate.(arguments, context)
defp eval_predicate(arguments, _context, where) when is_function(where.predicate, 1),
do: where.predicate.(arguments)
defimpl Guard.Build do
@doc false
def build(where) do
{:ok,
[
%Guard{fun: {Reactor.Dsl.Where, :eval, [where]}}
]}
end
end
end