Unleash.Flags (Unleash v5.1.2)

View Source

Generates a feature-flag facade module with one predicate function per flag.

Define a module in your application and use Unleash.Flags:

defmodule MyApp.Features do
  use Unleash.Flags
end

With

config :unleash_fresha, Unleash, allowed_features: [:my_flag, :other_flag]

this generates, at compile time:

MyApp.Features.my_flag(context \\ %{})
MyApp.Features.other_flag(context \\ %{})

Each function queries the cache through the configured runtime:

MyApp.Features.my_flag(%{user_id: "42"})
# => Unleash.Runtime.enabled?(:my_flag, context: %{user_id: "42"})

The functions are generated at compile time, so they exist for tooling (autocomplete, Dialyzer, no "undefined function" warnings). The flag list is read from config when this module compiles; if you change :allowed_features, recompile the facade module (e.g. mix compile --force) to regenerate it.

Options

  • :features - the list of flag names (atoms or strings) to generate functions for. Defaults to the configured :allowed_features. Must be a concrete list; the nil "cache everything" default cannot be used here since the function names must be known at compile time.

  • :transform - a 1-arity function mapping a raw flag name (string) to the function name (string), which is turned into an atom verbatim. The transform owns the full name: downcase it and append ? yourself if you want those. Defaults to &String.downcase/1 (a downcasing identity). The feature name passed to the runtime is always the real, downcased flag — only the function name is transformed. For example, to turn TEAM_X_FEATURE_Y into feature_y_enabled?:

    use Unleash.Flags,
      features: ["TEAM_X_FEATURE_Y"],
      transform: fn name ->
        name |> String.downcase() |> String.replace_prefix("team_x_", "") |> Kernel.<>("_enabled?")
      end

    generates feature_y_enabled?/1, which queries enabled?(:team_x_feature_y, ...).

  • :runtime - the module the generated functions delegate to. Defaults to Unleash.Runtime; pass this option to inject a test runtime for the facade.