Flippant.Rules behaviour (Flippant v2.0.0) View Source

The Rules module glues rules, actors, and groups are together.

Link to this section Summary

Functions

Check whether any rules are enabled for a particular actor. The function accepts a list of names/value pairs and an actor.

Callbacks

Validate that an actor is both a member of group and in the enabled_for list. For example, if we had a staff group, and a rule containing %{"staff" => ids}, we could have a function like this

Link to this section Types

Link to this section Functions

Link to this function

enabled_for_actor?(name \\ Flippant, rules, actor)

View Source

Specs

enabled_for_actor?(name :: atom(), rules(), actor()) :: boolean()

Check whether any rules are enabled for a particular actor. The function accepts a list of names/value pairs and an actor.

Link to this section Callbacks

Link to this callback

enabled?(group, enabled_for, actor)

View Source

Specs

enabled?(group :: binary(), enabled_for :: list(), actor :: Rules.actor()) ::
  boolean()

Validate that an actor is both a member of group and in the enabled_for list. For example, if we had a staff group, and a rule containing %{"staff" => ids}, we could have a function like this:

  def enabled?("staff", enabled_for, %{staff: true} = actor) do
    Enum.any?(enabled_for, &actor.id == &1.id)
  end

This both validates the actor is staff, and that the actor is in the enabled list. Allowing rules definition in this way makes the logic for rule checking very visible in application code.