Packages

Fast feature toggling for applications, with plugable backends.

Current section

Files

Jump to
flippant lib flippant rules.ex
Raw

lib/flippant/rules.ex

defmodule Flippant.Rules do
@moduledoc """
The Rules module glues rules, actors, and groups are together.
"""
@type rules :: Enumerable.t()
@type actor :: term()
alias Flippant.Registry
@doc """
Check whether any rules are enabled for a particular actor. The function
accepts a list of names/value pairs and an actor.
# Example
Flippant.Rules.enabled_for_actor?(rules, actor, groups)
Without a third argument of the groups to be checked it falls back to
collecting the globally registered groups.
"""
@spec enabled_for_actor?(rules(), actor(), map() | nil) :: boolean()
def enabled_for_actor?(rules, actor, groups \\ nil) do
groups = groups || Registry.registered()
Enum.any?(rules, fn {name, values} ->
if fun = Map.get(groups, name) do
apply(fun, [actor, values])
end
end)
end
end