Unleash.Strategy behaviour (Unleash v5.1.2)

View Source

Used to extend the client and create custom strategies. To do so, use this module within your custom strategy and implmenent enabled?/2. Provide a name that is human-readable, as it is logged.

defmodule MyApp.CustomStrategy
  use Unleash.Strategy, name: "CustomStrategy"

  def enabled?(_params, _context), do: true
end

Summary

Types

Debug metadata returned alongside a strategy result to explain how it was evaluated.

t()

Callbacks

You can implement this callback a couple of ways, returning a bare boolean() or a {boolean, map()}. The latter is preferred, as it generates a :debug level log entry detailing the name of the strategy, the result, and the contents of map(), in an effort to help understand why the result was what it was.

Types

evaluation_metadata()

@type evaluation_metadata() :: map()

Debug metadata returned alongside a strategy result to explain how it was evaluated.

t()

@type t() :: %Unleash.Strategy{
  constraints: list() | nil,
  name: String.t() | nil,
  parameters: map() | nil,
  segments: list(),
  variants: list()
}

Callbacks

enabled?(parameters, context)

@callback enabled?(parameters :: map(), context :: Unleash.context()) ::
  boolean() | {boolean(), map()}

You can implement this callback a couple of ways, returning a bare boolean() or a {boolean, map()}. The latter is preferred, as it generates a :debug level log entry detailing the name of the strategy, the result, and the contents of map(), in an effort to help understand why the result was what it was.

Arguments

  • parameters - A map of parameters returned from the Unleash server. This can be whatever you like, such as a configured list of userIds.
  • context - The context passed into Unleash.enabled?/2.

Examples

@behaviour Unleash.Strategy

def enabled?(params, context), do: {false, params}

def enabled(params, %{}), do: false