StateMachine.Guard (state_machine v0.1.8)

Guards are functions that help to decide whether it's allowed to proceed with Event or Transition. They might serve different purposes:

  • Preventing a state machine from getting into a state unless some criteria met
  • Creating event with multiple target states with a single source state

Guards should not have any side-effects, cause they are getting run no matter if transition is successful or not, and also to determine the list of possible events for a certain state.

Summary

Functions

Check runs guards associated with given Event or Transition and returns true if all passed. First argument of the guard is a model, second argument is the context.

Unifies if and unless guards into a single stream of guards.

Types

t(model)

@type t(model) :: %StateMachine.Guard{
  arity: integer(),
  fun:
    (model, StateMachine.Context.t(model) -> boolean())
    | (model -> boolean())
    | (-> boolean()),
  inverted: boolean()
}

Functions

check(ctx, map)

Check runs guards associated with given Event or Transition and returns true if all passed. First argument of the guard is a model, second argument is the context.

prepare(opts)

@spec prepare(keyword()) :: [t(any())]

Unifies if and unless guards into a single stream of guards.