FeatureFlippers (FeatureFlippers v0.1.2) View Source

Provides a mechanism to turn features on and off within your application.

Declare a module where you'll define the available FFs and just declare them all:

defmodule Flags do
    use FeatureFlippers, otp_app: :my_app

    feature_flipper :foo?
    feature_flipper :bar?, expires: "2000-01-01"
    feature_flipper :baz?, always_disabled: true
end

Each feature_flipper/2 declaration defines a function returning boolean in the containing module. So, in your code, use them as follows:

if Flags.foo? do
    # new feature
else
    # original code
end

It means that if you remove a feature flipper declaration, your code won't compile and mix will also indicate where the missing feature flippers are being used. This may be useful when decommissioning feature flippers.

You can turn them on/off in compile time setting them in your config/config.exs (or in runtime through config/runtime.exs):

config :my_app, Flags,
  foo?: true
  bar?: false
  baz: true

You can also set them later by updating the application environment:

Application.get_env(:my_app, Flags)
|> Keyword.update(:bar?, true, fn _ -> true end)
|> (&Application.put_env(:my_app, Flags, &1)).()

While running unit tests, you might want to enable them all:

Flags.all()
|> Enum.map(fn key -> {key, true} end)
|> (&Application.put_env(:my_app, Flags, &1)).()

At any moment, you can discover which feature flippers have expired calling expired/0.

Link to this section Summary

Link to this section Functions

Link to this macro

feature_flipper(feature_flipper_name, options \\ [])

View Source (macro)

Defines a feature flipper.

The feature_flipper_name must end with ?.

Examples

feature_flipper :foo?
feature_flipper :bar?, expires: "2000-01-01"
feature_flipper :baz?, always_disabled: true

Options

feature_flipper/2 accepts the following options:

  • :expires - a string in YYYY-MM-DD format representing the date when it should expire. FeatureFlippers does not alter any feature flipper flag in execution when they expire; this works as informational indication to developers when the respective flag should be decommissioned. The function expired/0 will also show the expired flag names.

  • :always_disabled - a boolean indicating if the feature flipper should be forcefully disabled. This is defined in compile time, so if a feature flipper has always_disabled: true, it means that no application configuration will be read; the developer should reenable it and recompile the code if they need to turn it on again. It can be used when new features are not prepared for production yet, therefore they should never have the capability to be turned on.